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 part of crypto; | 5 library crypto.sha1; |
| 6 |
| 7 import 'dart:typed_data'; |
| 8 |
| 9 import 'hash.dart'; |
| 10 import 'hash_base.dart'; |
| 11 import 'utils.dart'; |
6 | 12 |
7 /** | 13 /** |
8 * SHA1 hash function implementation. | 14 * SHA1 hash function implementation. |
9 */ | 15 */ |
10 class SHA1 extends _HashBase { | 16 abstract class SHA1 implements Hash { |
| 17 factory SHA1() = _SHA1; |
| 18 |
| 19 SHA1 newInstance(); |
| 20 } |
| 21 |
| 22 class _SHA1 extends HashBase implements SHA1 { |
11 final Uint32List _w; | 23 final Uint32List _w; |
12 | 24 |
13 // Construct a SHA1 hasher object. | 25 // Construct a SHA1 hasher object. |
14 SHA1() | 26 _SHA1() |
15 : _w = new Uint32List(80), | 27 : _w = new Uint32List(80), |
16 super(16, 5, true) { | 28 super(16, 5, true) { |
17 _h[0] = 0x67452301; | 29 h[0] = 0x67452301; |
18 _h[1] = 0xEFCDAB89; | 30 h[1] = 0xEFCDAB89; |
19 _h[2] = 0x98BADCFE; | 31 h[2] = 0x98BADCFE; |
20 _h[3] = 0x10325476; | 32 h[3] = 0x10325476; |
21 _h[4] = 0xC3D2E1F0; | 33 h[4] = 0xC3D2E1F0; |
22 } | 34 } |
23 | 35 |
24 // Returns a new instance of this Hash. | 36 // Returns a new instance of this Hash. |
25 SHA1 newInstance() { | 37 SHA1 newInstance() { |
26 return new SHA1(); | 38 return new _SHA1(); |
27 } | 39 } |
28 | 40 |
29 // Compute one iteration of the SHA1 algorithm with a chunk of | 41 // Compute one iteration of the SHA1 algorithm with a chunk of |
30 // 16 32-bit pieces. | 42 // 16 32-bit pieces. |
31 void _updateHash(Uint32List m) { | 43 void updateHash(Uint32List m) { |
32 assert(m.length == 16); | 44 assert(m.length == 16); |
33 | 45 |
34 var a = _h[0]; | 46 var a = h[0]; |
35 var b = _h[1]; | 47 var b = h[1]; |
36 var c = _h[2]; | 48 var c = h[2]; |
37 var d = _h[3]; | 49 var d = h[3]; |
38 var e = _h[4]; | 50 var e = h[4]; |
39 | 51 |
40 for (var i = 0; i < 80; i++) { | 52 for (var i = 0; i < 80; i++) { |
41 if (i < 16) { | 53 if (i < 16) { |
42 _w[i] = m[i]; | 54 _w[i] = m[i]; |
43 } else { | 55 } else { |
44 var n = _w[i - 3] ^ _w[i - 8] ^ _w[i - 14] ^ _w[i - 16]; | 56 var n = _w[i - 3] ^ _w[i - 8] ^ _w[i - 14] ^ _w[i - 16]; |
45 _w[i] = _rotl32(n, 1); | 57 _w[i] = rotl32(n, 1); |
46 } | 58 } |
47 var t = _add32(_add32(_rotl32(a, 5), e), _w[i]); | 59 var t = add32(add32(rotl32(a, 5), e), _w[i]); |
48 if (i < 20) { | 60 if (i < 20) { |
49 t = _add32(_add32(t, (b & c) | (~b & d)), 0x5A827999); | 61 t = add32(add32(t, (b & c) | (~b & d)), 0x5A827999); |
50 } else if (i < 40) { | 62 } else if (i < 40) { |
51 t = _add32(_add32(t, (b ^ c ^ d)), 0x6ED9EBA1); | 63 t = add32(add32(t, (b ^ c ^ d)), 0x6ED9EBA1); |
52 } else if (i < 60) { | 64 } else if (i < 60) { |
53 t = _add32(_add32(t, (b & c) | (b & d) | (c & d)), 0x8F1BBCDC); | 65 t = add32(add32(t, (b & c) | (b & d) | (c & d)), 0x8F1BBCDC); |
54 } else { | 66 } else { |
55 t = _add32(_add32(t, b ^ c ^ d), 0xCA62C1D6); | 67 t = add32(add32(t, b ^ c ^ d), 0xCA62C1D6); |
56 } | 68 } |
57 | 69 |
58 e = d; | 70 e = d; |
59 d = c; | 71 d = c; |
60 c = _rotl32(b, 30); | 72 c = rotl32(b, 30); |
61 b = a; | 73 b = a; |
62 a = t & _MASK_32; | 74 a = t & MASK_32; |
63 } | 75 } |
64 | 76 |
65 _h[0] = _add32(a, _h[0]); | 77 h[0] = add32(a, h[0]); |
66 _h[1] = _add32(b, _h[1]); | 78 h[1] = add32(b, h[1]); |
67 _h[2] = _add32(c, _h[2]); | 79 h[2] = add32(c, h[2]); |
68 _h[3] = _add32(d, _h[3]); | 80 h[3] = add32(d, h[3]); |
69 _h[4] = _add32(e, _h[4]); | 81 h[4] = add32(e, h[4]); |
70 } | 82 } |
71 } | 83 } |
OLD | NEW |