Skip to contents

Computes the BLAKE2b hash of text or bytes as lowercase hex, with any digest length from 1 to 64 bytes. With a key it becomes a message authentication code: only someone holding the key can produce or check the digest. Use it for fingerprints and keyed checks inside your own workflow; use csha256() or chmac() where other tools must reproduce the value.

Usage

cblake2b(x, key = NULL, length = 32L)

Arguments

x

A character vector or a raw vector.

key

Optional key: a single string or a raw vector of at most 64 bytes. NULL (default) gives the plain digest.

length

Digest length in bytes, 1 to 64 (default 32, matching SHA-256's width).

Value

A character vector of lowercase hex digests – one per element for character input, length-1 for raw input.

Details

BLAKE2b (RFC 7693): a modern cryptographic hash, faster than SHA-256 in software, that takes a key natively and produces any digest length from 1 to 64 bytes.

The native key is the interesting part. A keyed BLAKE2b IS a message authentication code, with no HMAC wrapper and so no doubled hashing – cblake2b(msg, key = k) does the job of chmac() at lower cost. Use SHA-256 or HMAC where interoperability with other tools matters; use this where it does not and speed does.

Character input is hashed element by element, as the bytes of each string; NA elements give NA. A raw vector is one message. The digest length is a parameter of BLAKE2b, so a 32-byte digest is not the first half of the 64-byte digest of the same input. Errors: length outside 1 to 64; a key that is not a single string or raw vector, or is longer than 64 bytes.

References

Saarinen MJ, Aumasson JP (2015). The BLAKE2 Cryptographic Hash and Message Authentication Code (MAC). RFC 7693. doi:10.17487/RFC7693

Examples

# The RFC 7693 test vector for BLAKE2b-512 of "abc".
cblake2b("abc", length = 64)
#> [1] "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923"

# 32 bytes by default, the same width as SHA-256.
cblake2b("abc")
#> [1] "bddd813c634239723171ef3fee98579b94964e3bb1cb3e427262c8c068d52319"
nchar(cblake2b("abc"))
#> [1] 64

# Any digest length, which SHA-2 cannot do.
cblake2b("abc", length = 8)
#> [1] "d8bb14d833d59559"

# Keyed, so it authenticates without an HMAC construction.
cblake2b("manifest", key = "secret")
#> [1] "0fa448067a359db7f409a8de6a616a39d51393c32532bce014075308cbb1f222"
cblake2b("manifest", key = "secret") ==
  cblake2b("manifest", key = "other")
#> [1] FALSE

# Vectorised, and raw input hashes the same bytes.
cblake2b(c("a", "b"))
#> [1] "8928aae63c84d87ea098564d1e03ad813f107add474e56aedd286349c0c03ea4"
#> [2] "6e5c1f45cbaf19f94230ba3501c378a5335af71a331b5b5aed62792332288dc3"
identical(cblake2b("abc"), cblake2b(charToRaw("abc")))
#> [1] TRUE

try(cblake2b("abc", length = 65))
#> Error : `length` must be a single integer between 1 and 64