blake2s.odin 2.7 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. BLAKE2s and BLAKE2b share the implementation in the _blake2 package.
  9. */
  10. import "core:io"
  11. import "core:os"
  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: Context
  27. cfg: _blake2.Blake2_Config
  28. cfg.size = _blake2.BLAKE2S_SIZE
  29. ctx.cfg = cfg
  30. init(&ctx)
  31. update(&ctx, data)
  32. 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. ctx: Context
  46. cfg: _blake2.Blake2_Config
  47. cfg.size = _blake2.BLAKE2S_SIZE
  48. ctx.cfg = cfg
  49. init(&ctx)
  50. update(&ctx, data)
  51. final(&ctx, hash)
  52. }
  53. // hash_stream will read the stream in chunks and compute a
  54. // hash from its contents
  55. hash_stream :: proc(s: io.Stream) -> ([DIGEST_SIZE]byte, bool) {
  56. hash: [DIGEST_SIZE]byte
  57. ctx: Context
  58. cfg: _blake2.Blake2_Config
  59. cfg.size = _blake2.BLAKE2S_SIZE
  60. ctx.cfg = cfg
  61. init(&ctx)
  62. buf := make([]byte, 512)
  63. defer delete(buf)
  64. read := 1
  65. for read > 0 {
  66. read, _ = io.read(s, buf)
  67. if read > 0 {
  68. update(&ctx, buf[:read])
  69. }
  70. }
  71. final(&ctx, hash[:])
  72. return hash, true
  73. }
  74. // hash_file will read the file provided by the given handle
  75. // and compute a hash
  76. hash_file :: proc(hd: os.Handle, load_at_once := false) -> ([DIGEST_SIZE]byte, bool) {
  77. if !load_at_once {
  78. return hash_stream(os.stream_from_handle(hd))
  79. } else {
  80. if buf, ok := os.read_entire_file(hd); ok {
  81. return hash_bytes(buf[:]), ok
  82. }
  83. }
  84. return [DIGEST_SIZE]byte{}, false
  85. }
  86. hash :: proc {
  87. hash_stream,
  88. hash_file,
  89. hash_bytes,
  90. hash_string,
  91. hash_bytes_to_buffer,
  92. hash_string_to_buffer,
  93. }
  94. /*
  95. Low level API
  96. */
  97. Context :: _blake2.Blake2s_Context
  98. init :: proc(ctx: ^Context) {
  99. _blake2.init(ctx)
  100. }
  101. update :: proc(ctx: ^Context, data: []byte) {
  102. _blake2.update(ctx, data)
  103. }
  104. final :: proc(ctx: ^Context, hash: []byte) {
  105. _blake2.final(ctx, hash)
  106. }