Current Folder
This commit is contained in:
1 parent
8b3382289b
commit
cfcada880d
4379 files changed
No files matched your search
+68
@@ -0,0 +1,68 @@
|
||||
const { Transform } = require('stream')
|
||||
|
||||
module.exports = (KeccakState) => class Shake extends Transform {
|
||||
constructor (rate, capacity, delimitedSuffix, options) {
|
||||
super(options)
|
||||
|
||||
this._rate = rate
|
||||
this._capacity = capacity
|
||||
this._delimitedSuffix = delimitedSuffix
|
||||
this._options = options
|
||||
|
||||
this._state = new KeccakState()
|
||||
this._state.initialize(rate, capacity)
|
||||
this._finalized = false
|
||||
}
|
||||
|
||||
_transform (chunk, encoding, callback) {
|
||||
let error = null
|
||||
try {
|
||||
this.update(chunk, encoding)
|
||||
} catch (err) {
|
||||
error = err
|
||||
}
|
||||
|
||||
callback(error)
|
||||
}
|
||||
|
||||
_flush () {}
|
||||
|
||||
_read (size) {
|
||||
this.push(this.squeeze(size))
|
||||
}
|
||||
|
||||
update (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
|
||||
}
|
||||
|
||||
squeeze (dataByteLength, encoding) {
|
||||
if (!this._finalized) {
|
||||
this._finalized = true
|
||||
this._state.absorbLastFewBits(this._delimitedSuffix)
|
||||
}
|
||||
|
||||
let data = this._state.squeeze(dataByteLength)
|
||||
if (encoding !== undefined) data = data.toString(encoding)
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
_resetState () {
|
||||
this._state.initialize(this._rate, this._capacity)
|
||||
return this
|
||||
}
|
||||
|
||||
_clone () {
|
||||
const clone = new Shake(this._rate, this._capacity, this._delimitedSuffix, this._options)
|
||||
this._state.copy(clone._state)
|
||||
clone._finalized = this._finalized
|
||||
|
||||
return clone
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user