Beta 7 with some repo names changed.
This commit is contained in:
1 parent
0f7b40cca7
commit
963c19493a
3408 files changed
+462217
-15516
No files matched your search
+27
@@ -0,0 +1,27 @@
|
||||
const createKeccak = require('./keccak')
|
||||
const createShake = require('./shake')
|
||||
|
||||
module.exports = function (KeccakState) {
|
||||
const Keccak = createKeccak(KeccakState)
|
||||
const Shake = createShake(KeccakState)
|
||||
|
||||
return function (algorithm, options) {
|
||||
const hash = typeof algorithm === 'string' ? algorithm.toLowerCase() : algorithm
|
||||
switch (hash) {
|
||||
case 'keccak224': return new Keccak(1152, 448, null, 224, options)
|
||||
case 'keccak256': return new Keccak(1088, 512, null, 256, options)
|
||||
case 'keccak384': return new Keccak(832, 768, null, 384, options)
|
||||
case 'keccak512': return new Keccak(576, 1024, null, 512, options)
|
||||
|
||||
case 'sha3-224': return new Keccak(1152, 448, 0x06, 224, options)
|
||||
case 'sha3-256': return new Keccak(1088, 512, 0x06, 256, options)
|
||||
case 'sha3-384': return new Keccak(832, 768, 0x06, 384, options)
|
||||
case 'sha3-512': return new Keccak(576, 1024, 0x06, 512, options)
|
||||
|
||||
case 'shake128': return new Shake(1344, 256, 0x1f, options)
|
||||
case 'shake256': return new Shake(1088, 512, 0x1f, options)
|
||||
|
||||
default: throw new Error('Invald algorithm: ' + algorithm)
|
||||
}
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
const { Transform } = require('stream')
|
||||
|
||||
module.exports = (KeccakState) => class Keccak extends Transform {
|
||||
constructor (rate, capacity, delimitedSuffix, hashBitLength, options) {
|
||||
super(options)
|
||||
|
||||
this._rate = rate
|
||||
this._capacity = capacity
|
||||
this._delimitedSuffix = delimitedSuffix
|
||||
this._hashBitLength = hashBitLength
|
||||
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 (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._state.absorb(data)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
digest (encoding) {
|
||||
if (this._finalized) throw new Error('Digest already called')
|
||||
this._finalized = true
|
||||
|
||||
if (this._delimitedSuffix) this._state.absorbLastFewBits(this._delimitedSuffix)
|
||||
let digest = this._state.squeeze(this._hashBitLength / 8)
|
||||
if (encoding !== undefined) digest = digest.toString(encoding)
|
||||
|
||||
this._resetState()
|
||||
|
||||
return digest
|
||||
}
|
||||
|
||||
// remove result from memory
|
||||
_resetState () {
|
||||
this._state.initialize(this._rate, this._capacity)
|
||||
return this
|
||||
}
|
||||
|
||||
// because sometimes we need hash right now and little later
|
||||
_clone () {
|
||||
const clone = new Keccak(this._rate, this._capacity, this._delimitedSuffix, this._hashBitLength, this._options)
|
||||
this._state.copy(clone._state)
|
||||
clone._finalized = this._finalized
|
||||
|
||||
return clone
|
||||
}
|
||||
}
|
||||
+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