OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package md5 implements the MD5 hash algorithm as defined in RFC 1321. | 5 // Package md5 implements the MD5 hash algorithm as defined in RFC 1321. |
6 package md5 | 6 package md5 |
7 | 7 |
8 import ( | 8 import ( |
9 "crypto" | 9 "crypto" |
10 "hash" | 10 "hash" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 p = p[n:] | 70 p = p[n:] |
71 } | 71 } |
72 n := _Block(d, p) | 72 n := _Block(d, p) |
73 p = p[n:] | 73 p = p[n:] |
74 if len(p) > 0 { | 74 if len(p) > 0 { |
75 d.nx = copy(d.x[:], p) | 75 d.nx = copy(d.x[:], p) |
76 } | 76 } |
77 return | 77 return |
78 } | 78 } |
79 | 79 |
80 func (d0 *digest) Sum() []byte { | 80 func (d0 *digest) Sum(in []byte) []byte { |
81 // Make a copy of d0 so that caller can keep writing and summing. | 81 // Make a copy of d0 so that caller can keep writing and summing. |
82 d := new(digest) | 82 d := new(digest) |
83 *d = *d0 | 83 *d = *d0 |
84 | 84 |
85 // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. | 85 // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. |
86 len := d.len | 86 len := d.len |
87 var tmp [64]byte | 87 var tmp [64]byte |
88 tmp[0] = 0x80 | 88 tmp[0] = 0x80 |
89 if len%64 < 56 { | 89 if len%64 < 56 { |
90 d.Write(tmp[0 : 56-len%64]) | 90 d.Write(tmp[0 : 56-len%64]) |
91 } else { | 91 } else { |
92 d.Write(tmp[0 : 64+56-len%64]) | 92 d.Write(tmp[0 : 64+56-len%64]) |
93 } | 93 } |
94 | 94 |
95 // Length in bits. | 95 // Length in bits. |
96 len <<= 3 | 96 len <<= 3 |
97 for i := uint(0); i < 8; i++ { | 97 for i := uint(0); i < 8; i++ { |
98 tmp[i] = byte(len >> (8 * i)) | 98 tmp[i] = byte(len >> (8 * i)) |
99 } | 99 } |
100 d.Write(tmp[0:8]) | 100 d.Write(tmp[0:8]) |
101 | 101 |
102 if d.nx != 0 { | 102 if d.nx != 0 { |
103 panic("d.nx != 0") | 103 panic("d.nx != 0") |
104 } | 104 } |
105 | 105 |
106 p := make([]byte, 16) | |
107 j := 0 | |
108 for _, s := range d.s { | 106 for _, s := range d.s { |
109 » » p[j+0] = byte(s >> 0) | 107 » » in = append(in, byte(s>>0)) |
110 » » p[j+1] = byte(s >> 8) | 108 » » in = append(in, byte(s>>8)) |
111 » » p[j+2] = byte(s >> 16) | 109 » » in = append(in, byte(s>>16)) |
112 » » p[j+3] = byte(s >> 24) | 110 » » in = append(in, byte(s>>24)) |
113 » » j += 4 | |
114 } | 111 } |
115 » return p | 112 » return in |
116 } | 113 } |
OLD | NEW |