Cryptographic Hashing in R

knitr::opts_chunk$set(comment = "")

The functions sha1, sha256, sha512, md4, md5 and ripemd160 bind to the respective digest functions in OpenSSL's libcrypto. Both binary and string inputs are supported and the output type will match the input type.


Functions are fully vectorized for the case of character vectors: a vector with n strings will return n hashes.

# Vectorized for strings
md5(c("foo", "bar", "baz"))

Besides character and raw vectors we can pass a connection object (e.g. a file, socket or url). In this case the function will stream-hash the binary contents of the connection.

# Stream-hash a file
myfile <- system.file("CITATION")

Same for URLs. The hash of the R-installer.exe below should match the one in md5sum.txt

# Stream-hash from a network connection

# Compare

Compare to digest

Similar functionality is also available in the digest package, but with a slightly different interface:

# Compare to digest
digest("foo", "md5", serialize = FALSE)

# Other way around
digest(cars, skip = 0)
md5(serialize(cars, NULL))

Try the openssl package in your browser

Any scripts or data that you put into this service are public.

openssl documentation built on Sept. 26, 2023, 1:09 a.m.