Usage

First, load the package:

using CryptographicHashFunctions

High-Level

Working with the high-level interface is quite easy:

bytes2hex(sha3_224("Hash me!"))
"dd49934acc7a09dbdaebe02d0e53443a57dd0f1cfb7f458a05e7f0f5"

Or, to compute the file hash (of an empty file named filename):

bytes2hex(sha256(open(filename)))
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

Calculating HMAC's is similarly straightforward:

bytes2hex(hmac_sha256("some key", "some data"))
"92003059a722e7632fc06d79b2c682849aa17195b617580464d048e12242c844"

The backend used to do the hashing can be specified with the keyword argument provider:

sha256(""; provider = CryptographicHashFunctions.OpenSSL) ==
sha256(""; provider = CryptographicHashFunctions.Libgcrypt)
true

Extracting a fixed number of bytes from an extendable-output function can be done either in one go

bytes2hex(shake256("Hash and extract me!", 42))
"3004d4bd9c5a815b3423492bde4635875b5552f2f60708cb44a4595aae1ce93d409311c173579c5e8fe8"

or step by step:

xof = shake256("Hash and extract me!")
bytes2hex([digest!(xof, 10); digest!(xof, 10); digest!(xof, 22)])
"3004d4bd9c5a815b3423492bde4635875b5552f2f60708cb44a4595aae1ce93d409311c173579c5e8fe8"

Low-Level

The low-level interface allows for more controlled operations.

Setting up a hash context, feeding it with some data, and finally computing the digest works as follows:

ctx = context(SHA3_224)
update!(ctx, "Hash ")
update!(ctx, "me!")
bytes2hex(digest!(ctx))
"dd49934acc7a09dbdaebe02d0e53443a57dd0f1cfb7f458a05e7f0f5"

Or, in the case of an extendable-output function:

ctx = context(SHAKE256, "Hash and "; provider = CryptographicHashFunctions.Libgcrypt)
update!(ctx, IOBuffer("extract me!"); buffersize = 2)
[bytes2hex(digest!(ctx, 15)) for i ∈ 1:5]
5-element Vector{String}:
 "3004d4bd9c5a815b3423492bde4635"
 "875b5552f2f60708cb44a4595aae1c"
 "e93d409311c173579c5e8fe861895a"
 "ee20b2d50d4897a65fbd9ab998edb3"
 "0ed6d639d42a0c12ebb130134137d6"

To avoid unneccessary allocations, it's sometimes useful to be able to copy or reset hash contexts:

ctx = context(SHA3_224, "Hash me!")
ctx_copy = copy(ctx)
reset!(ctx)
[bytes2hex(digest!(ctx)), bytes2hex(digest!(ctx_copy))]
2-element Vector{String}:
 "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7"
 "dd49934acc7a09dbdaebe02d0e53443a57dd0f1cfb7f458a05e7f0f5"

Dealing with HMAC objects works similarly:

hmac = HMAC(SHA256, "some key")
update!(hmac, "some data")
bytes2hex(digest!(hmac))
"92003059a722e7632fc06d79b2c682849aa17195b617580464d048e12242c844"