SHA-2, SHA-3 and SHAKE with HMAC, KangarooTwelve, BLAKE2b and Argon2id, comparison in constant time, AES-SIV, AES-GCM-SIV and ChaCha20, and random bytes from the operating system.
Each section is one help page: what its functions are for, and the
example of the page as it runs. ?name gives the arguments
and the value in full.
nqhash(), nqhmac(): nqhash()
computes a SHA-2 (FIPS 180-4), SHA-3 or SHAKE (FIPS 202) digest of a raw
vector, a string or a file; nqhmac() computes an HMAC (RFC
2104) with a SHA-2 or SHA-3 hash.
nqhash("abc", hex = TRUE)
#> [1] "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
nqhash(charToRaw("abc"), "sha3-256")
#> [1] 3a 98 5d a7 4f e2 25 b2 04 5c 17 2d 6b d3 90 bd 85 5f 08 6e 3e 9d 52 5b 46 bf e2 45
#> [29] 11 43 15 32
nqhash("abc", "shake128", len = 16)
#> [1] 58 81 09 2d d8 18 bf 5c f8 a3 dd b7 93 fb cb a7
nqhash("abc", "sha512-256", hex = TRUE)
#> [1] "53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23"
f <- tempfile()
writeBin(charToRaw("abc"), f)
identical(nqhash(f, file = TRUE), nqhash("abc"))
#> [1] TRUE
unlink(f)
nqhmac("key", "The quick brown fox jumps over the lazy dog", hex = TRUE)
#> [1] "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"
nqhmac(as.raw(1:64), "data", "sha3-256", hex = TRUE)
#> [1] "df498140572047752f9f5ec49a2c4017bf7f7b18cdc5831df836b5f7f87fb326"nqblake2b(), nqargon2id(),
nqcteq(): nqblake2b() is BLAKE2b (RFC 7693):
an n-byte digest of x, keyed (a MAC) with up
to 64 bytes of key. nqargon2id() is Argon2id
(RFC 9106, version 0x13), the memory-hard password hash: n
bytes from the password and the salt, t passes over
m KiB of memory in p lanes, with an optional
secret key and associated data. nqcteq() compares two raw
vectors in time that depends only on their length; compare MACs and tags
with it.
nqblake2b("abc")
#> [1] ba 80 a5 3f 98 1c 4d 0d 6a 27 97 b6 9f 12 f6 e9 4c 21 2f 14 68 5a c4 b7 4b 12 bb 6f
#> [29] db ff a2 d1 7d 87 c5 39 2a ab 79 2d c2 52 d5 de 45 33 cc 95 18 d3 8a a8 db f1 92 5a
#> [57] b9 23 86 ed d4 00 99 23
nqblake2b("abc", key = as.raw(1:32), n = 32)
#> [1] 28 07 76 13 5e dc d5 61 b5 5e db 7a be c9 85 33 3c 39 95 dd a3 d4 93 de 3d 29 ee 5b
#> [29] c4 5f b3 33
nqargon2id("password", as.raw(1:16), t = 1, m = 8, p = 1)
#> [1] 0b 67 e7 20 bc dc 4f ea 3d 23 47 b3 73 55 1a a3 75 92 c6 37 79 a1 b5 70 3f ab 5b 9b
#> [29] 8a 59 4b 02
nqcteq(nqblake2b("a"), nqblake2b("a"))
#> [1] TRUEnqkt(), nqsivenc(),
nqsivdec(), nqgsivenc(),
nqgsivdec(), nqchacha20(): nqkt()
is KangarooTwelve (RFC 9861): n bytes of KT128
(bits = 128) or KT256 (bits = 256), the tree
hash and extendable-output function on Keccak with 12 rounds, with an
optional customization string (a secret key there makes it a MAC). A
shorter output is the start of a longer one.
nqkt("abc")
#> [1] ab 17 4f 32 8c 55 a5 51 0b 0b 20 97 91 bf 8b 60 e8 01 a7 cf c2 aa 42 04 2d cb 8f 54
#> [29] 7f be 3a 7d
nqkt("abc", n = 64, bits = 256, custom = "my application")
#> [1] a8 5c 05 c0 0c 57 62 4d 3f e9 d5 c7 90 cb 86 4e 0f 1d 23 01 63 41 0c 2f d9 08 6b ce
#> [29] 54 98 ef 9c 77 94 36 31 dd e0 74 2b 17 52 23 f6 64 e9 77 73 16 16 b2 8d d0 e9 36 99
#> [57] ec 88 88 b8 18 e3 26 9c
key <- as.raw(1:64)
ct <- nqsivenc(key, "a message", ad = list("header"))
rawToChar(nqsivdec(key, ct, ad = list("header")))
#> [1] "a message"
nonce <- as.raw(1:12)
ct <- nqgsivenc(key[1:32], nonce, "a message", ad = "header")
rawToChar(nqgsivdec(key[1:32], nonce, ct, ad = "header"))
#> [1] "a message"
ks <- nqchacha20(key[1:32], nonce, "not authenticated")
rawToChar(nqchacha20(key[1:32], nonce, ks))
#> [1] "not authenticated"nqrand(): Bytes from the operating system’s
cryptographically secure generator, the same source key generation,
hedged signing and encapsulation use (getrandom() on Linux,
arc4random_buf() on macOS and the BSDs,
BCryptGenRandom() on Windows), read through the library’s
nqrand(). Independent of set.seed() and of R’s
own generator; suitable for keys, nonces and salts.