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 /** | 13 /// An implementation of the [SHA-1][rfc] hash function. |
14 * SHA1 hash function implementation. | 14 /// |
15 */ | 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]. |
| 23 /// |
| 24 /// This is separate so that it can extend [HashBase] without leaking additional |
| 25 /// public memebers. |
22 class _SHA1 extends HashBase implements SHA1 { | 26 class _SHA1 extends HashBase implements SHA1 { |
| 27 /// The sixteen words from the original chunk, extended to 80 words. |
| 28 /// |
| 29 /// This is an instance variable to avoid re-allocating, but its data isn't |
| 30 /// used across invocations of [updateHash]. |
23 final Uint32List _w; | 31 final Uint32List _w; |
24 | 32 |
25 // Construct a SHA1 hasher object. | |
26 _SHA1() | 33 _SHA1() |
27 : _w = new Uint32List(80), | 34 : _w = new Uint32List(80), |
28 super(16, 5, true) { | 35 super(16, 5, true) { |
29 h[0] = 0x67452301; | 36 h[0] = 0x67452301; |
30 h[1] = 0xEFCDAB89; | 37 h[1] = 0xEFCDAB89; |
31 h[2] = 0x98BADCFE; | 38 h[2] = 0x98BADCFE; |
32 h[3] = 0x10325476; | 39 h[3] = 0x10325476; |
33 h[4] = 0xC3D2E1F0; | 40 h[4] = 0xC3D2E1F0; |
34 } | 41 } |
35 | 42 |
36 // Returns a new instance of this Hash. | |
37 SHA1 newInstance() { | 43 SHA1 newInstance() { |
38 return new _SHA1(); | 44 return new _SHA1(); |
39 } | 45 } |
40 | 46 |
41 // Compute one iteration of the SHA1 algorithm with a chunk of | |
42 // 16 32-bit pieces. | |
43 void updateHash(Uint32List m) { | 47 void updateHash(Uint32List m) { |
44 assert(m.length == 16); | 48 assert(m.length == 16); |
45 | 49 |
46 var a = h[0]; | 50 var a = h[0]; |
47 var b = h[1]; | 51 var b = h[1]; |
48 var c = h[2]; | 52 var c = h[2]; |
49 var d = h[3]; | 53 var d = h[3]; |
50 var e = h[4]; | 54 var e = h[4]; |
51 | 55 |
52 for (var i = 0; i < 80; i++) { | 56 for (var i = 0; i < 80; i++) { |
(...skipping 21 matching lines...) Expand all Loading... |
74 a = t & MASK_32; | 78 a = t & MASK_32; |
75 } | 79 } |
76 | 80 |
77 h[0] = add32(a, h[0]); | 81 h[0] = add32(a, h[0]); |
78 h[1] = add32(b, h[1]); | 82 h[1] = add32(b, h[1]); |
79 h[2] = add32(c, h[2]); | 83 h[2] = add32(c, h[2]); |
80 h[3] = add32(d, h[3]); | 84 h[3] = add32(d, h[3]); |
81 h[4] = add32(e, h[4]); | 85 h[4] = add32(e, h[4]); |
82 } | 86 } |
83 } | 87 } |
OLD | NEW |