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 directly, whilst all other objects are first serialised (using R serialisation version 3, big-endian representation).

The result of hashing is always a raw vector, which is translated to a character string if 'convert' is TRUE, or returned directly 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"