...
1
2
3
4
5
6
7
8
9
10 package md4
11
12 import (
13 "crypto"
14 "hash"
15 )
16
17 func init() {
18 crypto.RegisterHash(crypto.MD4, New)
19 }
20
21
22 const Size = 16
23
24
25 const BlockSize = 64
26
27 const (
28 _Chunk = 64
29 _Init0 = 0x67452301
30 _Init1 = 0xEFCDAB89
31 _Init2 = 0x98BADCFE
32 _Init3 = 0x10325476
33 )
34
35
36 type digest struct {
37 s [4]uint32
38 x [_Chunk]byte
39 nx int
40 len uint64
41 }
42
43 func (d *digest) Reset() {
44 d.s[0] = _Init0
45 d.s[1] = _Init1
46 d.s[2] = _Init2
47 d.s[3] = _Init3
48 d.nx = 0
49 d.len = 0
50 }
51
52
53 func New() hash.Hash {
54 d := new(digest)
55 d.Reset()
56 return d
57 }
58
59 func (d *digest) Size() int { return Size }
60
61 func (d *digest) BlockSize() int { return BlockSize }
62
63 func (d *digest) Write(p []byte) (nn int, err error) {
64 nn = len(p)
65 d.len += uint64(nn)
66 if d.nx > 0 {
67 n := len(p)
68 if n > _Chunk-d.nx {
69 n = _Chunk - d.nx
70 }
71 for i := 0; i < n; i++ {
72 d.x[d.nx+i] = p[i]
73 }
74 d.nx += n
75 if d.nx == _Chunk {
76 _Block(d, d.x[0:])
77 d.nx = 0
78 }
79 p = p[n:]
80 }
81 n := _Block(d, p)
82 p = p[n:]
83 if len(p) > 0 {
84 d.nx = copy(d.x[:], p)
85 }
86 return
87 }
88
89 func (d0 *digest) Sum(in []byte) []byte {
90
91 d := new(digest)
92 *d = *d0
93
94
95 len := d.len
96 var tmp [64]byte
97 tmp[0] = 0x80
98 if len%64 < 56 {
99 d.Write(tmp[0 : 56-len%64])
100 } else {
101 d.Write(tmp[0 : 64+56-len%64])
102 }
103
104
105 len <<= 3
106 for i := uint(0); i < 8; i++ {
107 tmp[i] = byte(len >> (8 * i))
108 }
109 d.Write(tmp[0:8])
110
111 if d.nx != 0 {
112 panic("d.nx != 0")
113 }
114
115 for _, s := range d.s {
116 in = append(in, byte(s>>0))
117 in = append(in, byte(s>>8))
118 in = append(in, byte(s>>16))
119 in = append(in, byte(s>>24))
120 }
121 return in
122 }
123
View as plain text