SHA

SHA functions

Usage is very straightforward:

  1. julia> using SHA
  2. julia> bytes2hex(sha256("test"))
  3. "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 an AbstractVector{UInt8}, an AbstractString or an IO object. This makes it trivial to checksum a file:

  1. shell> cat /tmp/test.txt
  2. test
  3. julia> using SHA
  4. julia> open("/tmp/test.txt") do f
  5. sha2_256(f)
  6. end
  7. 32-element Array{UInt8,1}:
  8. 0x9f
  9. 0x86
  10. 0xd0
  11. 0x81
  12. 0x88
  13. 0x4c
  14. 0x7d
  15. 0x65
  16. 0x5d
  17. 0x6c
  18. 0x15
  19. 0xb0
  20. 0xf0
  21. 0x0a
  22. 0x08

All SHA functions

Due to the colloquial usage of sha256 to refer to sha2_256, convenience functions are provided, mapping shaxxx() function calls to sha2_xxx(). For SHA-3, no such colloquialisms exist and the user must use the full sha3_xxx() names.

shaxxx() takes AbstractString and array-like objects (NTuple and Array) with elements of type UInt8.

SHA-1

SHA.sha1 — Function

  1. sha1(data)

Hash data using the sha1 algorithm and return the resulting digest. See also SHA1_CTX.

  1. sha1(io::IO)

Hash data from io using sha1 algorithm.

SHA-2

SHA.sha224 — Function

  1. sha224(data)

Hash data using the sha224 algorithm and return the resulting digest. See also SHA2_224_CTX.

  1. sha224(io::IO)

Hash data from io using sha224 algorithm.

SHA.sha256 — Function

  1. sha256(data)

Hash data using the sha256 algorithm and return the resulting digest. See also SHA2_256_CTX.

  1. sha256(io::IO)

Hash data from io using sha256 algorithm.

SHA.sha384 — Function

  1. sha384(data)

Hash data using the sha384 algorithm and return the resulting digest. See also SHA2_384_CTX.

  1. sha384(io::IO)

Hash data from io using sha384 algorithm.

SHA.sha512 — Function

  1. sha512(data)

Hash data using the sha512 algorithm and return the resulting digest. See also SHA2_512_CTX.

  1. sha512(io::IO)

Hash data from io using sha512 algorithm.

SHA.sha2_224 — Function

  1. sha2_224(data)

Hash data using the sha2_224 algorithm and return the resulting digest. See also SHA2_224_CTX.

  1. sha2_224(io::IO)

Hash data from io using sha2_224 algorithm.

SHA.sha2_256 — Function

  1. sha2_256(data)

Hash data using the sha2_256 algorithm and return the resulting digest. See also SHA2_256_CTX.

  1. sha2_256(io::IO)

Hash data from io using sha2_256 algorithm.

SHA.sha2_384 — Function

  1. sha2_384(data)

Hash data using the sha2_384 algorithm and return the resulting digest. See also SHA2_384_CTX.

  1. sha2_384(io::IO)

Hash data from io using sha2_384 algorithm.

SHA.sha2_512 — Function

  1. sha2_512(data)

Hash data using the sha2_512 algorithm and return the resulting digest. See also SHA2_512_CTX.

  1. sha2_512(io::IO)

Hash data from io using sha2_512 algorithm.

SHA-3

SHA.sha3_224 — Function

  1. sha3_224(data)

Hash data using the sha3_224 algorithm and return the resulting digest. See also SHA3_224_CTX.

  1. sha3_224(io::IO)

Hash data from io using sha3_224 algorithm.

SHA.sha3_256 — Function

  1. sha3_256(data)

Hash data using the sha3_256 algorithm and return the resulting digest. See also SHA3_256_CTX.

  1. sha3_256(io::IO)

Hash data from io using sha3_256 algorithm.

SHA.sha3_384 — Function

  1. sha3_384(data)

Hash data using the sha3_384 algorithm and return the resulting digest. See also SHA3_384_CTX.

  1. sha3_384(io::IO)

Hash data from io using sha3_384 algorithm.

