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 SHA1 newInstance() => new SHA1(); |
| 32 |
| 33 ByteConversionSink startChunkedConversion(Sink<Digest> sink) => |
| 34 new ByteConversionSink.from(new _SHA1Sink(sink)); |
20 } | 35 } |
21 | 36 |
22 /// The concrete implementation of [SHA1]. | 37 /// The concrete implementation of [SHA1]. |
23 /// | 38 /// |
24 /// This is separate so that it can extend [HashBase] without leaking additional | 39 /// This is separate so that it can extend [HashBase] without leaking additional |
25 /// public memebers. | 40 /// public memebers. |
26 class _SHA1 extends HashBase implements SHA1 { | 41 class _SHA1Sink extends HashSink { |
27 final digest = new Uint32List(5); | 42 final digest = new Uint32List(5); |
28 | 43 |
29 /// The sixteen words from the original chunk, extended to 80 words. | 44 /// The sixteen words from the original chunk, extended to 80 words. |
30 /// | 45 /// |
31 /// This is an instance variable to avoid re-allocating, but its data isn't | 46 /// This is an instance variable to avoid re-allocating, but its data isn't |
32 /// used across invocations of [updateHash]. | 47 /// used across invocations of [updateHash]. |
33 final Uint32List _extended; | 48 final Uint32List _extended; |
34 | 49 |
35 _SHA1() | 50 _SHA1Sink(Sink<Digest> sink) |
36 : _extended = new Uint32List(80), | 51 : _extended = new Uint32List(80), |
37 super(16) { | 52 super(sink, 16) { |
38 digest[0] = 0x67452301; | 53 digest[0] = 0x67452301; |
39 digest[1] = 0xEFCDAB89; | 54 digest[1] = 0xEFCDAB89; |
40 digest[2] = 0x98BADCFE; | 55 digest[2] = 0x98BADCFE; |
41 digest[3] = 0x10325476; | 56 digest[3] = 0x10325476; |
42 digest[4] = 0xC3D2E1F0; | 57 digest[4] = 0xC3D2E1F0; |
43 } | 58 } |
44 | 59 |
45 SHA1 newInstance() => new _SHA1(); | |
46 | |
47 void updateHash(Uint32List chunk) { | 60 void updateHash(Uint32List chunk) { |
48 assert(chunk.length == 16); | 61 assert(chunk.length == 16); |
49 | 62 |
50 var a = digest[0]; | 63 var a = digest[0]; |
51 var b = digest[1]; | 64 var b = digest[1]; |
52 var c = digest[2]; | 65 var c = digest[2]; |
53 var d = digest[3]; | 66 var d = digest[3]; |
54 var e = digest[4]; | 67 var e = digest[4]; |
55 | 68 |
56 for (var i = 0; i < 80; i++) { | 69 for (var i = 0; i < 80; i++) { |
(...skipping 24 matching lines...) Expand all Loading... |
81 a = newA & mask32; | 94 a = newA & mask32; |
82 } | 95 } |
83 | 96 |
84 digest[0] = add32(a, digest[0]); | 97 digest[0] = add32(a, digest[0]); |
85 digest[1] = add32(b, digest[1]); | 98 digest[1] = add32(b, digest[1]); |
86 digest[2] = add32(c, digest[2]); | 99 digest[2] = add32(c, digest[2]); |
87 digest[3] = add32(d, digest[3]); | 100 digest[3] = add32(d, digest[3]); |
88 digest[4] = add32(e, digest[4]); | 101 digest[4] = add32(e, digest[4]); |
89 } | 102 } |
90 } | 103 } |
OLD | NEW |