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 import 'dart:typed_data'; | 5 import 'dart:typed_data'; |
6 | 6 |
7 import 'package:typed_data/typed_data.dart'; | 7 import 'package:typed_data/typed_data.dart'; |
8 | 8 |
9 import 'digest.dart'; | 9 import 'digest.dart'; |
10 import 'utils.dart'; | 10 import 'utils.dart'; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 // land cleanly on a chunk boundary. | 120 // land cleanly on a chunk boundary. |
121 _pendingData.add(0x80); | 121 _pendingData.add(0x80); |
122 var contentsLength = _lengthInBytes + 9; | 122 var contentsLength = _lengthInBytes + 9; |
123 var finalizedLength = _roundUp(contentsLength, _currentChunk.lengthInBytes); | 123 var finalizedLength = _roundUp(contentsLength, _currentChunk.lengthInBytes); |
124 for (var i = 0; i < finalizedLength - contentsLength; i++) { | 124 for (var i = 0; i < finalizedLength - contentsLength; i++) { |
125 _pendingData.add(0); | 125 _pendingData.add(0); |
126 } | 126 } |
127 | 127 |
128 if (_lengthInBytes > _maxMessageLengthInBytes) { | 128 if (_lengthInBytes > _maxMessageLengthInBytes) { |
129 throw new UnsupportedError( | 129 throw new UnsupportedError( |
130 "Hashing is unsupported for messages with more than 2^64 bits."); | 130 'Hashing is unsupported for messages with more than 2^64 bits.'); |
131 } | 131 } |
132 | 132 |
133 var lengthInBits = _lengthInBytes * bitsPerByte; | 133 var lengthInBits = _lengthInBytes * bitsPerByte; |
134 | 134 |
135 // Add the full length of the input data as a 64-bit value at the end of the | 135 // Add the full length of the input data as a 64-bit value at the end of the |
136 // hash. | 136 // hash. |
137 var offset = _pendingData.length; | 137 var offset = _pendingData.length; |
138 _pendingData.addAll(new Uint8List(8)); | 138 _pendingData.addAll(new Uint8List(8)); |
139 var byteData = _pendingData.buffer.asByteData(); | 139 var byteData = _pendingData.buffer.asByteData(); |
140 | 140 |
141 // We're essentially doing byteData.setUint64(offset, lengthInBits, _endian) | 141 // We're essentially doing byteData.setUint64(offset, lengthInBits, _endian) |
142 // here, but that method isn't supported on dart2js so we implement it | 142 // here, but that method isn't supported on dart2js so we implement it |
143 // manually instead. | 143 // manually instead. |
144 var highBits = lengthInBits >> 32; | 144 var highBits = lengthInBits >> 32; |
145 var lowBits = lengthInBits & mask32; | 145 var lowBits = lengthInBits & mask32; |
146 if (_endian == Endianness.BIG_ENDIAN) { | 146 if (_endian == Endianness.BIG_ENDIAN) { |
147 byteData.setUint32(offset, highBits, _endian); | 147 byteData.setUint32(offset, highBits, _endian); |
148 byteData.setUint32(offset + bytesPerWord, lowBits, _endian); | 148 byteData.setUint32(offset + bytesPerWord, lowBits, _endian); |
149 } else { | 149 } else { |
150 byteData.setUint32(offset, lowBits, _endian); | 150 byteData.setUint32(offset, lowBits, _endian); |
151 byteData.setUint32(offset + bytesPerWord, highBits, _endian); | 151 byteData.setUint32(offset + bytesPerWord, highBits, _endian); |
152 } | 152 } |
153 } | 153 } |
154 | 154 |
155 /// Rounds [val] up to the next multiple of [n], as long as [n] is a power of | 155 /// Rounds [val] up to the next multiple of [n], as long as [n] is a power of |
156 /// two. | 156 /// two. |
157 int _roundUp(int val, int n) => (val + n - 1) & -n; | 157 int _roundUp(int val, int n) => (val + n - 1) & -n; |
158 } | 158 } |
OLD | NEW |