All hash functions either calculate a hash-digest for key == NULL
or HMAC
(hashed message authentication code) when key
is not NULL
. Supported
inputs are binary (raw vector), strings (character vector) or a connection object.
sha1(x, key = NULL)sha224(x, key = NULL)
sha256(x, key = NULL)
sha384(x, key = NULL)
sha512(x, key = NULL)
keccak(x, size = 256, key = NULL)
sha2(x, size = 256, key = NULL)
sha3(x, size = 256, key = NULL)
md4(x, key = NULL)
md5(x, key = NULL)
blake2b(x, key = NULL)
blake2s(x, key = NULL)
ripemd160(x, key = NULL)
multihash(x, algos = c("md5", "sha1", "sha256", "sha384", "sha512"))
character vector, raw vector or connection object.
string or raw vector used as the key for HMAC hashing
must be equal to 224 256 384 or 512
string vector with names of hashing algorithms
The most efficient way to calculate hashes is by using input connections, such as a file() or url() object. In this case the hash is calculated streamingly, using almost no memory or disk space, regardless of the data size. When using a connection input in the multihash function, the data is only read only once while streaming to multiple hash functions simultaneously. Therefore several hashes are calculated simultanously, without the need to store any data or download it multiple times.
Functions are vectorized for the case of character vectors: a vector with n
strings returns n
hashes. When passing a connection object, the contents will
be stream-hashed which minimizes the amount of required memory. This is recommended
for hashing files from disk or network.
The sha2 family of algorithms (sha224, sha256, sha384 and sha512) is generally recommended for sensitive information. While sha1 and md5 are usually sufficient for collision-resistant identifiers, they are no longer considered secure for cryptographic purposes.
In applications where hashes should be irreversible (such as names or passwords) it is often recommended to use a random key for HMAC hashing. This prevents attacks where we can lookup hashes of common and/or short strings. See examples. A common special case is adding a random salt to a large number of records to test for uniqueness within the dataset, while simultaneously rendering the results incomparable to other datasets.
The blake2b
and blake2s
algorithms are only available if your system has
libssl 1.1 or newer.
NB R base file()
function has a poor default raw = FALSE
which causes files to get
altereted (e.g. decompressed) when reading. Use file(path, raw = TRUE)
to get the
hash of the file as it exists on your disk.
Digest types: https://docs.openssl.org/1.1.1/man1/dgst/