md5.odin 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package md5
  2. /*
  3. Copyright 2021 zhibog
  4. Made available under the BSD-3 license.
  5. List of contributors:
  6. zhibog, dotbmp: Initial implementation.
  7. Implementation of the MD5 hashing algorithm, as defined in RFC 1321 <https://datatracker.ietf.org/doc/html/rfc1321>
  8. */
  9. import "core:mem"
  10. import "core:os"
  11. import "core:io"
  12. import "../util"
  13. /*
  14. High level API
  15. */
  16. DIGEST_SIZE :: 16
  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: Md5_Context
  27. init(&ctx)
  28. update(&ctx, data)
  29. final(&ctx, hash[:])
  30. return hash
  31. }
  32. // hash_string_to_buffer will hash the given input and assign the
  33. // computed hash to the second parameter.
  34. // It requires that the destination buffer is at least as big as the digest size
  35. hash_string_to_buffer :: proc(data: string, hash: []byte) {
  36. hash_bytes_to_buffer(transmute([]byte)(data), hash)
  37. }
  38. // hash_bytes_to_buffer will hash the given input and write the
  39. // computed hash into the second parameter.
  40. // It requires that the destination buffer is at least as big as the digest size
  41. hash_bytes_to_buffer :: proc(data, hash: []byte) {
  42. assert(len(hash) >= DIGEST_SIZE, "Size of destination buffer is smaller than the digest size")
  43. ctx: Md5_Context
  44. init(&ctx)
  45. update(&ctx, data)
  46. final(&ctx, hash)
  47. }
  48. // hash_stream will read the stream in chunks and compute a
  49. // hash from its contents
  50. hash_stream :: proc(s: io.Stream) -> ([DIGEST_SIZE]byte, bool) {
  51. hash: [DIGEST_SIZE]byte
  52. ctx: Md5_Context
  53. init(&ctx)
  54. buf := make([]byte, 512)
  55. defer delete(buf)
  56. read := 1
  57. for read > 0 {
  58. read, _ = s->impl_read(buf)
  59. if read > 0 {
  60. update(&ctx, buf[:read])
  61. }
  62. }
  63. final(&ctx, hash[:])
  64. return hash, true
  65. }
  66. // hash_file will read the file provided by the given handle
  67. // and compute a hash
  68. hash_file :: proc(hd: os.Handle, load_at_once := false) -> ([DIGEST_SIZE]byte, bool) {
  69. if !load_at_once {
  70. return hash_stream(os.stream_from_handle(hd))
  71. } else {
  72. if buf, ok := os.read_entire_file(hd); ok {
  73. return hash_bytes(buf[:]), ok
  74. }
  75. }
  76. return [DIGEST_SIZE]byte{}, false
  77. }
  78. hash :: proc {
  79. hash_stream,
  80. hash_file,
  81. hash_bytes,
  82. hash_string,
  83. hash_bytes_to_buffer,
  84. hash_string_to_buffer,
  85. }
  86. /*
  87. Low level API
  88. */
  89. init :: proc(ctx: ^Md5_Context) {
  90. ctx.state[0] = 0x67452301
  91. ctx.state[1] = 0xefcdab89
  92. ctx.state[2] = 0x98badcfe
  93. ctx.state[3] = 0x10325476
  94. }
  95. update :: proc(ctx: ^Md5_Context, data: []byte) {
  96. for i := 0; i < len(data); i += 1 {
  97. ctx.data[ctx.datalen] = data[i]
  98. ctx.datalen += 1
  99. if(ctx.datalen == BLOCK_SIZE) {
  100. transform(ctx, ctx.data[:])
  101. ctx.bitlen += 512
  102. ctx.datalen = 0
  103. }
  104. }
  105. }
  106. final :: proc(ctx: ^Md5_Context, hash: []byte){
  107. i : u32
  108. i = ctx.datalen
  109. if ctx.datalen < 56 {
  110. ctx.data[i] = 0x80
  111. i += 1
  112. for i < 56 {
  113. ctx.data[i] = 0x00
  114. i += 1
  115. }
  116. } else if ctx.datalen >= 56 {
  117. ctx.data[i] = 0x80
  118. i += 1
  119. for i < BLOCK_SIZE {
  120. ctx.data[i] = 0x00
  121. i += 1
  122. }
  123. transform(ctx, ctx.data[:])
  124. mem.set(&ctx.data, 0, 56)
  125. }
  126. ctx.bitlen += u64(ctx.datalen * 8)
  127. ctx.data[56] = byte(ctx.bitlen)
  128. ctx.data[57] = byte(ctx.bitlen >> 8)
  129. ctx.data[58] = byte(ctx.bitlen >> 16)
  130. ctx.data[59] = byte(ctx.bitlen >> 24)
  131. ctx.data[60] = byte(ctx.bitlen >> 32)
  132. ctx.data[61] = byte(ctx.bitlen >> 40)
  133. ctx.data[62] = byte(ctx.bitlen >> 48)
  134. ctx.data[63] = byte(ctx.bitlen >> 56)
  135. transform(ctx, ctx.data[:])
  136. for i = 0; i < 4; i += 1 {
  137. hash[i] = byte(ctx.state[0] >> (i * 8)) & 0x000000ff
  138. hash[i + 4] = byte(ctx.state[1] >> (i * 8)) & 0x000000ff
  139. hash[i + 8] = byte(ctx.state[2] >> (i * 8)) & 0x000000ff
  140. hash[i + 12] = byte(ctx.state[3] >> (i * 8)) & 0x000000ff
  141. }
  142. }
  143. /*
  144. MD4 implementation
  145. */
  146. BLOCK_SIZE :: 64
  147. Md5_Context :: struct {
  148. data: [BLOCK_SIZE]byte,
  149. state: [4]u32,
  150. bitlen: u64,
  151. datalen: u32,
  152. }
  153. /*
  154. @note(zh): F, G, H and I, as mentioned in the RFC, have been inlined into FF, GG, HH
  155. and II respectively, instead of declaring them separately.
  156. */
  157. FF :: #force_inline proc "contextless" (a, b, c, d, m: u32, s: int, t: u32) -> u32 {
  158. return b + util.ROTL32(a + ((b & c) | (~b & d)) + m + t, s)
  159. }
  160. GG :: #force_inline proc "contextless" (a, b, c, d, m: u32, s: int, t: u32) -> u32 {
  161. return b + util.ROTL32(a + ((b & d) | (c & ~d)) + m + t, s)
  162. }
  163. HH :: #force_inline proc "contextless" (a, b, c, d, m: u32, s: int, t: u32) -> u32 {
  164. return b + util.ROTL32(a + (b ~ c ~ d) + m + t, s)
  165. }
  166. II :: #force_inline proc "contextless" (a, b, c, d, m: u32, s: int, t: u32) -> u32 {
  167. return b + util.ROTL32(a + (c ~ (b | ~d)) + m + t, s)
  168. }
  169. transform :: proc(ctx: ^Md5_Context, data: []byte) {
  170. i, j: u32
  171. m: [DIGEST_SIZE]u32
  172. for i, j = 0, 0; i < DIGEST_SIZE; i+=1 {
  173. m[i] = u32(data[j]) + u32(data[j + 1]) << 8 + u32(data[j + 2]) << 16 + u32(data[j + 3]) << 24
  174. j += 4
  175. }
  176. a := ctx.state[0]
  177. b := ctx.state[1]
  178. c := ctx.state[2]
  179. d := ctx.state[3]
  180. a = FF(a, b, c, d, m[0], 7, 0xd76aa478)
  181. d = FF(d, a, b, c, m[1], 12, 0xe8c7b756)
  182. c = FF(c, d, a, b, m[2], 17, 0x242070db)
  183. b = FF(b, c, d, a, m[3], 22, 0xc1bdceee)
  184. a = FF(a, b, c, d, m[4], 7, 0xf57c0faf)
  185. d = FF(d, a, b, c, m[5], 12, 0x4787c62a)
  186. c = FF(c, d, a, b, m[6], 17, 0xa8304613)
  187. b = FF(b, c, d, a, m[7], 22, 0xfd469501)
  188. a = FF(a, b, c, d, m[8], 7, 0x698098d8)
  189. d = FF(d, a, b, c, m[9], 12, 0x8b44f7af)
  190. c = FF(c, d, a, b, m[10], 17, 0xffff5bb1)
  191. b = FF(b, c, d, a, m[11], 22, 0x895cd7be)
  192. a = FF(a, b, c, d, m[12], 7, 0x6b901122)
  193. d = FF(d, a, b, c, m[13], 12, 0xfd987193)
  194. c = FF(c, d, a, b, m[14], 17, 0xa679438e)
  195. b = FF(b, c, d, a, m[15], 22, 0x49b40821)
  196. a = GG(a, b, c, d, m[1], 5, 0xf61e2562)
  197. d = GG(d, a, b, c, m[6], 9, 0xc040b340)
  198. c = GG(c, d, a, b, m[11], 14, 0x265e5a51)
  199. b = GG(b, c, d, a, m[0], 20, 0xe9b6c7aa)
  200. a = GG(a, b, c, d, m[5], 5, 0xd62f105d)
  201. d = GG(d, a, b, c, m[10], 9, 0x02441453)
  202. c = GG(c, d, a, b, m[15], 14, 0xd8a1e681)
  203. b = GG(b, c, d, a, m[4], 20, 0xe7d3fbc8)
  204. a = GG(a, b, c, d, m[9], 5, 0x21e1cde6)
  205. d = GG(d, a, b, c, m[14], 9, 0xc33707d6)
  206. c = GG(c, d, a, b, m[3], 14, 0xf4d50d87)
  207. b = GG(b, c, d, a, m[8], 20, 0x455a14ed)
  208. a = GG(a, b, c, d, m[13], 5, 0xa9e3e905)
  209. d = GG(d, a, b, c, m[2], 9, 0xfcefa3f8)
  210. c = GG(c, d, a, b, m[7], 14, 0x676f02d9)
  211. b = GG(b, c, d, a, m[12], 20, 0x8d2a4c8a)
  212. a = HH(a, b, c, d, m[5], 4, 0xfffa3942)
  213. d = HH(d, a, b, c, m[8], 11, 0x8771f681)
  214. c = HH(c, d, a, b, m[11], 16, 0x6d9d6122)
  215. b = HH(b, c, d, a, m[14], 23, 0xfde5380c)
  216. a = HH(a, b, c, d, m[1], 4, 0xa4beea44)
  217. d = HH(d, a, b, c, m[4], 11, 0x4bdecfa9)
  218. c = HH(c, d, a, b, m[7], 16, 0xf6bb4b60)
  219. b = HH(b, c, d, a, m[10], 23, 0xbebfbc70)
  220. a = HH(a, b, c, d, m[13], 4, 0x289b7ec6)
  221. d = HH(d, a, b, c, m[0], 11, 0xeaa127fa)
  222. c = HH(c, d, a, b, m[3], 16, 0xd4ef3085)
  223. b = HH(b, c, d, a, m[6], 23, 0x04881d05)
  224. a = HH(a, b, c, d, m[9], 4, 0xd9d4d039)
  225. d = HH(d, a, b, c, m[12], 11, 0xe6db99e5)
  226. c = HH(c, d, a, b, m[15], 16, 0x1fa27cf8)
  227. b = HH(b, c, d, a, m[2], 23, 0xc4ac5665)
  228. a = II(a, b, c, d, m[0], 6, 0xf4292244)
  229. d = II(d, a, b, c, m[7], 10, 0x432aff97)
  230. c = II(c, d, a, b, m[14], 15, 0xab9423a7)
  231. b = II(b, c, d, a, m[5], 21, 0xfc93a039)
  232. a = II(a, b, c, d, m[12], 6, 0x655b59c3)
  233. d = II(d, a, b, c, m[3], 10, 0x8f0ccc92)
  234. c = II(c, d, a, b, m[10], 15, 0xffeff47d)
  235. b = II(b, c, d, a, m[1], 21, 0x85845dd1)
  236. a = II(a, b, c, d, m[8], 6, 0x6fa87e4f)
  237. d = II(d, a, b, c, m[15], 10, 0xfe2ce6e0)
  238. c = II(c, d, a, b, m[6], 15, 0xa3014314)
  239. b = II(b, c, d, a, m[13], 21, 0x4e0811a1)
  240. a = II(a, b, c, d, m[4], 6, 0xf7537e82)
  241. d = II(d, a, b, c, m[11], 10, 0xbd3af235)
  242. c = II(c, d, a, b, m[2], 15, 0x2ad7d2bb)
  243. b = II(b, c, d, a, m[9], 21, 0xeb86d391)
  244. ctx.state[0] += a
  245. ctx.state[1] += b
  246. ctx.state[2] += c
  247. ctx.state[3] += d
  248. }