SHA.sha3_512 — Function

  1. sha3_512(data)

Hash data using the sha3_512 algorithm and return the resulting digest. See also SHA3_512_CTX.

  1. sha3_512(io::IO)

Hash data from io using sha3_512 algorithm.

Working with context

To create a hash from multiple items the SHAX_XXX_CTX() types can be used to create a stateful hash object that is updated with update! and finalized with digest!

  1. julia> using SHA
  2. julia> ctx = SHA2_256_CTX()
  3. SHA2 256-bit hash state
  4. julia> update!(ctx, b"some data")
  5. 0x0000000000000009
  6. julia> update!(ctx, b"some more data")
  7. 0x0000000000000017
  8. julia> digest!(ctx)
  9. 32-element Vector{UInt8}:
  10. 0xbe
  11. 0xcf
  12. 0x23
  13. 0xda
  14. 0xaf
  15. 0x02
  16. 0xf7
  17. 0xa3
  18. 0x57
  19. 0x92
  20. 0x89
  21. 0x4f
  22. 0x59
  23. 0xd8
  24. 0xb3
  25. 0xb4
  26. 0x81
  27. 0x8b
  28. 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! — Function

  1. update!(context, data[, datalen])

Update the SHA context with the bytes in data. See also digest! for finalizing the hash.

Examples

  1. julia> ctx = SHA1_CTX()
  2. SHA1 hash state
  3. julia> update!(ctx, b"data to to be hashed")

SHA.digest! — Function

  1. digest!(context)

Finalize the SHA context and return the hash as array of bytes (Array{Uint8, 1}).

Examples

  1. julia> ctx = SHA1_CTX()
  2. SHA1 hash state
  3. julia> update!(ctx, b"data to to be hashed")
  4. julia> digest!(ctx)
  5. 20-element Array{UInt8,1}:
  6. 0x83
  7. 0xe4
  8. 0x89
  9. 0xf5

All SHA context types

SHA-1

SHA.SHA1_CTX — Type

  1. SHA1_CTX()

Construct an empty SHA1 context.

SHA-2

Convenience types are also provided, where SHAXXX_CTX is a type alias for SHA2_XXX_CTX.

SHA.SHA224_CTX — Type

  1. SHA2_224_CTX()

Construct an empty SHA2_224 context.

SHA.SHA256_CTX — Type

  1. SHA2_256_CTX()

Construct an empty SHA2_256 context.

SHA.SHA384_CTX — Type

  1. SHA2_384()

Construct an empty SHA2_384 context.

SHA.SHA512_CTX — Type

  1. SHA2_512_CTX()

Construct an empty SHA2_512 context.

SHA.SHA2_224_CTX — Type

  1. SHA2_224_CTX()

Construct an empty SHA2_224 context.

SHA.SHA2_256_CTX — Type

  1. SHA2_256_CTX()

Construct an empty SHA2_256 context.

SHA.SHA2_384_CTX — Type

  1. SHA2_384()

Construct an empty SHA2_384 context.

SHA.SHA2_512_CTX — Type

  1. SHA2_512_CTX()

Construct an empty SHA2_512 context.

SHA-3

SHA.SHA3_224_CTX — Type

  1. SHA3_224_CTX()

Construct an empty SHA3_224 context.

SHA.SHA3_256_CTX — Type

  1. SHA3_256_CTX()

Construct an empty SHA3_256 context.

SHA.SHA3_384_CTX — Type

  1. SHA3_384_CTX()

Construct an empty SHA3_384 context.

SHA.SHA3_512_CTX — Type

  1. SHA3_512_CTX()

Construct an empty SHA3_512 context.

HMAC functions

  1. julia> using SHA
  2. julia> key = collect(codeunits("key_string"))
  3. 10-element Vector{UInt8}:
  4. 0x6b
  5. 0x65
  6. 0x79
  7. 0x5f
  8. 0x73
  9. 0x74
  10. 0x72
  11. 0x69
  12. 0x6e
  13. 0x67
  14. julia> bytes2hex(hmac_sha3_256(key, "test-message"))
  15. "bc49a6f2aa29b27ee5ed1e944edd7f3d153e8a01535d98b5e24dac9a589a6248"

