core/crypto/hash
crypto_hash
Types
2Algorithm
Algorithm :: enum int {
Invalid = 0,
BLAKE2B = 1,
BLAKE2S = 2,
SHA224 = 3,
SHA256 = 4,
SHA384 = 5,
SHA512 = 6,
SHA512_256 = 7,
SHA3_224 = 8,
SHA3_256 = 9,
SHA3_384 = 10,
SHA3_512 = 11,
SM3 = 12,
Legacy_KECCAK_224 = 13,
Legacy_KECCAK_256 = 14,
Legacy_KECCAK_384 = 15,
Legacy_KECCAK_512 = 16,
Insecure_MD5 = 17,
Insecure_SHA1 = 18,
}SourceAlgorithm is the algorithm identifier associated with a given Context.
Context
Context :: struct {
_algo: Algorithm,
_impl: union {
blake2b.Context,
blake2s.Context,
sha2.Context_256,
sha2.Context_512,
sha3.Context,
sm3.Context,
keccak.Context,
md5.Context,
sha1.Context,
},
}SourceContext is a concrete instantiation of a specific hash algorithm.
Constants
2MAX_BLOCK_SIZE
MAX_BLOCK_SIZE :: BLOCK_SIZE_224SourceMAX_BLOCK_SIZE is the maximum block size used by any of Algorithms supported by this package.
MAX_DIGEST_SIZE
MAX_DIGEST_SIZE :: 64SourceMAX_DIGEST_SIZE is the maximum size digest that can be returned by any of the Algorithms supported via this package.
Variables
3ALGORITHM_NAMES
ALGORITHM_NAMES :: [19]string = [Algorithm]string {
.Invalid = "Invalid",
.BLAKE2B = "BLAKE2b",
.BLAKE2S = "BLAKE2s",
.SHA224 = "SHA-224",
.SHA256 = "SHA-256",
.SHA384 = "SHA-384",
.SHA512 = "SHA-512",
.SHASourceALGORITHM_NAMES is the Algorithm to algorithm name string.
BLOCK_SIZES
BLOCK_SIZES :: [19]int = [Algorithm]int {
.Invalid = 0,
.BLAKE2B = blake2b.BLOCK_SIZE,
.BLAKE2S = blake2s.BLOCK_SIZE,
.SHA224 = sha2.BLOCK_SIZE_256,
.SHA256 = sha2.BLOCK_SIZE_256,
.SHA384 = sha2.BLOCK_SIZE_512,
SourceBLOCK_SIZES is the Algoritm to block size in bytes.
DIGEST_SIZES
DIGEST_SIZES :: [19]int = [Algorithm]int {
.Invalid = 0,
.BLAKE2B = blake2b.DIGEST_SIZE,
.BLAKE2S = blake2s.DIGEST_SIZE,
.SHA224 = sha2.DIGEST_SIZE_224,
.SHA256 = sha2.DIGEST_SIZE_256,
.SHA384 = sha2.DIGEST_SIZE_3SourceDIGEST_SIZES is the Algorithm to digest size in bytes.
Procedures
15algorithm
algorithm :: proc(ctx: ^Context) -> (Algorithm)Sourcealgorithm returns the Algorithm used by a Context instance.
block_size
block_size :: proc(ctx: ^Context) -> (int)Sourceblock_size returns the block size of a Context instance in bytes.
clone
clone :: proc(ctx: ^Context, other: ^Context)Sourceclone clones the Context other into ctx.
digest_size
digest_size :: proc(ctx: ^Context) -> (int)Sourcedigest_size returns the digest size of a Context instance in bytes.
final
final :: proc(ctx: ^Context, hash: []u8, finalize_clone: bool)Sourcefinal finalizes the Context, writes the digest to hash, and calls reset on the Context.
If and only if (⟺) finalize_clone is set, final will work on a copy of the Context, which is useful for for calculating rolling digests.
hash_bytes
hash_bytes :: proc(algorithm: Algorithm, data: []u8, allocator: mem.Allocator = context.allocator) -> ([]u8)Sourcehash_bytes will hash the given input and return the computed digest in a newly allocated slice.
hash_bytes_to_buffer
hash_bytes_to_buffer :: proc(algorithm: Algorithm, data: []u8, hash: []u8) -> ([]u8)Sourcehash_bytes_to_buffer will hash the given input and write the computed digest into the third parameter. It requires that the destination buffer is at least as big as the digest size. The provided destination buffer is returned to match the behavior of hash_bytes.
hash_file_by_handle
hash_file_by_handle :: proc(algorithm: Algorithm, handle: ^os.File, load_at_once: untyped boolean = false, allocator: mem.Allocator = context.allocator) -> ([]u8, io.Error)Sourcehash_file will read the file provided by the given handle and return the computed digest in a newly allocated slice.
hash_file_by_name
hash_file_by_name :: proc(algorithm: Algorithm, filename: string, load_at_once: untyped boolean = false, allocator: mem.Allocator = context.allocator) -> ([]u8, io.Error)Sourcehash_stream
hash_stream :: proc(algorithm: Algorithm, s: io.Stream, allocator: mem.Allocator = context.allocator) -> ([]u8, io.Error)Sourcehash_stream will incrementally fully consume a stream, and return the computed digest in a newly allocated slice.
hash_string
hash_string :: proc(algorithm: Algorithm, data: string, allocator: mem.Allocator = context.allocator) -> ([]u8)Sourcehash_bytes will hash the given input and return the computed digest in a newly allocated slice.
hash_string_to_buffer
hash_string_to_buffer :: proc(algorithm: Algorithm, data: string, hash: []u8) -> ([]u8)Sourcehash_string_to_buffer will hash the given input and assign the computed digest to the third parameter. It requires that the destination buffer is at least as big as the digest size. The provided destination buffer is returned to match the behavior of hash_string.
init
init :: proc(ctx: ^Context, algorithm: Algorithm)Sourceinit initializes a Context with a specific hash Algorithm.
reset
reset :: proc(ctx: ^Context)Sourcereset sanitizes the Context. The Context must be re-initialized to be used again.
update
update :: proc(ctx: ^Context, data: []u8)Sourceupdate adds more data to the Context.