blake2s.odin 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. package blake2s implements the BLAKE2s hash algorithm.
  3. See:
  4. - [[ https://datatracker.ietf.org/doc/html/rfc7693 ]]
  5. - [[ https://www.blake2.net/ ]]
  6. */
  7. package blake2s
  8. /*
  9. Copyright 2021 zhibog
  10. Made available under the BSD-3 license.
  11. List of contributors:
  12. zhibog, dotbmp: Initial implementation.
  13. */
  14. import "../_blake2"
  15. // DIGEST_SIZE is the BLAKE2s digest size in bytes.
  16. DIGEST_SIZE :: _blake2.BLAKE2S_SIZE
  17. // BLOCK_SIZE is the BLAKE2s block size in bytes.
  18. BLOCK_SIZE :: _blake2.BLAKE2S_BLOCK_SIZE
  19. // Context is a BLAKE2s instance.
  20. Context :: _blake2.Blake2s_Context
  21. // init initializes a Context with the default BLAKE2s config.
  22. init :: proc(ctx: ^Context, digest_size := DIGEST_SIZE) {
  23. ensure(digest_size <= _blake2.MAX_SIZE, "crypto/blake2s: invalid digest size")
  24. cfg: _blake2.Blake2_Config
  25. cfg.size = u8(digest_size)
  26. _blake2.init(ctx, &cfg)
  27. }
  28. // update adds more data to the Context.
  29. update :: proc(ctx: ^Context, data: []byte) {
  30. _blake2.update(ctx, data)
  31. }
  32. // final finalizes the Context, writes the digest to hash, and calls
  33. // reset on the Context.
  34. //
  35. // Iff finalize_clone is set, final will work on a copy of the Context,
  36. // which is useful for for calculating rolling digests.
  37. final :: proc(ctx: ^Context, hash: []byte, finalize_clone: bool = false) {
  38. _blake2.final(ctx, hash, finalize_clone)
  39. }
  40. // clone clones the Context other into ctx.
  41. clone :: proc(ctx, other: ^Context) {
  42. _blake2.clone(ctx, other)
  43. }
  44. // reset sanitizes the Context. The Context must be re-initialized to
  45. // be used again.
  46. reset :: proc(ctx: ^Context) {
  47. _blake2.reset(ctx)
  48. }