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] f8 91 cb 77 d2 d6 9f 81 46 47 16 e7 79 7b f2 8c c7 2b 02 58 38 37 0d 71 1f
#> [26] b1 bd 9a b7 55 88 ce
sha256(msg)
#>  [1] 52 00 e2 ab 6d cc c4 75 7c eb 6e ff da bc 89 f8 a5 bb 6d c7 2d 7d 01 e4 53
#> [26] 66 d2 cf 6f 1f 33 ac
sha512(msg)
#>  [1] 30 8d cd 70 81 3d 8f e4 e2 34 46 a9 37 7b cd 91 54 a1 ae a8 13 36 66 b9 2b
#> [26] fd 87 5f 36 f6 06 84 4a e3 bb e8 cf 84 8a 6e 82 5a 29 42 69 49 c3 e2 68 fd
#> [51] ca ef 06 fc 57 94 f1 d8 3d 35 f9 c0 66 9b
scrypt(msg)
#>  [1] f8 ae fe 7b 7a 48 ae 6d 41 13 b7 d2 8e 96 04 a4 78 01 26 2a 86 b8 0b db 3b
#> [26] dc 4b b7 89 7c 0e 91

# 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] dc ee b9 ec ee 15 9c 73 44 b6 cc e7 2f 93 b1 01 ff 96 b6 03 60 35 69 05 7c
#> [26] 6c 51 dd 68 35 df 5c
shorthash(msg, shortkey)
#> [1] 77 c9 a5 66 48 dc da c1
sha256(msg, key = key)
#>  [1] f3 5e dd 26 b2 cc 86 bf cf 74 2c dd cd 24 ee fb 9b 00 35 d4 3d 79 ec a8 82
#> [26] 05 37 87 44 b2 75 58
sha512(msg, key = longkey)
#>  [1] 49 30 5e ff 68 42 3d da e3 ce 6c ab 19 92 5d ed e8 d3 4f ba dc 9a ab 36 c4
#> [26] 00 9a 53 95 15 1d 72 fe 20 7c 67 44 ba 71 7f fc e1 0b 3f 4a 31 d1 f9 ff a1
#> [51] db c4 83 35 2d 53 52 33 33 8f f1 d9 e9 14