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.sha256; | 5 library crypto.sha256; |
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 [SHA256]. |
| 16 /// |
| 17 /// This instance provides convenient access to the [SHA256][rfc] hash function. |
| 18 /// |
| 19 /// [rfc]: http://tools.ietf.org/html/rfc6234 |
| 20 final sha256 = new SHA256(); |
| 21 |
13 /// An implementation of the [SHA-256][rfc] hash function. | 22 /// An implementation of the [SHA-256][rfc] hash function. |
14 /// | 23 /// |
15 /// [rfc]: http://tools.ietf.org/html/rfc6234 | 24 /// [rfc]: http://tools.ietf.org/html/rfc6234 |
16 abstract class SHA256 implements Hash { | 25 /// |
17 factory SHA256() = _SHA256; | 26 /// Note that it's almost always easier to use [sha256] rather than creating a |
| 27 /// new instance. |
| 28 class SHA256 extends Hash { |
| 29 final int blockSize = 16 * bytesPerWord; |
18 | 30 |
19 SHA256 newInstance(); | 31 @Deprecated("Use the sha256 field instead.") |
| 32 SHA256(); |
| 33 |
| 34 SHA256 newInstance() => new SHA256(); |
| 35 |
| 36 ByteConversionSink startChunkedConversion(Sink<Digest> sink) => |
| 37 new ByteConversionSink.from(new _SHA256Sink(sink)); |
20 } | 38 } |
21 | 39 |
22 /// Data from a non-linear function that functions as reproducible noise. | 40 /// Data from a non-linear function that functions as reproducible noise. |
23 const List<int> _noise = const [ | 41 const List<int> _noise = const [ |
24 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, | 42 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, |
25 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, | 43 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
26 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, | 44 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, |
27 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, | 45 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
28 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, | 46 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, |
29 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, | 47 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
30 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, | 48 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, |
31 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, | 49 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
32 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, | 50 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, |
33 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, | 51 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
34 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 | 52 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
35 ]; | 53 ]; |
36 | 54 |
37 /// The concrete implementation of [SHA256]. | 55 /// The concrete implementation of [SHA256]. |
38 /// | 56 /// |
39 /// This is separate so that it can extend [HashBase] without leaking additional | 57 /// This is separate so that it can extend [HashBase] without leaking additional |
40 /// public memebers. | 58 /// public memebers. |
41 class _SHA256 extends HashBase implements SHA256 { | 59 class _SHA256Sink extends HashSink { |
42 final digest = new Uint32List(8); | 60 final digest = new Uint32List(8); |
43 | 61 |
44 /// The sixteen words from the original chunk, extended to 64 words. | 62 /// The sixteen words from the original chunk, extended to 64 words. |
45 /// | 63 /// |
46 /// This is an instance variable to avoid re-allocating, but its data isn't | 64 /// This is an instance variable to avoid re-allocating, but its data isn't |
47 /// used across invocations of [updateHash]. | 65 /// used across invocations of [updateHash]. |
48 final Uint32List _extended; | 66 final Uint32List _extended; |
49 | 67 |
50 _SHA256() | 68 _SHA256Sink(Sink<Digest> sink) |
51 : _extended = new Uint32List(64), | 69 : _extended = new Uint32List(64), |
52 super(16) { | 70 super(sink, 16) { |
53 // Initial value of the hash parts. First 32 bits of the fractional parts | 71 // Initial value of the hash parts. First 32 bits of the fractional parts |
54 // of the square roots of the first 8 prime numbers. | 72 // of the square roots of the first 8 prime numbers. |
55 digest[0] = 0x6a09e667; | 73 digest[0] = 0x6a09e667; |
56 digest[1] = 0xbb67ae85; | 74 digest[1] = 0xbb67ae85; |
57 digest[2] = 0x3c6ef372; | 75 digest[2] = 0x3c6ef372; |
58 digest[3] = 0xa54ff53a; | 76 digest[3] = 0xa54ff53a; |
59 digest[4] = 0x510e527f; | 77 digest[4] = 0x510e527f; |
60 digest[5] = 0x9b05688c; | 78 digest[5] = 0x9b05688c; |
61 digest[6] = 0x1f83d9ab; | 79 digest[6] = 0x1f83d9ab; |
62 digest[7] = 0x5be0cd19; | 80 digest[7] = 0x5be0cd19; |
63 } | 81 } |
64 | 82 |
65 SHA256 newInstance() => new _SHA256(); | |
66 | |
67 // The following helper functions are taken directly from | 83 // The following helper functions are taken directly from |
68 // http://tools.ietf.org/html/rfc6234. | 84 // http://tools.ietf.org/html/rfc6234. |
69 | 85 |
70 _rotr32(int n, int x) => (x >> n) | ((x << (32 - n)) & mask32); | 86 _rotr32(int n, int x) => (x >> n) | ((x << (32 - n)) & mask32); |
71 _ch(int x, int y, int z) => (x & y) ^ ((~x & mask32) & z); | 87 _ch(int x, int y, int z) => (x & y) ^ ((~x & mask32) & z); |
72 _maj(int x, int y, int z) => (x & y) ^ (x & z) ^ (y & z); | 88 _maj(int x, int y, int z) => (x & y) ^ (x & z) ^ (y & z); |
73 _bsig0(int x) => _rotr32(2, x) ^ _rotr32(13, x) ^ _rotr32(22, x); | 89 _bsig0(int x) => _rotr32(2, x) ^ _rotr32(13, x) ^ _rotr32(22, x); |
74 _bsig1(int x) => _rotr32(6, x) ^ _rotr32(11, x) ^ _rotr32(25, x); | 90 _bsig1(int x) => _rotr32(6, x) ^ _rotr32(11, x) ^ _rotr32(25, x); |
75 _ssig0(int x) => _rotr32(7, x) ^ _rotr32(18, x) ^ (x >> 3); | 91 _ssig0(int x) => _rotr32(7, x) ^ _rotr32(18, x) ^ (x >> 3); |
76 _ssig1(int x) => _rotr32(17, x) ^ _rotr32(19, x) ^ (x >> 10); | 92 _ssig1(int x) => _rotr32(17, x) ^ _rotr32(19, x) ^ (x >> 10); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 digest[0] = add32(a, digest[0]); | 135 digest[0] = add32(a, digest[0]); |
120 digest[1] = add32(b, digest[1]); | 136 digest[1] = add32(b, digest[1]); |
121 digest[2] = add32(c, digest[2]); | 137 digest[2] = add32(c, digest[2]); |
122 digest[3] = add32(d, digest[3]); | 138 digest[3] = add32(d, digest[3]); |
123 digest[4] = add32(e, digest[4]); | 139 digest[4] = add32(e, digest[4]); |
124 digest[5] = add32(f, digest[5]); | 140 digest[5] = add32(f, digest[5]); |
125 digest[6] = add32(g, digest[6]); | 141 digest[6] = add32(g, digest[6]); |
126 digest[7] = add32(h, digest[7]); | 142 digest[7] = add32(h, digest[7]); |
127 } | 143 } |
128 } | 144 } |
OLD | NEW |