@airgap/coinlib-core
Version:
The @airgap/coinlib-core is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.
84 lines (64 loc) • 2.22 kB
JavaScript
'use strict'
var Transform = require('stream').Transform
var inherits = require('../../../inherits-2.0.4/inherits')
module.exports = function (internal) {
function Keccak (rate, capacity, delimitedSuffix, hashBitLength, options) {
Transform.call(this, options)
this._rate = rate
this._capacity = capacity
this._delimitedSuffix = delimitedSuffix
this._hashBitLength = hashBitLength
this._options = options
this._state = new internal.Keccak()
this._state.initialize(rate, capacity)
this._finalized = false
}
inherits(Keccak, Transform)
Keccak.prototype._transform = function (chunk, encoding, callback) {
var error = null
try {
this.update(chunk, encoding)
} catch (err) {
error = err
}
callback(error)
}
Keccak.prototype._flush = function (callback) {
var error = null
try {
this.push(this.digest())
} catch (err) {
error = err
}
callback(error)
}
Keccak.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('Digest already called')
if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding)
this._state.absorb(data)
return this
}
Keccak.prototype.digest = function (encoding) {
if (this._finalized) throw new Error('Digest already called')
this._finalized = true
if (this._delimitedSuffix) this._state.absorbLastFewBits(this._delimitedSuffix)
var digest = this._state.squeeze(this._hashBitLength / 8)
if (encoding !== undefined) digest = digest.toString(encoding)
this._resetState()
return digest
}
// remove result from memory
Keccak.prototype._resetState = function () {
this._state.initialize(this._rate, this._capacity)
return this
}
// because sometimes we need hash right now and little later
Keccak.prototype._clone = function () {
var clone = new Keccak(this._rate, this._capacity, this._delimitedSuffix, this._hashBitLength, this._options)
this._state.copy(clone._state)
clone._finalized = this._finalized
return clone
}
return Keccak
}