优化 sign 和 verify
This commit is contained in:
parent
00287a0944
commit
88ee0cd8df
30
index.js
30
index.js
@ -262,25 +262,23 @@ class TICrypto {
|
|||||||
* @return {String}
|
* @return {String}
|
||||||
* @memberof TICrypto
|
* @memberof TICrypto
|
||||||
*/
|
*/
|
||||||
static async sign(data, seckey, option = {}) {
|
static async sign(data, seckey, { tool, hasher }) {
|
||||||
// data can be string or buffer or object, results are the same
|
// data can be string or buffer or object, results are the same
|
||||||
if (this.isHashable(data) && this.isSeckey(seckey)) {
|
if (this.isHashable(data) && this.isSeckey(seckey)) {
|
||||||
if (option.tool === 'nacl' && seckey.length === 128) {
|
if (tool === 'nacl' && seckey.length === 128) {
|
||||||
// 使用nacl的签名算法。注意,nacl.sign需要的seckey是64字节=128字符。
|
// 使用nacl的签名算法。注意,nacl.sign需要的seckey是64字节=128字符。
|
||||||
option.output = 'buf' // 哈希必须输出为 buffer
|
let hashBuf = this.hash(data, { output: 'buf' }) // 哈希必须输出为 buffer
|
||||||
let hashBuf = this.hash(data, option)
|
|
||||||
let signature = nacl.sign.detached(hashBuf, Buffer.from(seckey, 'hex'))
|
let signature = nacl.sign.detached(hashBuf, Buffer.from(seckey, 'hex'))
|
||||||
return Buffer.from(signature).toString('hex') // 签名是64节,128个hex字符
|
return Buffer.from(signature).toString('hex') // 签名是64节,128个hex字符
|
||||||
} else if (option.tool === 'eccrypto' && seckey.length === 64) {
|
} else if (tool === 'eccrypto' && seckey.length === 64) {
|
||||||
// eccrypto 对同一组data,seckey生成的签名是固定的,观察到hex长度为140或142,是der格式。
|
// eccrypto 对同一组data,seckey生成的签名是固定的,观察到hex长度为140或142,是der格式。
|
||||||
let signature = await eccrypto.sign(Buffer.from(seckey, 'hex'), this.hash(data, { output: 'buf' }))
|
let signature = await eccrypto.sign(Buffer.from(seckey, 'hex'), this.hash(data, { output: 'buf' }))
|
||||||
return signature.toString('hex')
|
return signature.toString('hex')
|
||||||
} else if (seckey.length === 64) {
|
} else if (seckey.length === 64) {
|
||||||
// 纯 crypto
|
// 纯 crypto
|
||||||
let seckeyPEM = await new keyman.Key('oct', this.hex2buf(seckey), { namedCurve: 'P-256K' }).export('pem') // 私钥导出的der格式为144字节。
|
let seckeyPEM = await new keyman.Key('oct', this.hex2buf(seckey), { namedCurve: 'P-256K' }).export('pem') // 私钥导出的der格式为144字节。
|
||||||
let hasher = my.HASHER_LIST.indexOf(option.hasher) >= 0 ? option.hasher : my.HASHER
|
let signer = crypto.createSign(['sha1', 'sha256', 'sha512'].indexOf(hasher) >= 0 ? hasher : my.HASHER)
|
||||||
let signer = crypto.createSign(hasher)
|
signer.update(this.hash(data)).end()
|
||||||
signer.update(this.hash(data, option)).end()
|
|
||||||
let signature = signer.sign(seckeyPEM, 'hex')
|
let signature = signer.sign(seckeyPEM, 'hex')
|
||||||
return signature // 发现同样的输入,nodejs里每次调用会生成不同的 signature, 且长度不定(140,142,144 hex) 但都可以通过 verify。但在浏览器里调用,signature却是固定的。
|
return signature // 发现同样的输入,nodejs里每次调用会生成不同的 signature, 且长度不定(140,142,144 hex) 但都可以通过 verify。但在浏览器里调用,signature却是固定的。
|
||||||
}
|
}
|
||||||
@ -299,17 +297,16 @@ class TICrypto {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
* @memberof TICrypto
|
* @memberof TICrypto
|
||||||
*/
|
*/
|
||||||
static async verify(data, signature, pubkey, option = {}) {
|
static async verify(data, signature, pubkey, { tool, hasher }) {
|
||||||
// data could be anything, but converts to string or remains be Buffer/TypedArray/DataView
|
// data could be anything, but converts to string or remains be Buffer/TypedArray/DataView
|
||||||
if (this.isHashable(data) && this.isSignature(signature) && this.isPubkey(pubkey)) {
|
if (this.isHashable(data) && this.isSignature(signature) && this.isPubkey(pubkey)) {
|
||||||
if ('nacl' === option.tool && signature.length === 128) {
|
if ('nacl' === tool && signature.length === 128) {
|
||||||
option.output = 'buf' // 哈希必须输出为 buffer
|
let bufHash = this.hash(data, { output: 'buf' })
|
||||||
let bufHash = this.hash(data, option)
|
|
||||||
let bufSignature = Buffer.from(signature, 'hex')
|
let bufSignature = Buffer.from(signature, 'hex')
|
||||||
let bufPubkey = Buffer.from(pubkey, 'hex')
|
let bufPubkey = Buffer.from(pubkey, 'hex')
|
||||||
let verified = nacl.sign.detached.verify(bufHash, bufSignature, bufPubkey)
|
let verified = nacl.sign.detached.verify(bufHash, bufSignature, bufPubkey)
|
||||||
return verified
|
return verified
|
||||||
} else if ('eccrypto' === option.tool && signature.length >= 140) {
|
} else if ('eccrypto' === tool && signature.length >= 140) {
|
||||||
// 默认使用 eccrypto
|
// 默认使用 eccrypto
|
||||||
try {
|
try {
|
||||||
let result = await eccrypto.verify(Buffer.from(pubkey, 'hex'), this.hash(data, { output: 'buf' }), Buffer.from(signature, 'hex')) // 如果给signature添加1位hex,eccrypto 的 verify结果也是true! 估计因为一位hex不被转成字节。
|
let result = await eccrypto.verify(Buffer.from(pubkey, 'hex'), this.hash(data, { output: 'buf' }), Buffer.from(signature, 'hex')) // 如果给signature添加1位hex,eccrypto 的 verify结果也是true! 估计因为一位hex不被转成字节。
|
||||||
@ -321,10 +318,9 @@ class TICrypto {
|
|||||||
} else if (signature.length >= 140) {
|
} else if (signature.length >= 140) {
|
||||||
// 纯 crypto
|
// 纯 crypto
|
||||||
let pubkeyPEM = await new keyman.Key('oct', this.hex2buf(pubkey), { namedCurve: 'P-256K' }).export('pem') // 公钥导出的der格式为88字节。经测试,同一对压缩和非压缩公钥得出的结果一模一样。
|
let pubkeyPEM = await new keyman.Key('oct', this.hex2buf(pubkey), { namedCurve: 'P-256K' }).export('pem') // 公钥导出的der格式为88字节。经测试,同一对压缩和非压缩公钥得出的结果一模一样。
|
||||||
let hasher = my.HASHER_LIST.indexOf(option.hasher) >= 0 ? option.hasher : my.HASHER
|
let verifier = crypto.createVerify() // 不像 createSign(hasher),createVerify()不需要参数
|
||||||
let verifier = crypto.createVerify(hasher)
|
verifier.update(this.hash(data)).end() // end() 在 nodejs 12 里返回verifier自身,但在浏览器里返回 undefined,因此不能串联运行。
|
||||||
verifier.update(this.hash(data, option)).end() // end() 在 nodejs 12 里返回verifier自身,但在浏览器里返回 undefined,因此不能串联运行。
|
let verified = verifier.verify(pubkeyPEM, signature, 'hex') // 如果给signature添加1位hex,crypto 的 verify结果也是true! 估计因为一位hex不被转成字节。但减少1位会导致false
|
||||||
let verified = verifier.verify(pubkeyPEM, signature, 'hex') // 如果给signature添加1位hex,crypto 的 verify结果也是true! 估计因为一位hex不被转成字节。
|
|
||||||
return verified
|
return verified
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user