sm2 签名验签通过
This commit is contained in:
parent
0cb442eaa0
commit
cccff194b6
@ -172,9 +172,12 @@ function App() {
|
||||
// SM2 签名
|
||||
const handleSignSm2 = () => {
|
||||
try {
|
||||
console.log('开始签名,消息:', sm2Message, '私钥:', sm2PrivateKey)
|
||||
const signature = sm2Sign(sm2Message, sm2PrivateKey)
|
||||
console.log('签名成功,结果:', signature)
|
||||
setSm2Signature(signature)
|
||||
} catch (error) {
|
||||
console.error('签名失败:', error)
|
||||
alert('签名失败: ' + (error instanceof Error ? error.message : error))
|
||||
}
|
||||
}
|
||||
@ -182,9 +185,12 @@ function App() {
|
||||
// SM2 验证签名
|
||||
const handleVerifySm2 = () => {
|
||||
try {
|
||||
console.log('开始验签,消息:', sm2Message, '签名:', sm2Signature, '公钥:', sm2PublicKey)
|
||||
const result = sm2Verify(sm2Message, sm2Signature, sm2PublicKey)
|
||||
console.log('验签成功,结果:', result)
|
||||
setSm2VerifyResult(result ? '验证成功' : '验证失败')
|
||||
} catch (error) {
|
||||
console.error('验签失败:', error)
|
||||
alert('验证签名失败: ' + (error instanceof Error ? error.message : error))
|
||||
}
|
||||
}
|
||||
|
||||
4
src/types/sm-crypto.d.ts
vendored
4
src/types/sm-crypto.d.ts
vendored
@ -8,8 +8,8 @@ declare module 'sm-crypto' {
|
||||
generateKeyPairHex: (privateKey?: string) => SM2KeyPair;
|
||||
doEncrypt: (message: string, publicKey: string) => string;
|
||||
doDecrypt: (ciphertext: string, privateKey: string) => string;
|
||||
doSign: (message: string, privateKey: string) => string;
|
||||
doVerify: (message: string, signature: string, publicKey: string) => boolean;
|
||||
doSignature: (message: string, privateKey: string, options?: { hash?: boolean, der?: boolean, userId?: string, publicKey?: string, pointPool?: any[] }) => string;
|
||||
doVerifySignature: (message: string, signature: string, publicKey: string, options?: { hash?: boolean, der?: boolean, userId?: string }) => boolean;
|
||||
};
|
||||
|
||||
export const sm3: {
|
||||
|
||||
@ -77,11 +77,8 @@ export const sm2Sign = (message: string, privateKey: string): string => {
|
||||
|
||||
// 将 hex 字符串转换为字节数组
|
||||
const messageBytes = new Uint8Array(hexToArrayBuffer(message))
|
||||
// 将字节数组转换为字符串
|
||||
const messageString = new TextDecoder().decode(messageBytes)
|
||||
|
||||
// 签名
|
||||
const signature = sm2.doSign(messageString, privateKey)
|
||||
const signature = sm2.doSignature(messageBytes, privateKey, { hash: false })
|
||||
return signature
|
||||
}
|
||||
|
||||
@ -102,10 +99,7 @@ export const sm2Verify = (message: string, signature: string, publicKey: string)
|
||||
|
||||
// 将 hex 字符串转换为字节数组
|
||||
const messageBytes = new Uint8Array(hexToArrayBuffer(message))
|
||||
// 将字节数组转换为字符串
|
||||
const messageString = new TextDecoder().decode(messageBytes)
|
||||
|
||||
// 验证签名
|
||||
const result = sm2.doVerify(messageString, signature, publicKey)
|
||||
const result = sm2.doVerifySignature(messageBytes, signature, publicKey, { hash: false })
|
||||
return result
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user