Skip to contents

Functions to calculate cryptographic hash of a message, with optionally a key for HMAC applications. For storing passwords, use password_store instead.

Usage

hash(buf, key = NULL, size = 32)

scrypt(buf, salt = raw(32), size = 32)

argon2(buf, salt = raw(16), size = 32)

shorthash(buf, key)

sha512(buf, key = NULL)

sha256(buf, key = NULL)

Arguments

buf

data to be hashed

key

key for HMAC hashing. Optional, except for in shorthash.

size

length of the output hash. Must be between 16 and 64 (recommended is 32)

salt

non-confidential random data to seed the algorithm

Details

The generic hash function is recommended for most applications. It uses dynamic length BLAKE2b where output size can be any value between 16 bytes (128bit) and 64 bytes (512bit).

The scrypt hash function is designed to be CPU and memory expensive to protect against brute force attacks. This algorithm is also used by the password_store function.

The argon2 hash function is also designed to be CPU and memory expensive to protect against brute force attacks. Argon2 is a password-hashing function that summarizes the state of the art in the design of memory-hard functions

The shorthash function is a special 8 byte (64 bit) hash based on SipHash-2-4. The output of this function is only 64 bits (8 bytes). It is useful for in e.g. Hash tables, but it should not be considered collision-resistant.

Hash functions can be used for HMAC by specifying a secret key. They key size for shorthash is 16 bytes, for sha256 it is 32 bytes and for sha512 it is 64 bytes. For hash the key size can be any value between 16 and 62, recommended is at least 32.

Examples

# Basic hashing
msg <- serialize(iris, NULL)
hash(msg)
#>  [1] 9c 73 76 74 fe f0 d2 cc a5 c0 a3 45 ec 1a 5c c5 37 9e bf aa c0 2f 28 be 4f
#> [26] 5d 68 bb f5 eb 9c e4
sha256(msg)
#>  [1] 98 54 60 9b 5f d8 30 8f e9 27 81 20 a9 1f 53 43 4b 68 78 d3 d4 2d 5d fa 1e
#> [26] a1 30 f4 de 2b 87 8a
sha512(msg)
#>  [1] dd 95 e5 ef 42 69 9f c5 5e 98 ab c1 8e 8c a5 a8 36 72 19 c7 54 92 2e a1 96
#> [26] 2e df a3 d1 cf b4 5f 21 68 57 8d 5a 1c d7 6a c8 03 6b 07 56 e8 9b 6f 89 8b
#> [51] dc 7a ce 62 27 f9 74 ee 6c d0 79 f2 83 cd
scrypt(msg)
#>  [1] 89 0b c4 49 97 2c db aa aa aa 01 58 49 0a 57 1d bf 98 35 fe 56 b2 37 21 f3
#> [26] fd fc f0 6c bb f4 28

# Generate keys from passphrase
passphrase <- charToRaw("This is super secret")
key <- hash(passphrase)
shortkey <- hash(passphrase, size = 16)
longkey <- hash(passphrase, size = 64)

# HMAC (hashing with key)
hash(msg, key = key)
#>  [1] 8f c9 2a ac 15 0e 5e 83 b4 b0 f0 59 66 08 ef d9 fd ef 52 9b 20 3d b9 08 5e
#> [26] 8a 20 af 1d 79 84 f7
shorthash(msg, shortkey)
#> [1] a1 0d 6c d4 de a4 1a 0f
sha256(msg, key = key)
#>  [1] 0e a5 b4 6c 37 54 cd 7a e7 26 03 cb 64 c0 dc fc 69 b7 8c fe d6 23 76 14 50
#> [26] b8 f3 16 16 ac 50 c7
sha512(msg, key = longkey)
#>  [1] 94 0b 56 34 ad 9c 48 6d 03 0f 0b e0 2c f6 76 d4 76 91 82 d3 3f a0 3a 02 55
#> [26] 2a fc 42 4b 2b b7 dc 97 42 73 80 6a a3 ad 08 b5 70 bd cf 69 06 82 b7 41 02
#> [51] 26 81 76 3b b4 d6 d9 ff 90 f0 ab e4 33 24