代码之家  ›  专栏  ›  技术社区  ›  lucacerone

将md5哈希转换为R中的bigint

  •  2
  • lucacerone  · 技术社区  · 10 年前

    我想知道如何将md5哈希转换为大整数,以便对其应用模运算符。

    我使用 digest 库:

    h <- digest("luca", algo="md5", ascii=TRUE, raw=TRUE)
    > h
    [1] 18 0e 41 2e 42 db 5a 8c 45 3c 8a 81 c5 90 4e 5b
    

    我现在想转换 h 并能够应用模运算符( %% )这是一个很好的例子。

    我该怎么做?

    1 回复  |  直到 10 年前
        1
  •  2
  •   Konrad Rudolph    10 年前

    使用Rmpfr库 1. ,以下工作:

    # Get a hex string of the MD5 hash:
    h = digest("luca", algo="md5", ascii = TRUE, raw = FALSE)
    result = mpfr(h, base = 16)
    result
    # 1 'mpfr' number of precision  128   bits
    # [1] 31975486076668374554448903959384968795
    
    result %% 1024
    # 1 'mpfr' number of precision  128   bits
    # [1] 603
    

    1. 要安装Rmpfr,需要安装其依赖项GNU mpfr库。更多信息请参见注释。