Learn R Programming

nanonext (version 0.9.2)

sha1: Cryptographic Hashing Using the SHA-1 Algorithm

Description

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.

Usage

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

Value

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

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. Supplying a non-logical value will error.

Details

For arguments 'x' and 'key', a scalar string or raw vector (with no attributes) is hashed directly, whilst all other objects are serialised first.

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

Run this code
# SHA-1 hash as character string:
sha1("hello world!")

# SHA-1 hash as raw vector:
sha1("hello world!", convert = FALSE)

# Obtain HMAC:
sha1("hello world!", "SECRET_KEY")

Run the code above in your browser using DataLab