OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library crypto.sha1; | 5 library crypto.sha1; |
6 | 6 |
7 import 'dart:typed_data'; | 7 import 'dart:typed_data'; |
8 | 8 |
9 import 'hash.dart'; | 9 import 'hash.dart'; |
10 import 'hash_base.dart'; | 10 import 'hash_base.dart'; |
11 import 'utils.dart'; | 11 import 'utils.dart'; |
12 | 12 |
13 /// An implementation of the [SHA-1][rfc] hash function. | 13 /// An implementation of the [SHA-1][rfc] hash function. |
14 /// | 14 /// |
15 /// [rfc]: http://tools.ietf.org/html/rfc3174 | 15 /// [rfc]: http://tools.ietf.org/html/rfc3174 |
16 abstract class SHA1 implements Hash { | 16 abstract class SHA1 implements Hash { |
17 factory SHA1() = _SHA1; | 17 factory SHA1() = _SHA1; |
18 | 18 |
19 SHA1 newInstance(); | 19 SHA1 newInstance(); |
20 } | 20 } |
21 | 21 |
22 /// The concrete implementation of [SHA1]. | 22 /// The concrete implementation of [SHA1]. |
23 /// | 23 /// |
24 /// This is separate so that it can extend [HashBase] without leaking additional | 24 /// This is separate so that it can extend [HashBase] without leaking additional |
25 /// public memebers. | 25 /// public memebers. |
26 class _SHA1 extends HashBase implements SHA1 { | 26 class _SHA1 extends HashBase implements SHA1 { |
| 27 final digest = new Uint32List(5); |
| 28 |
27 /// The sixteen words from the original chunk, extended to 80 words. | 29 /// The sixteen words from the original chunk, extended to 80 words. |
28 /// | 30 /// |
29 /// This is an instance variable to avoid re-allocating, but its data isn't | 31 /// This is an instance variable to avoid re-allocating, but its data isn't |
30 /// used across invocations of [updateHash]. | 32 /// used across invocations of [updateHash]. |
31 final Uint32List _w; | 33 final Uint32List _extended; |
32 | 34 |
33 _SHA1() | 35 _SHA1() |
34 : _w = new Uint32List(80), | 36 : _extended = new Uint32List(80), |
35 super(16, 5, true) { | 37 super(16) { |
36 h[0] = 0x67452301; | 38 digest[0] = 0x67452301; |
37 h[1] = 0xEFCDAB89; | 39 digest[1] = 0xEFCDAB89; |
38 h[2] = 0x98BADCFE; | 40 digest[2] = 0x98BADCFE; |
39 h[3] = 0x10325476; | 41 digest[3] = 0x10325476; |
40 h[4] = 0xC3D2E1F0; | 42 digest[4] = 0xC3D2E1F0; |
41 } | 43 } |
42 | 44 |
43 SHA1 newInstance() { | 45 SHA1 newInstance() => new _SHA1(); |
44 return new _SHA1(); | |
45 } | |
46 | 46 |
47 void updateHash(Uint32List m) { | 47 void updateHash(Uint32List chunk) { |
48 assert(m.length == 16); | 48 assert(chunk.length == 16); |
49 | 49 |
50 var a = h[0]; | 50 var a = digest[0]; |
51 var b = h[1]; | 51 var b = digest[1]; |
52 var c = h[2]; | 52 var c = digest[2]; |
53 var d = h[3]; | 53 var d = digest[3]; |
54 var e = h[4]; | 54 var e = digest[4]; |
55 | 55 |
56 for (var i = 0; i < 80; i++) { | 56 for (var i = 0; i < 80; i++) { |
57 if (i < 16) { | 57 if (i < 16) { |
58 _w[i] = m[i]; | 58 _extended[i] = chunk[i]; |
59 } else { | 59 } else { |
60 var n = _w[i - 3] ^ _w[i - 8] ^ _w[i - 14] ^ _w[i - 16]; | 60 _extended[i] = rotl32( |
61 _w[i] = rotl32(n, 1); | 61 _extended[i - 3] ^ _extended[i - 8] ^ _extended[i - 14] ^ |
| 62 _extended[i - 16], |
| 63 1); |
62 } | 64 } |
63 var t = add32(add32(rotl32(a, 5), e), _w[i]); | 65 |
| 66 var newA = add32(add32(rotl32(a, 5), e), _extended[i]); |
64 if (i < 20) { | 67 if (i < 20) { |
65 t = add32(add32(t, (b & c) | (~b & d)), 0x5A827999); | 68 newA = add32(add32(newA, (b & c) | (~b & d)), 0x5A827999); |
66 } else if (i < 40) { | 69 } else if (i < 40) { |
67 t = add32(add32(t, (b ^ c ^ d)), 0x6ED9EBA1); | 70 newA = add32(add32(newA, (b ^ c ^ d)), 0x6ED9EBA1); |
68 } else if (i < 60) { | 71 } else if (i < 60) { |
69 t = add32(add32(t, (b & c) | (b & d) | (c & d)), 0x8F1BBCDC); | 72 newA = add32(add32(newA, (b & c) | (b & d) | (c & d)), 0x8F1BBCDC); |
70 } else { | 73 } else { |
71 t = add32(add32(t, b ^ c ^ d), 0xCA62C1D6); | 74 newA = add32(add32(newA, b ^ c ^ d), 0xCA62C1D6); |
72 } | 75 } |
73 | 76 |
74 e = d; | 77 e = d; |
75 d = c; | 78 d = c; |
76 c = rotl32(b, 30); | 79 c = rotl32(b, 30); |
77 b = a; | 80 b = a; |
78 a = t & MASK_32; | 81 a = newA & mask32; |
79 } | 82 } |
80 | 83 |
81 h[0] = add32(a, h[0]); | 84 digest[0] = add32(a, digest[0]); |
82 h[1] = add32(b, h[1]); | 85 digest[1] = add32(b, digest[1]); |
83 h[2] = add32(c, h[2]); | 86 digest[2] = add32(c, digest[2]); |
84 h[3] = add32(d, h[3]); | 87 digest[3] = add32(d, digest[3]); |
85 h[4] = add32(e, h[4]); | 88 digest[4] = add32(e, digest[4]); |
86 } | 89 } |
87 } | 90 } |
OLD | NEW |