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

Side by Side Diff: lib/src/hash_utils.dart

Issue 1350933002: Stop using parts. (Closed) Base URL: git@github.com:dart-lang/crypto.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(
35 int chunkSizeInWords, int digestSizeInWords, bool this._bigEndianWords)
36 : _pendingData = [],
37 _currentChunk = new Uint32List(chunkSizeInWords),
38 _h = new Uint32List(digestSizeInWords),
39 _chunkSizeInWords = chunkSizeInWords,
40 _digestSizeInWords = digestSizeInWords;
41
42 // Update the hasher with more data.
43 void add(List<int> data) {
44 if (_digestCalled) {
45 throw new StateError(
46 'Hash update method called after digest was retrieved');
47 }
48 _lengthInBytes += data.length;
49 _pendingData.addAll(data);
50 _iterate();
51 }
52
53 // Finish the hash computation and return the digest string.
54 List<int> close() {
55 if (_digestCalled) {
56 return _resultAsBytes();
57 }
58 _digestCalled = true;
59 _finalizeData();
60 _iterate();
61 assert(_pendingData.length == 0);
62 return _resultAsBytes();
63 }
64
65 // Returns the block size of the hash in bytes.
66 int get blockSize {
67 return _chunkSizeInWords * _BYTES_PER_WORD;
68 }
69
70 // One round of the hash computation.
71 void _updateHash(Uint32List m);
72
73 // Helper methods.
74 int _add32(x, y) => (x + y) & _MASK_32;
75 int _roundUp(val, n) => (val + n - 1) & -n;
76
77 // Compute the final result as a list of bytes from the hash words.
78 List<int> _resultAsBytes() {
79 var result = [];
80 for (var i = 0; i < _h.length; i++) {
81 result.addAll(_wordToBytes(_h[i]));
82 }
83 return result;
84 }
85
86 // Converts a list of bytes to a chunk of 32-bit words.
87 void _bytesToChunk(List<int> data, int dataIndex) {
88 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD));
89
90 for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) {
91 var w3 = _bigEndianWords ? data[dataIndex] : data[dataIndex + 3];
92 var w2 = _bigEndianWords ? data[dataIndex + 1] : data[dataIndex + 2];
93 var w1 = _bigEndianWords ? data[dataIndex + 2] : data[dataIndex + 1];
94 var w0 = _bigEndianWords ? data[dataIndex + 3] : data[dataIndex];
95 dataIndex += 4;
96 var word = (w3 & 0xff) << 24;
97 word |= (w2 & _MASK_8) << 16;
98 word |= (w1 & _MASK_8) << 8;
99 word |= (w0 & _MASK_8);
100 _currentChunk[wordIndex] = word;
101 }
102 }
103
104 // Convert a 32-bit word to four bytes.
105 List<int> _wordToBytes(int word) {
106 List bytes = new List<int>(_BYTES_PER_WORD);
107 bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & _MASK_8;
108 bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & _MASK_8;
109 bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & _MASK_8;
110 bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & _MASK_8;
111 return bytes;
112 }
113
114 // Iterate through data updating the hash computation for each
115 // chunk.
116 void _iterate() {
117 var len = _pendingData.length;
118 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD;
119 if (len >= chunkSizeInBytes) {
120 var index = 0;
121 for (; (len - index) >= chunkSizeInBytes; index += chunkSizeInBytes) {
122 _bytesToChunk(_pendingData, index);
123 _updateHash(_currentChunk);
124 }
125 _pendingData = _pendingData.sublist(index, len);
126 }
127 }
128
129 // Finalize the data. Add a 1 bit to the end of the message. Expand with
130 // 0 bits and add the length of the message.
131 void _finalizeData() {
132 _pendingData.add(0x80);
133 var contentsLength = _lengthInBytes + 9;
134 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD;
135 var finalizedLength = _roundUp(contentsLength, chunkSizeInBytes);
136 var zeroPadding = finalizedLength - contentsLength;
137 for (var i = 0; i < zeroPadding; i++) {
138 _pendingData.add(0);
139 }
140 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE;
141 assert(lengthInBits < pow(2, 32));
142 if (_bigEndianWords) {
143 _pendingData.addAll(_wordToBytes(0));
144 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
145 } else {
146 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
147 _pendingData.addAll(_wordToBytes(0));
148 }
149 }
150 }
OLDNEW
« lib/src/hash_base.dart ('K') | « lib/src/hash_base.dart ('k') | lib/src/hmac.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698