53 lines
1.1 KiB
JavaScript
53 lines
1.1 KiB
JavaScript
const Transform = require('readable-stream').Transform
|
|
|
|
module.exports = class Blake extends Transform {
|
|
constructor (engine, options) {
|
|
super(options)
|
|
|
|
this._engine = engine
|
|
this._finalized = false
|
|
}
|
|
|
|
_transform (chunk, encoding, callback) {
|
|
let error = null
|
|
try {
|
|
this.update(chunk, encoding)
|
|
} catch (err) {
|
|
error = err
|
|
}
|
|
|
|
callback(error)
|
|
}
|
|
|
|
_flush (callback) {
|
|
let error = null
|
|
try {
|
|
this.push(this.digest())
|
|
} catch (err) {
|
|
error = err
|
|
}
|
|
|
|
callback(error)
|
|
}
|
|
|
|
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('Digest already called')
|
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding)
|
|
|
|
this._engine.update(data)
|
|
|
|
return this
|
|
}
|
|
|
|
digest (encoding) {
|
|
if (this._finalized) throw new Error('Digest already called')
|
|
this._finalized = true
|
|
|
|
let digest = this._engine.digest()
|
|
if (encoding !== undefined) digest = digest.toString(encoding)
|
|
|
|
return digest
|
|
}
|
|
}
|