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

Side by Side Diff: packages/crypto/lib/src/sha1.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/md5.dart ('k') | packages/crypto/lib/src/sha256.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 [Sha1].
14 ///
15 /// This instance provides convenient access to the [SHA-1][rfc] hash function.
16 ///
17 /// [rfc]: http://tools.ietf.org/html/rfc3174
18 final sha1 = new Sha1._();
19
20 /// An implementation of the [SHA-1][rfc] hash function.
21 ///
22 /// [rfc]: http://tools.ietf.org/html/rfc3174
23 class Sha1 extends Hash {
24 @override
25 final int blockSize = 16 * bytesPerWord;
26
27 Sha1._();
28
29 @override
30 ByteConversionSink startChunkedConversion(Sink<Digest> sink) =>
31 new ByteConversionSink.from(new _Sha1Sink(sink));
32 }
33
34 /// The concrete implementation of [Sha1].
35 ///
36 /// This is separate so that it can extend [HashSink] without leaking additional
37 /// public memebers.
38 class _Sha1Sink extends HashSink {
39 @override
40 final digest = new Uint32List(5);
41
42 /// The sixteen words from the original chunk, extended to 80 words.
43 ///
44 /// This is an instance variable to avoid re-allocating, but its data isn't
45 /// used across invocations of [updateHash].
46 final Uint32List _extended;
47
48 _Sha1Sink(Sink<Digest> sink)
49 : _extended = new Uint32List(80),
50 super(sink, 16) {
51 digest[0] = 0x67452301;
52 digest[1] = 0xEFCDAB89;
53 digest[2] = 0x98BADCFE;
54 digest[3] = 0x10325476;
55 digest[4] = 0xC3D2E1F0;
56 }
57
58 @override
59 void updateHash(Uint32List chunk) {
60 assert(chunk.length == 16);
61
62 var a = digest[0];
63 var b = digest[1];
64 var c = digest[2];
65 var d = digest[3];
66 var e = digest[4];
67
68 for (var i = 0; i < 80; i++) {
69 if (i < 16) {
70 _extended[i] = chunk[i];
71 } else {
72 _extended[i] = rotl32(
73 _extended[i - 3] ^
74 _extended[i - 8] ^
75 _extended[i - 14] ^
76 _extended[i - 16],
77 1);
78 }
79
80 var newA = add32(add32(rotl32(a, 5), e), _extended[i]);
81 if (i < 20) {
82 newA = add32(add32(newA, (b & c) | (~b & d)), 0x5A827999);
83 } else if (i < 40) {
84 newA = add32(add32(newA, (b ^ c ^ d)), 0x6ED9EBA1);
85 } else if (i < 60) {
86 newA = add32(add32(newA, (b & c) | (b & d) | (c & d)), 0x8F1BBCDC);
87 } else {
88 newA = add32(add32(newA, b ^ c ^ d), 0xCA62C1D6);
89 }
90
91 e = d;
92 d = c;
93 c = rotl32(b, 30);
94 b = a;
95 a = newA & mask32;
96 }
97
98 digest[0] = add32(a, digest[0]);
99 digest[1] = add32(b, digest[1]);
100 digest[2] = add32(c, digest[2]);
101 digest[3] = add32(d, digest[3]);
102 digest[4] = add32(e, digest[4]);
103 }
104 }
OLDNEW
« no previous file with comments | « packages/crypto/lib/src/md5.dart ('k') | packages/crypto/lib/src/sha256.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698