Returns a SHA-1 hash or HMAC of the supplied R object. Uses the optimised implementation from the Mbed TLS library. For secure applications, one of the SHA-2 algorithms such as sha256 should be considered instead.

sha1(x, key = NULL, convert = TRUE)

Arguments

x

an object.

key

(optional) supply a secret key to generate an HMAC. If missing or NULL, the SHA-1 hash of 'x' is returned.

convert

[default TRUE] logical value whether to convert the output to a character string or keep as a raw vector.

Value

A raw vector or character string depending on 'convert', of byte length 20.

Details

For arguments 'x' and 'key', a scalar string or raw vector (with no attributes) is hashed 'as is'.

All other objects are first serialized using R serialization version 3, big-endian representation, with the headers stripped (for portability as these contain the R version number and native encoding information).

The result of hashing is always a byte sequence, which is converted to a character string hex representation if 'convert' is TRUE, or returned as a raw vector if 'convert' is FALSE.

Examples

# SHA-1 hash as character string:
sha1("hello world!")
#> [1] "430ce34d020724ed75a196dfc2ad67c77772d169"

# SHA-1 hash as raw vector:
sha1("hello world!", convert = FALSE)
#>  [1] 43 0c e3 4d 02 07 24 ed 75 a1 96 df c2 ad 67 c7 77 72 d1 69

# Obtain HMAC:
sha1("hello world!", "SECRET_KEY")
#> [1] "aa362a42829ea0301720ec7d25fe8b441df4452f"