v / vlib / crypto / md5 / md5.v
170 lines · 153 sloc · 4.07 KB · 016010bf63e980d1a93eaa71d2c91515c846ae88
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4// Package md5 implements the MD5 hash algorithm as defined in RFC 1321.
5// MD5 is cryptographically broken and should not be used for secure
6// applications.
7// Based off: https://github.com/golang/go/blob/master/src/crypto/md5
8// Last commit: https://github.com/golang/go/commit/ed7f323c8f4f6bc61a75146bf34f5b8f73063a17
9module md5
10
11import encoding.binary
12
13// The size of an MD5 checksum in bytes.
14pub const size = 16
15// The blocksize of MD5 in bytes.
16pub const block_size = 64
17
18const init0 = u32(0x67452301)
19const init1 = u32(0xEFCDAB89)
20const init2 = u32(0x98BADCFE)
21const init3 = u32(0x10325476)
22
23// Digest represents the partial evaluation of a checksum.
24struct Digest {
25mut:
26 s []u32
27 x []u8
28 nx int
29 len u64
30}
31
32// free the resources taken by the Digest `d`
33@[unsafe]
34pub fn (mut d Digest) free() {
35 $if prealloc {
36 return
37 }
38 unsafe { d.x.free() }
39}
40
41fn (mut d Digest) init() {
42 d.s = []u32{len: (4)}
43 d.x = []u8{len: block_size}
44 d.reset()
45}
46
47// reset the state of the Digest `d`
48pub fn (mut d Digest) reset() {
49 d.s[0] = u32(init0)
50 d.s[1] = u32(init1)
51 d.s[2] = u32(init2)
52 d.s[3] = u32(init3)
53 d.nx = 0
54 d.len = 0
55}
56
57fn (d &Digest) clone() &Digest {
58 return &Digest{
59 ...d
60 s: d.s.clone()
61 x: d.x.clone()
62 }
63}
64
65// new returns a new Digest (implementing hash.Hash) computing the MD5 checksum.
66pub fn new() &Digest {
67 mut d := &Digest{}
68 d.init()
69 return d
70}
71
72// write writes the contents of `p_` to the internal hash representation.
73pub fn (mut d Digest) write(p_ []u8) !int {
74 unsafe {
75 mut p := p_
76 nn := p.len
77 d.len += u64(nn)
78 if d.nx > 0 {
79 n := copy(mut d.x[d.nx..], p)
80 d.nx += n
81 if d.nx == block_size {
82 block(mut d, d.x)
83 d.nx = 0
84 }
85 if n >= p.len {
86 p = []
87 } else {
88 p = p[n..]
89 }
90 }
91 if p.len >= block_size {
92 n := p.len & ~(block_size - 1)
93 block(mut d, p[..n])
94 if n >= p.len {
95 p = []
96 } else {
97 p = p[n..]
98 }
99 }
100 if p.len > 0 {
101 d.nx = copy(mut d.x, p)
102 }
103 return nn
104 }
105}
106
107// sum returns the md5 sum of the bytes in `b_in`.
108pub fn (d &Digest) sum(b_in []u8) []u8 {
109 // Make a copy of d so that caller can keep writing and summing.
110 mut d0 := d.clone()
111 hash := d0.checksum()
112 mut b_out := b_in.clone()
113 b_out << hash
114 return b_out
115}
116
117// checksum returns the byte checksum of the `Digest`,
118fn (mut d Digest) checksum() []u8 {
119 // Append 0x80 to the end of the message and then append zeros
120 // until the length is a multiple of 56 bytes. Finally append
121 // 8 bytes representing the message length in bits.
122 //
123 // 1 byte end marker :: 0-63 padding bytes :: 8 byte length
124 // tmp := [1 + 63 + 8]u8{0x80}
125 mut tmp := []u8{len: (1 + 63 + 8)}
126 tmp[0] = 0x80
127 pad := int((55 - d.len) % 64) // calculate number of padding bytes
128 binary.little_endian_put_u64(mut tmp[1 + pad..], d.len << 3) // append length in bits
129 d.write(tmp[..1 + pad + 8]) or { panic(err) }
130 // The previous write ensures that a whole number of
131 // blocks (i.e. a multiple of 64 bytes) have been hashed.
132 if d.nx != 0 {
133 panic('d.nx != 0')
134 }
135 mut digest := []u8{len: size}
136 binary.little_endian_put_u32(mut digest, d.s[0])
137 binary.little_endian_put_u32(mut digest[4..], d.s[1])
138 binary.little_endian_put_u32(mut digest[8..], d.s[2])
139 binary.little_endian_put_u32(mut digest[12..], d.s[3])
140 return digest
141}
142
143// sum returns the MD5 checksum of the data.
144pub fn sum(data []u8) []u8 {
145 mut d := new()
146 d.write(data) or { panic(err) }
147 return d.checksum()
148}
149
150fn block(mut dig Digest, p []u8) {
151 // For now just use block_generic until we have specific
152 // architecture optimized versions
153 block_generic(mut dig, p)
154}
155
156// size returns the size of the checksum in bytes.
157pub fn (d &Digest) size() int {
158 return size
159}
160
161// block_size returns the block size of the checksum in bytes.
162pub fn (d &Digest) block_size() int {
163 return block_size
164}
165
166// hexhash returns a hexadecimal MD5 hash sum `string` of `s`.
167// Example: assert md5.hexhash('V') == '5206560a306a2e085a437fd258eb57ce'
168pub fn hexhash(s string) string {
169 return sum(s.bytes()).hex()
170}
171