From 983750e7f5bd0e3ba051f8517e24677a3e51a718 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Wed, 26 Feb 2020 10:08:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 212f18e..a34a77d 100644 --- a/index.js +++ b/index.js @@ -31,21 +31,6 @@ my.COIN_LIST=['TIC','BTC','ETH'] my.CHAINNET='mainnet' // 默认的链网 module.exports = { - hash(data, option){ // 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' - return crypto.createHash(hasher).update(data, inputEncoding).digest(outputEncoding) - } - return null - } - , isHashable(data, option){ option=option||{} if (option.strict) { @@ -97,6 +82,21 @@ 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 + 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' + return crypto.createHash(hasher).update(data, inputEncoding).digest(outputEncoding) + } + return null + } + , async encrypt(data, {keytype, key, input, output, cipher}={}){ if (keytype==='pwd') { if (this.isHashable(data) && typeof(key)==='string') { @@ -158,7 +158,7 @@ module.exports = { // let naclSeckey = this.buf2hex(nacl.sign.keyPair.fromSeed(seckey).seckey) // return await this.sign(data, naclSeckey, option) }else { // default to eccrypto,因为它对同一组data,seckey生成的签名是固定的,观察到hex长度为140或142,是der格式。 - let signature = await eccrypto.sign(Buffer.from(seckey,'hex'), crypto.createHash('sha256').update(data).digest()) + let signature = await eccrypto.sign(Buffer.from(seckey,'hex'), this.hash(data, {output:'buf'})) return signature.toString('hex') } } @@ -186,7 +186,7 @@ module.exports = { }else { // 默认使用 eccrypto try { await eccrypto.verify(Buffer.from(pubkey, 'hex'), - crypto.createHash('sha256').update(data).digest(), + this.hash(data, {output:'buf'}, Buffer.from(signature, 'hex')) // 如果给signature添加1位hex,eccrypto 的 verify结果也是true! 估计因为一位hex不被转成字节。 return true }catch(exception){ @@ -251,6 +251,7 @@ module.exports = { let keypair = nacl.sign.keyPair.fromSeed(hashBuf) // nacl.sign.keyPair.fromSeed 要求32字节的种子,而 this.secword2seed生成的是64字节种子,所以要先做一次sha256 return { coin: option.coin, + secword: secword, pubkey: Buffer.from(keypair.publicKey).toString('hex'), // 测试过 不能直接keypair.publicKey.toString('hex'),不是buffer类型 seckey: Buffer.from(keypair.secretKey).toString('hex') // nacl.sign.keyPair.fromSeed 得到的 seckey 是64字节的,不同于比特币/以太坊的32字节密钥。 } @@ -273,6 +274,7 @@ module.exports = { } return { coin: option.coin, + secword: secword, seckey: key.privateKey.toString('hex'), // 或者 key.toJSON().privateKey。或者 key.privateKey.slice(2) 删除开头的'0x'如果是ethers.HDNode.fromMnemonic(secword)的结果 pubkey: key.publicKey.toString('hex') } @@ -286,7 +288,6 @@ module.exports = { option.coin=my.COIN_LIST.indexOf(option.coin)>=0?option.coin:my.COIN let kp=this.secword2keypair(secword, option) if (kp) { - kp.secword=secword kp.address=this.seckey2address(kp.seckey, option) return kp }