UNPKG

@airgap/coinlib-core

Version:

The @airgap/coinlib-core is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.

75 lines (56 loc) 1.91 kB
'use strict' var Transform = require('stream').Transform var inherits = require('../../../inherits-2.0.4/inherits') module.exports = function (internal) { function Shake (rate, capacity, delimitedSuffix, options) { Transform.call(this, options) this._rate = rate this._capacity = capacity this._delimitedSuffix = delimitedSuffix this._options = options this._state = new internal.Keccak() this._state.initialize(rate, capacity) this._finalized = false } inherits(Shake, Transform) Shake.prototype._transform = function (chunk, encoding, callback) { var error = null try { this.update(chunk, encoding) } catch (err) { error = err } callback(error) } Shake.prototype._flush = function () {} Shake.prototype._read = function (size) { this.push(this.squeeze(size)) } Shake.prototype.update = function (data, encoding) { if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer') if (this._finalized) throw new Error('Squeeze already called') if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding) this._state.absorb(data) return this } Shake.prototype.squeeze = function (dataByteLength, encoding) { if (!this._finalized) { this._finalized = true this._state.absorbLastFewBits(this._delimitedSuffix) } var data = this._state.squeeze(dataByteLength) if (encoding !== undefined) data = data.toString(encoding) return data } Shake.prototype._resetState = function () { this._state.initialize(this._rate, this._capacity) return this } Shake.prototype._clone = function () { var clone = new Shake(this._rate, this._capacity, this._delimitedSuffix, this._options) this._state.copy(clone._state) clone._finalized = this._finalized return clone } return Shake }