mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
added hash based methods
This commit is contained in:
parent
2992aafcc8
commit
1bb83c6866
@ -70,6 +70,23 @@ class MerkleTree {
|
||||
// Set the final hash as root
|
||||
this.root = currentRow[0]
|
||||
}
|
||||
inputHash(node) {
|
||||
return this.hashBase(node, this.option.inputHash)
|
||||
}
|
||||
|
||||
hash(node) {
|
||||
return this.hashBase(node, this.option.hash)
|
||||
}
|
||||
|
||||
hashBase(node, method = this.option.hash) {
|
||||
node = this._toString(node)
|
||||
if (method && (typeof method === 'function')) {
|
||||
return method(node)
|
||||
}
|
||||
const hash = crypto.createHash(method)
|
||||
return hash.update(node, 'utf8').digest('hex')
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user