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 part of crypto; | 5 library crypto.hash_base; |
6 | 6 |
7 // Constants. | 7 import 'dart:math' as math; |
8 const _MASK_8 = 0xff; | 8 import 'dart:typed_data'; |
9 const _MASK_32 = 0xffffffff; | |
10 const _BITS_PER_BYTE = 8; | |
11 const _BYTES_PER_WORD = 4; | |
12 | 9 |
13 // Helper functions used by more than one hasher. | 10 import 'hash.dart'; |
14 | 11 import 'utils.dart'; |
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 | 12 |
22 // Base class encapsulating common behavior for cryptographic hash | 13 // Base class encapsulating common behavior for cryptographic hash |
23 // functions. | 14 // functions. |
24 abstract class _HashBase implements Hash { | 15 abstract class HashBase implements Hash { |
25 final int _chunkSizeInWords; | 16 final int _chunkSizeInWords; |
26 final int _digestSizeInWords; | 17 final int _digestSizeInWords; |
27 final bool _bigEndianWords; | 18 final bool _bigEndianWords; |
28 final Uint32List _currentChunk; | 19 final Uint32List _currentChunk; |
29 final Uint32List _h; | 20 final Uint32List h; |
Bob Nystrom
2015/09/16 21:18:35
Is this now publicly exposed outside of the packag
nweiz
2015/09/16 21:21:33
Only private classes extend HashBase. Their public
| |
30 int _lengthInBytes = 0; | 21 int _lengthInBytes = 0; |
31 List<int> _pendingData; | 22 List<int> _pendingData; |
32 bool _digestCalled = false; | 23 bool _digestCalled = false; |
33 | 24 |
34 _HashBase( | 25 HashBase( |
35 int chunkSizeInWords, int digestSizeInWords, bool this._bigEndianWords) | 26 int chunkSizeInWords, int digestSizeInWords, bool this._bigEndianWords) |
36 : _pendingData = [], | 27 : _pendingData = [], |
37 _currentChunk = new Uint32List(chunkSizeInWords), | 28 _currentChunk = new Uint32List(chunkSizeInWords), |
38 _h = new Uint32List(digestSizeInWords), | 29 h = new Uint32List(digestSizeInWords), |
39 _chunkSizeInWords = chunkSizeInWords, | 30 _chunkSizeInWords = chunkSizeInWords, |
40 _digestSizeInWords = digestSizeInWords; | 31 _digestSizeInWords = digestSizeInWords; |
41 | 32 |
42 // Update the hasher with more data. | 33 // Update the hasher with more data. |
43 void add(List<int> data) { | 34 void add(List<int> data) { |
44 if (_digestCalled) { | 35 if (_digestCalled) { |
45 throw new StateError( | 36 throw new StateError( |
46 'Hash update method called after digest was retrieved'); | 37 'Hash update method called after digest was retrieved'); |
47 } | 38 } |
48 _lengthInBytes += data.length; | 39 _lengthInBytes += data.length; |
49 _pendingData.addAll(data); | 40 _pendingData.addAll(data); |
50 _iterate(); | 41 _iterate(); |
51 } | 42 } |
52 | 43 |
53 // Finish the hash computation and return the digest string. | 44 // Finish the hash computation and return the digest string. |
54 List<int> close() { | 45 List<int> close() { |
55 if (_digestCalled) { | 46 if (_digestCalled) { |
56 return _resultAsBytes(); | 47 return _resultAsBytes(); |
57 } | 48 } |
58 _digestCalled = true; | 49 _digestCalled = true; |
59 _finalizeData(); | 50 _finalizeData(); |
60 _iterate(); | 51 _iterate(); |
61 assert(_pendingData.length == 0); | 52 assert(_pendingData.length == 0); |
62 return _resultAsBytes(); | 53 return _resultAsBytes(); |
63 } | 54 } |
64 | 55 |
65 // Returns the block size of the hash in bytes. | 56 // Returns the block size of the hash in bytes. |
66 int get blockSize { | 57 int get blockSize { |
67 return _chunkSizeInWords * _BYTES_PER_WORD; | 58 return _chunkSizeInWords * BYTES_PER_WORD; |
68 } | 59 } |
69 | 60 |
70 // One round of the hash computation. | 61 // One round of the hash computation. |
71 void _updateHash(Uint32List m); | 62 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 | 63 |
77 // Compute the final result as a list of bytes from the hash words. | 64 // Compute the final result as a list of bytes from the hash words. |
78 List<int> _resultAsBytes() { | 65 List<int> _resultAsBytes() { |
79 var result = []; | 66 var result = []; |
80 for (var i = 0; i < _h.length; i++) { | 67 for (var i = 0; i < h.length; i++) { |
81 result.addAll(_wordToBytes(_h[i])); | 68 result.addAll(_wordToBytes(h[i])); |
82 } | 69 } |
83 return result; | 70 return result; |
84 } | 71 } |
85 | 72 |
86 // Converts a list of bytes to a chunk of 32-bit words. | 73 // Converts a list of bytes to a chunk of 32-bit words. |
87 void _bytesToChunk(List<int> data, int dataIndex) { | 74 void _bytesToChunk(List<int> data, int dataIndex) { |
88 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD)); | 75 assert((data.length - dataIndex) >= (_chunkSizeInWords * BYTES_PER_WORD)); |
89 | 76 |
90 for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) { | 77 for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) { |
91 var w3 = _bigEndianWords ? data[dataIndex] : data[dataIndex + 3]; | 78 var w3 = _bigEndianWords ? data[dataIndex] : data[dataIndex + 3]; |
92 var w2 = _bigEndianWords ? data[dataIndex + 1] : data[dataIndex + 2]; | 79 var w2 = _bigEndianWords ? data[dataIndex + 1] : data[dataIndex + 2]; |
93 var w1 = _bigEndianWords ? data[dataIndex + 2] : data[dataIndex + 1]; | 80 var w1 = _bigEndianWords ? data[dataIndex + 2] : data[dataIndex + 1]; |
94 var w0 = _bigEndianWords ? data[dataIndex + 3] : data[dataIndex]; | 81 var w0 = _bigEndianWords ? data[dataIndex + 3] : data[dataIndex]; |
95 dataIndex += 4; | 82 dataIndex += 4; |
96 var word = (w3 & 0xff) << 24; | 83 var word = (w3 & 0xff) << 24; |
97 word |= (w2 & _MASK_8) << 16; | 84 word |= (w2 & MASK_8) << 16; |
98 word |= (w1 & _MASK_8) << 8; | 85 word |= (w1 & MASK_8) << 8; |
99 word |= (w0 & _MASK_8); | 86 word |= (w0 & MASK_8); |
100 _currentChunk[wordIndex] = word; | 87 _currentChunk[wordIndex] = word; |
101 } | 88 } |
102 } | 89 } |
103 | 90 |
104 // Convert a 32-bit word to four bytes. | 91 // Convert a 32-bit word to four bytes. |
105 List<int> _wordToBytes(int word) { | 92 List<int> _wordToBytes(int word) { |
106 List bytes = new List<int>(_BYTES_PER_WORD); | 93 List bytes = new List<int>(BYTES_PER_WORD); |
107 bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & _MASK_8; | 94 bytes[0] = (word >> (_bigEndianWords ? 24 : 0)) & MASK_8; |
108 bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & _MASK_8; | 95 bytes[1] = (word >> (_bigEndianWords ? 16 : 8)) & MASK_8; |
109 bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & _MASK_8; | 96 bytes[2] = (word >> (_bigEndianWords ? 8 : 16)) & MASK_8; |
110 bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & _MASK_8; | 97 bytes[3] = (word >> (_bigEndianWords ? 0 : 24)) & MASK_8; |
111 return bytes; | 98 return bytes; |
112 } | 99 } |
113 | 100 |
114 // Iterate through data updating the hash computation for each | 101 // Iterate through data updating the hash computation for each |
115 // chunk. | 102 // chunk. |
116 void _iterate() { | 103 void _iterate() { |
117 var len = _pendingData.length; | 104 var len = _pendingData.length; |
118 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD; | 105 var chunkSizeInBytes = _chunkSizeInWords * BYTES_PER_WORD; |
119 if (len >= chunkSizeInBytes) { | 106 if (len >= chunkSizeInBytes) { |
120 var index = 0; | 107 var index = 0; |
121 for (; (len - index) >= chunkSizeInBytes; index += chunkSizeInBytes) { | 108 for (; (len - index) >= chunkSizeInBytes; index += chunkSizeInBytes) { |
122 _bytesToChunk(_pendingData, index); | 109 _bytesToChunk(_pendingData, index); |
123 _updateHash(_currentChunk); | 110 updateHash(_currentChunk); |
124 } | 111 } |
125 _pendingData = _pendingData.sublist(index, len); | 112 _pendingData = _pendingData.sublist(index, len); |
126 } | 113 } |
127 } | 114 } |
128 | 115 |
129 // Finalize the data. Add a 1 bit to the end of the message. Expand with | 116 // 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. | 117 // 0 bits and add the length of the message. |
131 void _finalizeData() { | 118 void _finalizeData() { |
132 _pendingData.add(0x80); | 119 _pendingData.add(0x80); |
133 var contentsLength = _lengthInBytes + 9; | 120 var contentsLength = _lengthInBytes + 9; |
134 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD; | 121 var chunkSizeInBytes = _chunkSizeInWords * BYTES_PER_WORD; |
135 var finalizedLength = _roundUp(contentsLength, chunkSizeInBytes); | 122 var finalizedLength = roundUp(contentsLength, chunkSizeInBytes); |
136 var zeroPadding = finalizedLength - contentsLength; | 123 var zeroPadding = finalizedLength - contentsLength; |
137 for (var i = 0; i < zeroPadding; i++) { | 124 for (var i = 0; i < zeroPadding; i++) { |
138 _pendingData.add(0); | 125 _pendingData.add(0); |
139 } | 126 } |
140 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE; | 127 var lengthInBits = _lengthInBytes * BITS_PER_BYTE; |
141 assert(lengthInBits < pow(2, 32)); | 128 assert(lengthInBits < math.pow(2, 32)); |
142 if (_bigEndianWords) { | 129 if (_bigEndianWords) { |
143 _pendingData.addAll(_wordToBytes(0)); | 130 _pendingData.addAll(_wordToBytes(0)); |
144 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); | 131 _pendingData.addAll(_wordToBytes(lengthInBits & MASK_32)); |
145 } else { | 132 } else { |
146 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); | 133 _pendingData.addAll(_wordToBytes(lengthInBits & MASK_32)); |
147 _pendingData.addAll(_wordToBytes(0)); | 134 _pendingData.addAll(_wordToBytes(0)); |
148 } | 135 } |
149 } | 136 } |
150 } | 137 } |
OLD | NEW |