JuneAndGreen / sm-crypto

国密算法js版
MIT License
954 stars 254 forks source link

fix bug in bytes/arrayToHex #103

Closed xueqianLu closed 11 months ago

xueqianLu commented 11 months ago

The function arrayToHex in src/sm2/utils.js can't work well.

JuneAndGreen commented 11 months ago

有没有具体的 bug 场景?

xueqianLu commented 11 months ago

有没有具体的 bug 场景?

对于 uint8 数组 132,164,84,121,112,101,7,162,73,68,205,3,234,162,79,80,196,103,131,165,119,84,121,112,101,0,163,85,73,68,217,64,54,101,48,102,57,101,49,52,51,52,52,99,53,52,48,54,97,48,99,102,53,97,51,98,52,100,102,98,54,54,53,102,56,55,102,52,97,55,55,49,97,51,49,102,55,101,100,98,98,53,99,55,50,56,55,52,97,51,50,98,50,57,53,55,162,79,80,196,20,130,166,117,115,101,114,73,68,205,3,234,164,105,110,102,111,163,49,50,51,162,84,83,207,0,0,1,139,242,102,11,112 用原来的算法得到的结果是 840547970657049440300050067830775479706500554944040366530663965313433343463353430366130636635613362346466623636356638376634613737316133316637656462623563373238373461333262323935370050014820757365724944030069066003132330545300010066070 正确的结果应该是 84a45479706507a24944cd03eaa24f50c46783a5775479706500a3554944d94036653066396531343334346335343036613063663561336234646662363635663837663461373731613331663765646262356337323837346133326232393537a24f50c41482a6757365724944cd03eaa4696e666fa3313233a25453cf0000018bf2660b70

JuneAndGreen commented 11 months ago

你是在哪个平台跑的?我在 chrome 下跑是一样的输出:

image

都是 84a45479706507a24944cd03eaa24f50c46783a5775479706500a3554944d94036653066396531343334346335343036613063663561336234646662363635663837663461373731613331663765646262356337323837346133326232393537a24f50c41482a6757365724944cd03eaa4696e666fa3313233a25453cf0000018bf2660b70

JuneAndGreen commented 11 months ago

node.js v16.14.0 下也正常。

xueqianLu commented 11 months ago

你是在哪个平台跑的?我在 chrome 下跑是一样的输出:

image

都是 84a45479706507a24944cd03eaa24f50c46783a5775479706500a3554944d94036653066396531343334346335343036613063663561336234646662363635663837663461373731613331663765646262356337323837346133326232393537a24f50c41482a6757365724944cd03eaa4696e666fa3313233a25453cf0000018bf2660b70

嗯,直接定义的是没有发现问题。我是在和msgpack结合的时候出现的问题,

var msgpack = require('@msgpack/msgpack')
const winfo = {
    UserID: 1002,
    Info: "123456",
}
var arr = msgpack.encode(winfo)
console.log(Buffer.from(arr).toString('hex'))

function arrayToHex1 (arr) {
    return arr.map(item => {
        item = item.toString(16)
        return item.length === 1 ? '0' + item : item
    }).join('')
}

function arrayToHex2 (arr) {
    return arr.reduce((output, elem) =>
        (output + ('0' + elem.toString(16)).slice(-2)),
        '')
}
console.log(arrayToHex1(arr))
console.log(arrayToHex2(arr))
image
JuneAndGreen commented 11 months ago

他返回的不是一个数组,是 TypedArray,我这边的实现不是基于 TypedArray 的。

有基于 TypedArray 的加解密需求可以看看这位同学的实现:https://github.com/Cubelrti/sm-crypto-v2 ,对外接口是支持 TypedArray 输入的

或者你转成 array 输入也可以:console.log(arrayToHex1(Array.prototype.slice.call(arr)))

xueqianLu commented 11 months ago

他返回的不是一个数组,是 TypedArray,我这边的实现不是基于 TypedArray 的。

有基于 TypedArray 的加解密需求可以看看这位同学的实现:https://github.com/Cubelrti/sm-crypto-v2 ,对外接口是支持 TypedArray 输入的

或者你转成 array 输入也可以:console.log(arrayToHex1(Array.prototype.slice.call(arr)))

好,多谢了