|
| 1 | +//This class was primarily influenced by: |
| 2 | +// Copyright 2015, the Dart project authors. |
| 3 | +// |
| 4 | +// Redistribution and use in source and binary forms, with or without |
| 5 | +// modification, are permitted provided that the following conditions are |
| 6 | +// met: |
| 7 | +// |
| 8 | +// * Redistributions of source code must retain the above copyright |
| 9 | +// notice, this list of conditions and the following disclaimer. |
| 10 | +// * Redistributions in binary form must reproduce the above |
| 11 | +// copyright notice, this list of conditions and the following |
| 12 | +// disclaimer in the documentation and/or other materials provided |
| 13 | +// with the distribution. |
| 14 | +// * Neither the name of Google LLC nor the names of its |
| 15 | +// contributors may be used to endorse or promote products derived |
| 16 | +// from this software without specific prior written permission. |
| 17 | +// |
| 18 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +import 'dart:typed_data'; |
| 30 | + |
| 31 | +import 'package:codec_utils/src/utils/sha/hash/digest.dart'; |
| 32 | +import 'package:typed_data/typed_buffers.dart'; |
| 33 | + |
| 34 | +/// [AHashSink] is a base class for processing streaming hash input efficiently. It handles incoming data in chunks, manages buffering, and ensures correct endian formatting. |
| 35 | +abstract class AHashSink implements Sink<List<int>> { |
| 36 | + static const int bitsPerByte = 8; |
| 37 | + static const int bytesPerWord = 4; |
| 38 | + static const int mask32 = 0xFFFFFFFF; |
| 39 | + |
| 40 | + final Sink<Digest> _sink; |
| 41 | + final Endian _endian; |
| 42 | + final Uint32List _currentUint8List; |
| 43 | + final int _signatureBytes; |
| 44 | + final Uint8Buffer _pendingUint8Buffer = Uint8Buffer(); |
| 45 | + |
| 46 | + int _lengthInBytes = 0; |
| 47 | + bool _closedBool = false; |
| 48 | + |
| 49 | + AHashSink(this._sink, int chunkSize, {Endian endian = Endian.big, int signaturesBytes = 8}) |
| 50 | + : _endian = endian, |
| 51 | + _signatureBytes = signaturesBytes, |
| 52 | + _currentUint8List = Uint32List(chunkSize); |
| 53 | + |
| 54 | + @override |
| 55 | + void add(List<int> dataList) { |
| 56 | + if (_closedBool) { |
| 57 | + return; |
| 58 | + } |
| 59 | + _lengthInBytes += dataList.length; |
| 60 | + _pendingUint8Buffer.addAll(dataList); |
| 61 | + _applyIterate(); |
| 62 | + } |
| 63 | + |
| 64 | + @override |
| 65 | + void close() { |
| 66 | + if (_closedBool) { |
| 67 | + return; |
| 68 | + } |
| 69 | + _closedBool = true; |
| 70 | + _finalizeData(); |
| 71 | + _applyIterate(); |
| 72 | + _sink |
| 73 | + ..add(Digest(_applyByteDigest())) |
| 74 | + ..close(); |
| 75 | + } |
| 76 | + |
| 77 | + Uint32List get digestUint32List; |
| 78 | + |
| 79 | + void updateHash(Uint32List inputUint32List); |
| 80 | + |
| 81 | + void _applyIterate() { |
| 82 | + ByteData pendingByteData = _pendingUint8Buffer.buffer.asByteData(); |
| 83 | + int pendingData = _pendingUint8Buffer.length ~/ _currentUint8List.lengthInBytes; |
| 84 | + |
| 85 | + for (int i = 0; i < pendingData; i++) { |
| 86 | + for (int j = 0; j < _currentUint8List.length; j++) { |
| 87 | + _currentUint8List[j] = pendingByteData.getUint32(i * _currentUint8List.lengthInBytes + j * bytesPerWord, _endian); |
| 88 | + } |
| 89 | + updateHash(_currentUint8List); |
| 90 | + } |
| 91 | + |
| 92 | + _pendingUint8Buffer.removeRange(0, pendingData * _currentUint8List.lengthInBytes); |
| 93 | + } |
| 94 | + |
| 95 | + Uint8List _applyByteDigest() { |
| 96 | + if (_endian == Endian.host) { |
| 97 | + return digestUint32List.buffer.asUint8List(); |
| 98 | + } |
| 99 | + Uint32List cacheUint32List = digestUint32List; |
| 100 | + Uint8List digestUint8List = Uint8List(cacheUint32List.lengthInBytes); |
| 101 | + ByteData currentByteData = digestUint8List.buffer.asByteData(); |
| 102 | + for (int i = 0; i < cacheUint32List.length; i++) { |
| 103 | + currentByteData.setUint32(i * bytesPerWord, cacheUint32List[i]); |
| 104 | + } |
| 105 | + return digestUint8List; |
| 106 | + } |
| 107 | + |
| 108 | + void _finalizeData() { |
| 109 | + _pendingUint8Buffer.add(0x80); |
| 110 | + int contentsLength = _lengthInBytes + 1 + _signatureBytes; |
| 111 | + int finalizedLength = _applyRoundUp(contentsLength, _currentUint8List.lengthInBytes); |
| 112 | + |
| 113 | + for (int i = 0; i < finalizedLength - contentsLength; i++) { |
| 114 | + _pendingUint8Buffer.add(0); |
| 115 | + } |
| 116 | + |
| 117 | + int lengthInBits = _lengthInBytes * bitsPerByte; |
| 118 | + int offsetOutput = _pendingUint8Buffer.length + (_signatureBytes - 8); |
| 119 | + |
| 120 | + _pendingUint8Buffer.addAll(Uint8List(_signatureBytes)); |
| 121 | + |
| 122 | + ByteData currentByteData = _pendingUint8Buffer.buffer.asByteData(); |
| 123 | + int highBits = lengthInBits ~/ 0x100000000; |
| 124 | + int lowBits = lengthInBits & mask32; |
| 125 | + |
| 126 | + if (_endian == Endian.big) { |
| 127 | + currentByteData |
| 128 | + ..setUint32(offsetOutput, highBits, _endian) |
| 129 | + ..setUint32(offsetOutput + bytesPerWord, lowBits, _endian); |
| 130 | + } else { |
| 131 | + currentByteData |
| 132 | + ..setUint32(offsetOutput, lowBits, _endian) |
| 133 | + ..setUint32(offsetOutput + bytesPerWord, highBits, _endian); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + int _applyRoundUp(int value, int multiple) { |
| 138 | + return (value + multiple - 1) & -multiple; |
| 139 | + } |
| 140 | +} |
0 commit comments