Usage is very straightforward:
julia> using SHAjulia> bytes2hex(sha256("test"))"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
Each exported function (at the time of this writing, SHA-1, SHA-2 224, 256, 384 and 512, and SHA-3 224, 256, 384 and 512 functions are implemented) takes in either anAbstractVector{UInt8}
, anAbstractString
or anIO
object. This makes it trivial to checksum a file:
shell> cat /tmp/test.txttestjulia> using SHAjulia> open("/tmp/test.txt") do f sha2_256(f) end32-element Array{UInt8,1}: 0x9f 0x86 0xd0 0x81 0x88 0x4c 0x7d 0x65 ⋮ 0x5d 0x6c 0x15 0xb0 0xf0 0x0a 0x08
Due to the colloquial usage ofsha256
to refer tosha2_256
, convenience functions are provided, mappingshaxxx()
function calls tosha2_xxx()
. For SHA-3, no such colloquialisms exist and the user must use the fullsha3_xxx()
names.
shaxxx()
takesAbstractString
and array-like objects (NTuple
andArray
) with elements of typeUInt8
.
SHA-1
SHA.sha1
—Functionsha1(data)
Hash data using thesha1
algorithm and return the resulting digest. See alsoSHA1_CTX
.
sha1(io::IO)
Hash data from io usingsha1
algorithm.
SHA-2
SHA.sha224
—Functionsha224(data)
Hash data using thesha224
algorithm and return the resulting digest. See alsoSHA2_224_CTX
.
sha224(io::IO)
Hash data from io usingsha224
algorithm.
SHA.sha256
—Functionsha256(data)
Hash data using thesha256
algorithm and return the resulting digest. See alsoSHA2_256_CTX
.
sha256(io::IO)
Hash data from io usingsha256
algorithm.
SHA.sha384
—Functionsha384(data)
Hash data using thesha384
algorithm and return the resulting digest. See alsoSHA2_384_CTX
.
sha384(io::IO)
Hash data from io usingsha384
algorithm.
SHA.sha512
—Functionsha512(data)
Hash data using thesha512
algorithm and return the resulting digest. See alsoSHA2_512_CTX
.
sha512(io::IO)
Hash data from io usingsha512
algorithm.
SHA.sha2_224
—Functionsha2_224(data)
Hash data using thesha2_224
algorithm and return the resulting digest. See alsoSHA2_224_CTX
.
sha2_224(io::IO)
Hash data from io usingsha2_224
algorithm.
SHA.sha2_256
—Functionsha2_256(data)
Hash data using thesha2_256
algorithm and return the resulting digest. See alsoSHA2_256_CTX
.
sha2_256(io::IO)
Hash data from io usingsha2_256
algorithm.
SHA.sha2_384
—Functionsha2_384(data)
Hash data using thesha2_384
algorithm and return the resulting digest. See alsoSHA2_384_CTX
.
sha2_384(io::IO)
Hash data from io usingsha2_384
algorithm.
SHA.sha2_512
—Functionsha2_512(data)
Hash data using thesha2_512
algorithm and return the resulting digest. See alsoSHA2_512_CTX
.
sha2_512(io::IO)
Hash data from io usingsha2_512
algorithm.
SHA-3
SHA.sha3_224
—Functionsha3_224(data)
Hash data using thesha3_224
algorithm and return the resulting digest. See alsoSHA3_224_CTX
.
sha3_224(io::IO)
Hash data from io usingsha3_224
algorithm.
SHA.sha3_256
—Functionsha3_256(data)
Hash data using thesha3_256
algorithm and return the resulting digest. See alsoSHA3_256_CTX
.
sha3_256(io::IO)
Hash data from io usingsha3_256
algorithm.
SHA.sha3_384
—Functionsha3_384(data)
Hash data using thesha3_384
algorithm and return the resulting digest. See alsoSHA3_384_CTX
.
sha3_384(io::IO)
Hash data from io usingsha3_384
algorithm.
SHA.sha3_512
—Functionsha3_512(data)
Hash data using thesha3_512
algorithm and return the resulting digest. See alsoSHA3_512_CTX
.
sha3_512(io::IO)
Hash data from io usingsha3_512
algorithm.
To create a hash from multiple items theSHAX_XXX_CTX()
types can be used to create a stateful hash object that is updated withupdate!
and finalized withdigest!
julia> using SHAjulia> ctx = SHA2_256_CTX()SHA2 256-bit hash statejulia> update!(ctx, b"some data")0x0000000000000009julia> update!(ctx, b"some more data")0x0000000000000017julia> digest!(ctx)32-element Vector{UInt8}: 0xbe 0xcf 0x23 0xda 0xaf 0x02 0xf7 0xa3 0x57 0x92 ⋮ 0x89 0x4f 0x59 0xd8 0xb3 0xb4 0x81 0x8b 0xc5
Note that, at the time of this writing, the SHA3 code is not optimized, and as such is roughly an order of magnitude slower than SHA2.
SHA.update!
—Functionupdate!(context, data[, datalen])
Update the SHA context with the bytes in data. See alsodigest!
for finalizing the hash.
Examples
julia> ctx = SHA1_CTX()SHA1 hash statejulia> update!(ctx, b"data to to be hashed")
SHA.digest!
—Functiondigest!(context)
Finalize the SHA context and return the hash as array of bytes (Array{Uint8, 1}). Updating the context after callingdigest!
on it will error.
Examples
julia> ctx = SHA1_CTX()SHA1 hash statejulia> update!(ctx, b"data to to be hashed")julia> digest!(ctx)20-element Array{UInt8,1}: 0x83 0xe4 ⋮ 0x89 0xf5julia> update!(ctx, b"more data")ERROR: Cannot update CTX after `digest!` has been called on it[...]
SHA-1
SHA.SHA1_CTX
—TypeSHA1_CTX()
Construct an empty SHA1 context.
SHA-2
Convenience types are also provided, whereSHAXXX_CTX
is a type alias forSHA2_XXX_CTX
.
SHA.SHA224_CTX
—TypeSHA2_224_CTX()
Construct an empty SHA2_224 context.
SHA.SHA256_CTX
—TypeSHA2_256_CTX()
Construct an empty SHA2_256 context.
SHA.SHA384_CTX
—TypeSHA2_384()
Construct an empty SHA2_384 context.
SHA.SHA512_CTX
—TypeSHA2_512_CTX()
Construct an empty SHA2_512 context.
SHA.SHA2_224_CTX
—TypeSHA2_224_CTX()
Construct an empty SHA2_224 context.
SHA.SHA2_256_CTX
—TypeSHA2_256_CTX()
Construct an empty SHA2_256 context.
SHA.SHA2_384_CTX
—TypeSHA2_384()
Construct an empty SHA2_384 context.
SHA.SHA2_512_CTX
—TypeSHA2_512_CTX()
Construct an empty SHA2_512 context.
SHA-3
SHA.SHA3_224_CTX
—TypeSHA3_224_CTX()
Construct an empty SHA3_224 context.
SHA.SHA3_256_CTX
—TypeSHA3_256_CTX()
Construct an empty SHA3_256 context.
SHA.SHA3_384_CTX
—TypeSHA3_384_CTX()
Construct an empty SHA3_384 context.
SHA.SHA3_512_CTX
—TypeSHA3_512_CTX()
Construct an empty SHA3_512 context.
julia> using SHAjulia> key = collect(codeunits("key_string"))10-element Vector{UInt8}: 0x6b 0x65 0x79 0x5f 0x73 0x74 0x72 0x69 0x6e 0x67julia> bytes2hex(hmac_sha3_256(key, "test-message"))"bc49a6f2aa29b27ee5ed1e944edd7f3d153e8a01535d98b5e24dac9a589a6248"
To create a hash from multiple items, theHMAC_CTX()
types can be used to create a stateful hash object that is updated withupdate!
and finalized withdigest!
.
julia> using SHAjulia> key = collect(codeunits("key_string"))10-element Vector{UInt8}: 0x6b 0x65 0x79 0x5f 0x73 0x74 0x72 0x69 0x6e 0x67julia> ctx = HMAC_CTX(SHA3_256_CTX(), key);julia> update!(ctx, b"test-")0x0000000000000000000000000000008djulia> update!(ctx, b"message")0x00000000000000000000000000000094julia> bytes2hex(digest!(ctx))"bc49a6f2aa29b27ee5ed1e944edd7f3d153e8a01535d98b5e24dac9a589a6248"
HMAC context type
SHA.HMAC_CTX
—TypeHMAC_CTX(ctx::CTX, key::Vector{UInt8}) where {CTX<:SHA_CTX}
Construct an empty HMAC_CTX context.
SHA-1
SHA.hmac_sha1
—Functionhmac_sha1(key, data)
Hash data using thesha1
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha1(key, io::IO)
Hash data fromio
with the passed key usingsha1
algorithm.
SHA-2
SHA.hmac_sha224
—Functionhmac_sha224(key, data)
Hash data using thesha224
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha224(key, io::IO)
Hash data fromio
with the passed key usingsha224
algorithm.
SHA.hmac_sha256
—Functionhmac_sha256(key, data)
Hash data using thesha256
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha256(key, io::IO)
Hash data fromio
with the passed key usingsha256
algorithm.
SHA.hmac_sha384
—Functionhmac_sha384(key, data)
Hash data using thesha384
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha384(key, io::IO)
Hash data fromio
with the passed key usingsha384
algorithm.
SHA.hmac_sha512
—Functionhmac_sha512(key, data)
Hash data using thesha512
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha512(key, io::IO)
Hash data fromio
with the passed key usingsha512
algorithm.
SHA.hmac_sha2_224
—Functionhmac_sha2_224(key, data)
Hash data using thesha2_224
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha2_224(key, io::IO)
Hash data fromio
with the passed key usingsha2_224
algorithm.
SHA.hmac_sha2_256
—Functionhmac_sha2_256(key, data)
Hash data using thesha2_256
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha2_256(key, io::IO)
Hash data fromio
with the passed key usingsha2_256
algorithm.
SHA.hmac_sha2_384
—Functionhmac_sha2_384(key, data)
Hash data using thesha2_384
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha2_384(key, io::IO)
Hash data fromio
with the passed key usingsha2_384
algorithm.
SHA.hmac_sha2_512
—Functionhmac_sha2_512(key, data)
Hash data using thesha2_512
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha2_512(key, io::IO)
Hash data fromio
with the passed key usingsha2_512
algorithm.
SHA-3
SHA.hmac_sha3_224
—Functionhmac_sha3_224(key, data)
Hash data using thesha3_224
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha3_224(key, io::IO)
Hash data fromio
with the passed key usingsha3_224
algorithm.
SHA.hmac_sha3_256
—Functionhmac_sha3_256(key, data)
Hash data using thesha3_256
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha3_256(key, io::IO)
Hash data fromio
with the passed key usingsha3_256
algorithm.
SHA.hmac_sha3_384
—Functionhmac_sha3_384(key, data)
Hash data using thesha3_384
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha3_384(key, io::IO)
Hash data fromio
with the passed key usingsha3_384
algorithm.
SHA.hmac_sha3_512
—Functionhmac_sha3_512(key, data)
Hash data using thesha3_512
algorithm using the passed key. See alsoHMAC_CTX
.
hmac_sha3_512(key, io::IO)
Hash data fromio
with the passed key usingsha3_512
algorithm.
Settings
This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.