1
2
3
4
5
6
7
8
9 package md4
10
11 import "math/bits"
12
13 var shift1 = []int{3, 7, 11, 19}
14 var shift2 = []int{3, 5, 9, 13}
15 var shift3 = []int{3, 9, 11, 15}
16
17 var xIndex2 = []uint{0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15}
18 var xIndex3 = []uint{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}
19
20 func _Block(dig *digest, p []byte) int {
21 a := dig.s[0]
22 b := dig.s[1]
23 c := dig.s[2]
24 d := dig.s[3]
25 n := 0
26 var X [16]uint32
27 for len(p) >= _Chunk {
28 aa, bb, cc, dd := a, b, c, d
29
30 j := 0
31 for i := 0; i < 16; i++ {
32 X[i] = uint32(p[j]) | uint32(p[j+1])<<8 | uint32(p[j+2])<<16 | uint32(p[j+3])<<24
33 j += 4
34 }
35
36
37
38
39
40
41
42
43
44
45
46
47
48 for i := uint(0); i < 16; i++ {
49 x := i
50 s := shift1[i%4]
51 f := ((c ^ d) & b) ^ d
52 a += f + X[x]
53 a = bits.RotateLeft32(a, s)
54 a, b, c, d = d, a, b, c
55 }
56
57
58 for i := uint(0); i < 16; i++ {
59 x := xIndex2[i]
60 s := shift2[i%4]
61 g := (b & c) | (b & d) | (c & d)
62 a += g + X[x] + 0x5a827999
63 a = bits.RotateLeft32(a, s)
64 a, b, c, d = d, a, b, c
65 }
66
67
68 for i := uint(0); i < 16; i++ {
69 x := xIndex3[i]
70 s := shift3[i%4]
71 h := b ^ c ^ d
72 a += h + X[x] + 0x6ed9eba1
73 a = bits.RotateLeft32(a, s)
74 a, b, c, d = d, a, b, c
75 }
76
77 a += aa
78 b += bb
79 c += cc
80 d += dd
81
82 p = p[_Chunk:]
83 n += _Chunk
84 }
85
86 dig.s[0] = a
87 dig.s[1] = b
88 dig.s[2] = c
89 dig.s[3] = d
90 return n
91 }
92
View as plain text