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

Side by Side Diff: mojo/public/dart/third_party/crypto/lib/src/hash_utils.dart

Issue 1346773002: Stop running pub get at gclient sync time and fix build bugs (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 3 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
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 part of crypto;
6
7 // Constants.
8 const _MASK_8 = 0xff;
9 const _MASK_32 = 0xffffffff;
10 const _BITS_PER_BYTE = 8;
11 const _BYTES_PER_WORD = 4;
12
13 // Helper functions used by more than one hasher.
14
15 // Rotate left limiting to unsigned 32-bit values.
16 int _rotl32(int val, int shift) {
17 var mod_shift = shift & 31;
18 return ((val << mod_shift) & _MASK_32) |
19 ((val & _MASK_32) >> (32 - mod_shift));
20 }
21
22 // Base class encapsulating common behavior for cryptographic hash
23 // functions.
24 abstract class _HashBase implements Hash {
25 final int _chunkSizeInWords;
26 final int _digestSizeInWords;
27 final bool _bigEndianWords;
28 final Uint32List _currentChunk;
29 final Uint32List _h;
30 int _lengthInBytes = 0;
31 List<int> _pendingData;
32 bool _digestCalled = false;
33
34 _HashBase(int chunkSizeInWords,
35 int digestSizeInWords,
36 bool this._bigEndianWords)
37 : _pendingData = [],
38 _currentChunk = new Uint32List(chunkSizeInWords),
39 _h = new Uint32List(digestSizeInWords),
40 _chunkSizeInWords = chunkSizeInWords,
41 _digestSizeInWords = digestSizeInWords;
42
43 // Update the hasher with more data.
44 void add(List<int> data) {
45 if (_digestCalled) {
46 throw new StateError(
47 'Hash update method called after digest was retrieved');
48 }
49 _lengthInBytes += data.length;
50 _pendingData.addAll(data);
51 _iterate();
52 }
53
54 // Finish the hash computation and return the digest string.
55 List<int> close() {
56 if (_digestCalled) {
57 return _resultAsBytes();
58 }
59 _digestCalled = true;
60 _finalizeData();
61 _iterate();
62 assert(_pendingData.length == 0);
63 return _resultAsBytes();
64 }
65
66 // Returns the block size of the hash in bytes.
67 int get blockSize {
68 return _chunkSizeInWords * _BYTES_PER_WORD;
69 }
70
71 // One round of the hash computation.
72 void _updateHash(Uint32List m);
73
74 // Helper methods.
75 int _add32(x, y) => (x + y) & _MASK_32;
76 int _roundUp(val, n) => (val + n - 1) & -n;
77
78 // Compute the final result as a list of bytes from the hash words.
79 List<int> _resultAsBytes() {
80 var result = [];
81 for (var i = 0; i < _h.length; i++) {
82 result.addAll(_wordToBytes(_h[i]));
83 }
84 return result;
85 }
86
87 // Converts a list of bytes to a chunk of 32-bit words.
88 void _bytesToChunk(List<int> data, int dataIndex) {
89 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD));
90
91 for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) {
92 var w3 = _bigEndianWords ? data[dataIndex] : data[dataIndex + 3];
93 var w2 = _bigEndianWords ? data[dataIndex + 1] : data[dataIndex + 2];
94 var w1 = _bigEndianWords ? data[dataIndex + 2] : data[dataIndex + 1];
95 var w0 = _bigEndianWords ? data[dataIndex + 3] : data[dataIndex];
96 dataIndex += 4;
97 var word = (w3 & 0xff) << 24;
98 word |= (w2 & _MASK_8) << 16;
99 word |= (w1 & _MASK_8) << 8;
100 word |= (w0 & _MASK_8);
101 _currentChunk[wordIndex] = word;
102 }
103 }
104
105 // Convert a 32-bit word to four bytes.
106 List<int> _wordToBytes(int word) {
107 List bytes = new List<int>(_BYTES_PER_WORD);
108 bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & _MASK_8;
109 bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & _MASK_8;
110 bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & _MASK_8;
111 bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & _MASK_8;
112 return bytes;
113 }
114
115 // Iterate through data updating the hash computation for each
116 // chunk.
117 void _iterate() {
118 var len = _pendingData.length;
119 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD;
120 if (len >= chunkSizeInBytes) {
121 var index = 0;
122 for (; (len - index) >= chunkSizeInBytes; index += chunkSizeInBytes) {
123 _bytesToChunk(_pendingData, index);
124 _updateHash(_currentChunk);
125 }
126 _pendingData = _pendingData.sublist(index, len);
127 }
128 }
129
130 // Finalize the data. Add a 1 bit to the end of the message. Expand with
131 // 0 bits and add the length of the message.
132 void _finalizeData() {
133 _pendingData.add(0x80);
134 var contentsLength = _lengthInBytes + 9;
135 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD;
136 var finalizedLength = _roundUp(contentsLength, chunkSizeInBytes);
137 var zeroPadding = finalizedLength - contentsLength;
138 for (var i = 0; i < zeroPadding; i++) {
139 _pendingData.add(0);
140 }
141 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE;
142 assert(lengthInBits < pow(2, 32));
143 if (_bigEndianWords) {
144 _pendingData.addAll(_wordToBytes(0));
145 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
146 } else {
147 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
148 _pendingData.addAll(_wordToBytes(0));
149 }
150 }
151 }
OLDNEW
« no previous file with comments | « mojo/public/dart/third_party/crypto/lib/src/crypto_utils.dart ('k') | mojo/public/dart/third_party/crypto/lib/src/hmac.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698