blake2s.odin 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package blake2s
  2. /*
  3. Copyright 2021 zhibog
  4. Made available under the BSD-3 license.
  5. List of contributors:
  6. zhibog, dotbmp: Initial implementation.
  7. Interface for the BLAKE2S hashing algorithm.
  8. BLAKE2B and BLAKE2B share the implementation in the _blake2 package.
  9. */
  10. import "core:os"
  11. import "core:io"
  12. import "../_blake2"
  13. /*
  14. High level API
  15. */
  16. DIGEST_SIZE :: 32
  17. // hash_string will hash the given input and return the
  18. // computed hash
  19. hash_string :: proc(data: string) -> [DIGEST_SIZE]byte {
  20. return hash_bytes(transmute([]byte)(data))
  21. }
  22. // hash_bytes will hash the given input and return the
  23. // computed hash
  24. hash_bytes :: proc(data: []byte) -> [DIGEST_SIZE]byte {
  25. hash: [DIGEST_SIZE]byte
  26. ctx: _blake2.Blake2s_Context
  27. cfg: _blake2.Blake2_Config
  28. cfg.size = _blake2.BLAKE2S_SIZE
  29. ctx.cfg = cfg
  30. _blake2.init(&ctx)
  31. _blake2.update(&ctx, data)
  32. _blake2.final(&ctx, hash[:])
  33. return hash
  34. }
  35. // hash_string_to_buffer will hash the given input and assign the
  36. // computed hash to the second parameter.
  37. // It requires that the destination buffer is at least as big as the digest size
  38. hash_string_to_buffer :: proc(data: string, hash: []byte) {
  39. hash_bytes_to_buffer(transmute([]byte)(data), hash);
  40. }
  41. // hash_bytes_to_buffer will hash the given input and write the
  42. // computed hash into the second parameter.
  43. // It requires that the destination buffer is at least as big as the digest size
  44. hash_bytes_to_buffer :: proc(data, hash: []byte) {
  45. assert(len(hash) >= DIGEST_SIZE, "Size of destination buffer is smaller than the digest size")
  46. ctx: _blake2.Blake2s_Context
  47. cfg: _blake2.Blake2_Config
  48. cfg.size = _blake2.BLAKE2S_SIZE
  49. ctx.cfg = cfg
  50. _blake2.init(&ctx)
  51. _blake2.update(&ctx, data)
  52. _blake2.final(&ctx, hash)
  53. }
  54. // hash_stream will read the stream in chunks and compute a
  55. // hash from its contents
  56. hash_stream :: proc(s: io.Stream) -> ([DIGEST_SIZE]byte, bool) {
  57. hash: [DIGEST_SIZE]byte
  58. ctx: _blake2.Blake2s_Context
  59. cfg: _blake2.Blake2_Config
  60. cfg.size = _blake2.BLAKE2S_SIZE
  61. ctx.cfg = cfg
  62. _blake2.init(&ctx)
  63. buf := make([]byte, 512)
  64. defer delete(buf)
  65. read := 1
  66. for read > 0 {
  67. read, _ = s->impl_read(buf)
  68. if read > 0 {
  69. _blake2.update(&ctx, buf[:read])
  70. }
  71. }
  72. _blake2.final(&ctx, hash[:])
  73. return hash, true
  74. }
  75. // hash_file will read the file provided by the given handle
  76. // and compute a hash
  77. hash_file :: proc(hd: os.Handle, load_at_once := false) -> ([DIGEST_SIZE]byte, bool) {
  78. if !load_at_once {
  79. return hash_stream(os.stream_from_handle(hd))
  80. } else {
  81. if buf, ok := os.read_entire_file(hd); ok {
  82. return hash_bytes(buf[:]), ok
  83. }
  84. }
  85. return [DIGEST_SIZE]byte{}, false
  86. }
  87. hash :: proc {
  88. hash_stream,
  89. hash_file,
  90. hash_bytes,
  91. hash_string,
  92. hash_bytes_to_buffer,
  93. hash_string_to_buffer,
  94. }
  95. /*
  96. Low level API
  97. */
  98. Blake2s_Context :: _blake2.Blake2b_Context
  99. init :: proc(ctx: ^_blake2.Blake2s_Context) {
  100. _blake2.init(ctx)
  101. }
  102. update :: proc "contextless" (ctx: ^_blake2.Blake2s_Context, data: []byte) {
  103. _blake2.update(ctx, data)
  104. }
  105. final :: proc "contextless" (ctx: ^_blake2.Blake2s_Context, hash: []byte) {
  106. _blake2.final(ctx, hash)
  107. }