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:convert'; |
7 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
8 | 9 |
| 10 import 'digest.dart'; |
9 import 'hash.dart'; | 11 import 'hash.dart'; |
10 import 'hash_base.dart'; | 12 import 'hash_sink.dart'; |
11 import 'utils.dart'; | 13 import 'utils.dart'; |
12 | 14 |
| 15 /// An instance of [SHA1]. |
| 16 /// |
| 17 /// This instance provides convenient access to the [SHA1][rfc] hash function. |
| 18 /// |
| 19 /// [rfc]: http://tools.ietf.org/html/rfc3174 |
| 20 final sha1 = new SHA1(); |
| 21 |
13 /// An implementation of the [SHA-1][rfc] hash function. | 22 /// An implementation of the [SHA-1][rfc] hash function. |
14 /// | 23 /// |
15 /// [rfc]: http://tools.ietf.org/html/rfc3174 | 24 /// [rfc]: http://tools.ietf.org/html/rfc3174 |
16 abstract class SHA1 implements Hash { | 25 /// |
17 factory SHA1() = _SHA1; | 26 /// Note that it's almost always easier to use [sha1] rather than creating a new |
| 27 /// instance. |
| 28 class SHA1 extends Hash { |
| 29 final int blockSize = 16 * bytesPerWord; |
18 | 30 |
19 SHA1 newInstance(); | 31 @Deprecated("Use the sha1 field instead.") |
| 32 SHA1(); |
| 33 |
| 34 SHA1 newInstance() => new SHA1(); |
| 35 |
| 36 ByteConversionSink startChunkedConversion(Sink<Digest> sink) => |
| 37 new ByteConversionSink.from(new _SHA1Sink(sink)); |
20 } | 38 } |
21 | 39 |
22 /// The concrete implementation of [SHA1]. | 40 /// The concrete implementation of [SHA1]. |
23 /// | 41 /// |
24 /// This is separate so that it can extend [HashBase] without leaking additional | 42 /// This is separate so that it can extend [HashBase] without leaking additional |
25 /// public memebers. | 43 /// public memebers. |
26 class _SHA1 extends HashBase implements SHA1 { | 44 class _SHA1Sink extends HashSink { |
27 final digest = new Uint32List(5); | 45 final digest = new Uint32List(5); |
28 | 46 |
29 /// The sixteen words from the original chunk, extended to 80 words. | 47 /// The sixteen words from the original chunk, extended to 80 words. |
30 /// | 48 /// |
31 /// This is an instance variable to avoid re-allocating, but its data isn't | 49 /// This is an instance variable to avoid re-allocating, but its data isn't |
32 /// used across invocations of [updateHash]. | 50 /// used across invocations of [updateHash]. |
33 final Uint32List _extended; | 51 final Uint32List _extended; |
34 | 52 |
35 _SHA1() | 53 _SHA1Sink(Sink<Digest> sink) |
36 : _extended = new Uint32List(80), | 54 : _extended = new Uint32List(80), |
37 super(16) { | 55 super(sink, 16) { |
38 digest[0] = 0x67452301; | 56 digest[0] = 0x67452301; |
39 digest[1] = 0xEFCDAB89; | 57 digest[1] = 0xEFCDAB89; |
40 digest[2] = 0x98BADCFE; | 58 digest[2] = 0x98BADCFE; |
41 digest[3] = 0x10325476; | 59 digest[3] = 0x10325476; |
42 digest[4] = 0xC3D2E1F0; | 60 digest[4] = 0xC3D2E1F0; |
43 } | 61 } |
44 | 62 |
45 SHA1 newInstance() => new _SHA1(); | |
46 | |
47 void updateHash(Uint32List chunk) { | 63 void updateHash(Uint32List chunk) { |
48 assert(chunk.length == 16); | 64 assert(chunk.length == 16); |
49 | 65 |
50 var a = digest[0]; | 66 var a = digest[0]; |
51 var b = digest[1]; | 67 var b = digest[1]; |
52 var c = digest[2]; | 68 var c = digest[2]; |
53 var d = digest[3]; | 69 var d = digest[3]; |
54 var e = digest[4]; | 70 var e = digest[4]; |
55 | 71 |
56 for (var i = 0; i < 80; i++) { | 72 for (var i = 0; i < 80; i++) { |
(...skipping 24 matching lines...) Expand all Loading... |
81 a = newA & mask32; | 97 a = newA & mask32; |
82 } | 98 } |
83 | 99 |
84 digest[0] = add32(a, digest[0]); | 100 digest[0] = add32(a, digest[0]); |
85 digest[1] = add32(b, digest[1]); | 101 digest[1] = add32(b, digest[1]); |
86 digest[2] = add32(c, digest[2]); | 102 digest[2] = add32(c, digest[2]); |
87 digest[3] = add32(d, digest[3]); | 103 digest[3] = add32(d, digest[3]); |
88 digest[4] = add32(e, digest[4]); | 104 digest[4] = add32(e, digest[4]); |
89 } | 105 } |
90 } | 106 } |
OLD | NEW |