This commit is contained in:
陆柯 2020-05-03 08:22:35 +08:00
parent c5334b6c8f
commit b6b90b6a38

View File

@ -31,22 +31,21 @@ my.COIN_LIST=['TIC','BTC','ETH']
my.CHAINNET='mainnet' // 默认的链网 my.CHAINNET='mainnet' // 默认的链网
module.exports = { module.exports = {
isHashable(data, option){ isHashable(data, {strict=false}={}){
option=option||{} if (strict) {
if (option.strict) {
return data && typeof(data)!=='boolean' && data!==Infinity // 允许大多数数据,除了空值、布尔值、无限数 return data && typeof(data)!=='boolean' && data!==Infinity // 允许大多数数据,除了空值、布尔值、无限数
} }
return typeof(data)!=='undefined' // 允许一切数据,除非 undefined return typeof(data)!=='undefined' // 允许一切数据,除非 undefined
} }
, ,
isHash(hash, option){ isHash(hash, {hasher=my.HASHER}={}){
option=option||{} if (my.HASHER_LIST.indexOf(hasher)>=0) {
option.hasher=my.HASHER_LIST.indexOf(option.hasher)>=0?option.hasher:my.HASHER switch(hasher){
switch(option.hasher){ case 'sha256': return /^[a-fA-F0-9]{64}$/.test(hash)
case 'sha256': return /^[a-fA-F0-9]{64}$/.test(hash) case 'md5': return /^[a-fA-F0-9]{32}$/.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 'ripemd160': case 'sha1': return /^[a-fA-F0-9]{40}$/.test(hash) case 'sha512': return /^[a-fA-F0-9]{128}$/.test(hash)
case 'sha512': return /^[a-fA-F0-9]{128}$/.test(hash) }
} }
return false 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. 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)) { if (this.isHashable(data)) {
option=option||{}
if (typeof(data)!=='string' && !(data instanceof Buffer) && !(data instanceof DataView)) if (typeof(data)!=='string' && !(data instanceof Buffer) && !(data instanceof DataView))
data=JSON.stringify(data) data=JSON.stringify(data)
if (option.salt && typeof(option.salt)==='string') if (salt && typeof(salt)==='string')
data=data+this.hash(option.salt) data=data+this.hash(salt)
let hasher= my.HASHER_LIST.indexOf(option.hasher)>=0?option.hasher:my.HASHER // 默认为 sha256. 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 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=(output==='buf')?undefined:output // (my.OUTPUT_LIST.indexOf(output)>=0?output:my.OUTPUT) // option.output: 留空=》默认输出hex格式或者手动指定 'buf', hex', 'latin1' or 'base64'
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'
return crypto.createHash(hasher).update(data, inputEncoding).digest(outputEncoding) return crypto.createHash(hasher).update(data, inputEncoding).digest(outputEncoding)
} }
return null return null