u
This commit is contained in:
parent
c5334b6c8f
commit
b6b90b6a38
33
index.js
33
index.js
@ -31,22 +31,21 @@ my.COIN_LIST=['TIC','BTC','ETH']
|
||||
my.CHAINNET='mainnet' // 默认的链网
|
||||
|
||||
module.exports = {
|
||||
isHashable(data, option){
|
||||
option=option||{}
|
||||
if (option.strict) {
|
||||
isHashable(data, {strict=false}={}){
|
||||
if (strict) {
|
||||
return data && typeof(data)!=='boolean' && data!==Infinity // 允许大多数数据,除了空值、布尔值、无限数
|
||||
}
|
||||
return typeof(data)!=='undefined' // 允许一切数据,除非 undefined
|
||||
}
|
||||
,
|
||||
isHash(hash, option){
|
||||
option=option||{}
|
||||
option.hasher=my.HASHER_LIST.indexOf(option.hasher)>=0?option.hasher:my.HASHER
|
||||
switch(option.hasher){
|
||||
case 'sha256': return /^[a-fA-F0-9]{64}$/.test(hash)
|
||||
case 'md5': return /^[a-fA-F0-9]{32}$/.test(hash)
|
||||
case 'ripemd160': case 'sha1': return /^[a-fA-F0-9]{40}$/.test(hash)
|
||||
case 'sha512': return /^[a-fA-F0-9]{128}$/.test(hash)
|
||||
isHash(hash, {hasher=my.HASHER}={}){
|
||||
if (my.HASHER_LIST.indexOf(hasher)>=0) {
|
||||
switch(hasher){
|
||||
case 'sha256': return /^[a-fA-F0-9]{64}$/.test(hash)
|
||||
case 'md5': return /^[a-fA-F0-9]{32}$/.test(hash)
|
||||
case 'ripemd160': case 'sha1': return /^[a-fA-F0-9]{40}$/.test(hash)
|
||||
case 'sha512': return /^[a-fA-F0-9]{128}$/.test(hash)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@ -87,16 +86,14 @@ module.exports = {
|
||||
return /^[a-fA-F0-9]{128,144}$/.test(signature) && (signature.length % 2 === 0) // 128 for nacl, 140/142/144 for crypto and eccrypto in der format.
|
||||
}
|
||||
,
|
||||
hash(data, option){ // data can be anything, but converts to string or remains be Buffer/TypedArray/DataView
|
||||
hash(data, {hasher=my.HASHER, salt, input=my.INPUT, output=my.OUTPUT}={}){ // data can be anything, but converts to string or remains be Buffer/TypedArray/DataView
|
||||
if (this.isHashable(data)) {
|
||||
option=option||{}
|
||||
if (typeof(data)!=='string' && !(data instanceof Buffer) && !(data instanceof DataView))
|
||||
data=JSON.stringify(data)
|
||||
if (option.salt && typeof(option.salt)==='string')
|
||||
data=data+this.hash(option.salt)
|
||||
let hasher= my.HASHER_LIST.indexOf(option.hasher)>=0?option.hasher:my.HASHER // 默认为 sha256.
|
||||
let inputEncoding=my.INPUT_LIST.indexOf(option.input)>=0?option.input:my.INPUT // 'utf8', 'ascii' or 'latin1' for string data, default to utf8 if not specified; ignored for Buffer, TypedArray, or DataView.
|
||||
let outputEncoding=(option.output==='buf')?undefined:(my.OUTPUT_LIST.indexOf(option.output)>=0?option.output:my.OUTPUT) // option.output: 留空=》默认输出hex格式;或者手动指定 'buf', hex', 'latin1' or 'base64'
|
||||
if (salt && typeof(salt)==='string')
|
||||
data=data+this.hash(salt)
|
||||
let inputEncoding=input // my.INPUT_LIST.indexOf(option.input)>=0?option.input:my.INPUT // 'utf8', 'ascii' or 'latin1' for string data, default to utf8 if not specified; ignored for Buffer, TypedArray, or DataView.
|
||||
let outputEncoding=(output==='buf')?undefined:output // (my.OUTPUT_LIST.indexOf(output)>=0?output:my.OUTPUT) // option.output: 留空=》默认输出hex格式;或者手动指定 'buf', hex', 'latin1' or 'base64'
|
||||
return crypto.createHash(hasher).update(data, inputEncoding).digest(outputEncoding)
|
||||
}
|
||||
return null
|
||||
|
Loading…
Reference in New Issue
Block a user