lucacerone
lucacerone

Reputation: 10119

Convert md5 hash to bigint in R

I would like to know how to convert an md5 hash to a big integer so that I can apply the modulus operator to it.

I create the hash using the digest library:

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

I would now like to convert h to a big integer and be able to apply the modulus operator (%%) to it.

How can I do that?

Upvotes: 2

Views: 740

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545628

Using the Rmpfr library1, the following works:

# 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 To install Rmpfr, one needs to install its dependency, the GNU mpfr library. See comments for further information.

Upvotes: 2

Related Questions