Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1168)

Side by Side Diff: packages/crypto/lib/src/sha256.dart

Issue 2990843002: Removed fixed dependencies (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/crypto/lib/src/sha1.dart ('k') | packages/crypto/lib/src/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:convert';
6 import 'dart:typed_data';
7
8 import 'digest.dart';
9 import 'hash.dart';
10 import 'hash_sink.dart';
11 import 'utils.dart';
12
13 /// An instance of [Sha256].
14 ///
15 /// This instance provides convenient access to the [Sha256][rfc] hash function.
16 ///
17 /// [rfc]: http://tools.ietf.org/html/rfc6234
18 final sha256 = new Sha256._();
19
20 /// An implementation of the [SHA-256][rfc] hash function.
21 ///
22 /// [rfc]: http://tools.ietf.org/html/rfc6234
23 ///
24 /// Note that it's almost always easier to use [sha256] rather than creating a
25 /// new instance.
26 class Sha256 extends Hash {
27 @override
28 final int blockSize = 16 * bytesPerWord;
29
30 Sha256._();
31
32 Sha256 newInstance() => new Sha256._();
33
34 @override
35 ByteConversionSink startChunkedConversion(Sink<Digest> sink) =>
36 new ByteConversionSink.from(new _Sha256Sink(sink));
37 }
38
39 /// Data from a non-linear function that functions as reproducible noise.
40 const List<int> _noise = const [
41 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, //
42 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
43 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
44 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
45 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
46 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
47 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
48 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
49 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
50 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
51 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
52 ];
53
54 /// The concrete implementation of [Sha256].
55 ///
56 /// This is separate so that it can extend [HashSink] without leaking additional
57 /// public members.
58 class _Sha256Sink extends HashSink {
59 @override
60 final digest = new Uint32List(8);
61
62 /// The sixteen words from the original chunk, extended to 64 words.
63 ///
64 /// This is an instance variable to avoid re-allocating, but its data isn't
65 /// used across invocations of [updateHash].
66 final Uint32List _extended;
67
68 _Sha256Sink(Sink<Digest> sink)
69 : _extended = new Uint32List(64),
70 super(sink, 16) {
71 // Initial value of the hash parts. First 32 bits of the fractional parts
72 // of the square roots of the first 8 prime numbers.
73 digest[0] = 0x6a09e667;
74 digest[1] = 0xbb67ae85;
75 digest[2] = 0x3c6ef372;
76 digest[3] = 0xa54ff53a;
77 digest[4] = 0x510e527f;
78 digest[5] = 0x9b05688c;
79 digest[6] = 0x1f83d9ab;
80 digest[7] = 0x5be0cd19;
81 }
82
83 // The following helper functions are taken directly from
84 // http://tools.ietf.org/html/rfc6234.
85
86 int _rotr32(int n, int x) => (x >> n) | ((x << (32 - n)) & mask32);
87 int _ch(int x, int y, int z) => (x & y) ^ ((~x & mask32) & z);
88 int _maj(int x, int y, int z) => (x & y) ^ (x & z) ^ (y & z);
89 int _bsig0(int x) => _rotr32(2, x) ^ _rotr32(13, x) ^ _rotr32(22, x);
90 int _bsig1(int x) => _rotr32(6, x) ^ _rotr32(11, x) ^ _rotr32(25, x);
91 int _ssig0(int x) => _rotr32(7, x) ^ _rotr32(18, x) ^ (x >> 3);
92 int _ssig1(int x) => _rotr32(17, x) ^ _rotr32(19, x) ^ (x >> 10);
93
94 @override
95 void updateHash(Uint32List chunk) {
96 assert(chunk.length == 16);
97
98 // Prepare message schedule.
99 for (var i = 0; i < 16; i++) {
100 _extended[i] = chunk[i];
101 }
102 for (var i = 16; i < 64; i++) {
103 _extended[i] = add32(add32(_ssig1(_extended[i - 2]), _extended[i - 7]),
104 add32(_ssig0(_extended[i - 15]), _extended[i - 16]));
105 }
106
107 // Shuffle around the bits.
108 var a = digest[0];
109 var b = digest[1];
110 var c = digest[2];
111 var d = digest[3];
112 var e = digest[4];
113 var f = digest[5];
114 var g = digest[6];
115 var h = digest[7];
116
117 for (var i = 0; i < 64; i++) {
118 var temp1 = add32(add32(h, _bsig1(e)),
119 add32(_ch(e, f, g), add32(_noise[i], _extended[i])));
120 var temp2 = add32(_bsig0(a), _maj(a, b, c));
121 h = g;
122 g = f;
123 f = e;
124 e = add32(d, temp1);
125 d = c;
126 c = b;
127 b = a;
128 a = add32(temp1, temp2);
129 }
130
131 // Update hash values after iteration.
132 digest[0] = add32(a, digest[0]);
133 digest[1] = add32(b, digest[1]);
134 digest[2] = add32(c, digest[2]);
135 digest[3] = add32(d, digest[3]);
136 digest[4] = add32(e, digest[4]);
137 digest[5] = add32(f, digest[5]);
138 digest[6] = add32(g, digest[6]);
139 digest[7] = add32(h, digest[7]);
140 }
141 }
OLDNEW
« no previous file with comments | « packages/crypto/lib/src/sha1.dart ('k') | packages/crypto/lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698