blake2s.odin 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 :: 32
  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) {
  23. cfg: _blake2.Blake2_Config
  24. cfg.size = _blake2.BLAKE2S_SIZE
  25. _blake2.init(ctx, &cfg)
  26. }
  27. // update adds more data to the Context.
  28. update :: proc(ctx: ^Context, data: []byte) {
  29. _blake2.update(ctx, data)
  30. }
  31. // final finalizes the Context, writes the digest to hash, and calls
  32. // reset on the Context.
  33. //
  34. // Iff finalize_clone is set, final will work on a copy of the Context,
  35. // which is useful for for calculating rolling digests.
  36. final :: proc(ctx: ^Context, hash: []byte, finalize_clone: bool = false) {
  37. _blake2.final(ctx, hash, finalize_clone)
  38. }
  39. // clone clones the Context other into ctx.
  40. clone :: proc(ctx, other: ^Context) {
  41. _blake2.clone(ctx, other)
  42. }
  43. // reset sanitizes the Context. The Context must be re-initialized to
  44. // be used again.
  45. reset :: proc(ctx: ^Context) {
  46. _blake2.reset(ctx)
  47. }