To create a hash from multiple items, the HMAC_CTX() types can be used to create a stateful hash object that is updated with update! and finalized with digest!.

  1. julia> using SHA
  2. julia> key = collect(codeunits("key_string"))
  3. 10-element Vector{UInt8}:
  4. 0x6b
  5. 0x65
  6. 0x79
  7. 0x5f
  8. 0x73
  9. 0x74
  10. 0x72
  11. 0x69
  12. 0x6e
  13. 0x67
  14. julia> ctx = HMAC_CTX(SHA3_256_CTX(), key);
  15. julia> update!(ctx, b"test-")
  16. 0x0000000000000000000000000000008d
  17. julia> update!(ctx, b"message")
  18. 0x00000000000000000000000000000094
  19. julia> bytes2hex(digest!(ctx))
  20. "bc49a6f2aa29b27ee5ed1e944edd7f3d153e8a01535d98b5e24dac9a589a6248"

All HMAC functions

HMAC context type

SHA.HMAC_CTX — Type

  1. HMAC_CTX(ctx::CTX, key::Vector{UInt8}) where {CTX<:SHA_CTX}

Construct an empty HMAC_CTX context.

SHA-1

SHA.hmac_sha1 — Function

  1. hmac_sha1(key, data)

Hash data using the sha1 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha1(key, io::IO)

Hash data from io with the passed key using sha1 algorithm.

SHA-2

SHA.hmac_sha224 — Function

  1. hmac_sha224(key, data)

Hash data using the sha224 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha224(key, io::IO)

Hash data from io with the passed key using sha224 algorithm.

SHA.hmac_sha256 — Function

  1. hmac_sha256(key, data)

Hash data using the sha256 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha256(key, io::IO)

Hash data from io with the passed key using sha256 algorithm.

SHA.hmac_sha384 — Function

  1. hmac_sha384(key, data)

Hash data using the sha384 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha384(key, io::IO)

Hash data from io with the passed key using sha384 algorithm.

SHA.hmac_sha512 — Function

  1. hmac_sha512(key, data)

Hash data using the sha512 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha512(key, io::IO)

Hash data from io with the passed key using sha512 algorithm.

SHA.hmac_sha2_224 — Function

  1. hmac_sha2_224(key, data)

Hash data using the sha2_224 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha2_224(key, io::IO)

Hash data from io with the passed key using sha2_224 algorithm.

SHA.hmac_sha2_256 — Function

  1. hmac_sha2_256(key, data)

Hash data using the sha2_256 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha2_256(key, io::IO)

Hash data from io with the passed key using sha2_256 algorithm.

SHA.hmac_sha2_384 — Function

  1. hmac_sha2_384(key, data)

Hash data using the sha2_384 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha2_384(key, io::IO)

Hash data from io with the passed key using sha2_384 algorithm.

SHA.hmac_sha2_512 — Function

  1. hmac_sha2_512(key, data)

Hash data using the sha2_512 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha2_512(key, io::IO)

Hash data from io with the passed key using sha2_512 algorithm.

SHA-3

SHA.hmac_sha3_224 — Function

  1. hmac_sha3_224(key, data)

Hash data using the sha3_224 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha3_224(key, io::IO)

Hash data from io with the passed key using sha3_224 algorithm.

SHA.hmac_sha3_256 — Function

  1. hmac_sha3_256(key, data)

Hash data using the sha3_256 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha3_256(key, io::IO)

Hash data from io with the passed key using sha3_256 algorithm.

SHA.hmac_sha3_384 — Function

  1. hmac_sha3_384(key, data)

Hash data using the sha3_384 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha3_384(key, io::IO)

Hash data from io with the passed key using sha3_384 algorithm.

SHA.hmac_sha3_512 — Function

  1. hmac_sha3_512(key, data)

Hash data using the sha3_512 algorithm using the passed key. See also HMAC_CTX.

  1. hmac_sha3_512(key, io::IO)

Hash data from io with the passed key using sha3_512 algorithm.