sm2 签名验签通过

This commit is contained in:
cheney 2026-04-18 11:09:41 +08:00
parent 0cb442eaa0
commit cccff194b6
3 changed files with 10 additions and 10 deletions

View File

@ -172,9 +172,12 @@ function App() {
// SM2 签名 // SM2 签名
const handleSignSm2 = () => { const handleSignSm2 = () => {
try { try {
console.log('开始签名,消息:', sm2Message, '私钥:', sm2PrivateKey)
const signature = sm2Sign(sm2Message, sm2PrivateKey) const signature = sm2Sign(sm2Message, sm2PrivateKey)
console.log('签名成功,结果:', signature)
setSm2Signature(signature) setSm2Signature(signature)
} catch (error) { } catch (error) {
console.error('签名失败:', error)
alert('签名失败: ' + (error instanceof Error ? error.message : error)) alert('签名失败: ' + (error instanceof Error ? error.message : error))
} }
} }
@ -182,9 +185,12 @@ function App() {
// SM2 验证签名 // SM2 验证签名
const handleVerifySm2 = () => { const handleVerifySm2 = () => {
try { try {
console.log('开始验签,消息:', sm2Message, '签名:', sm2Signature, '公钥:', sm2PublicKey)
const result = sm2Verify(sm2Message, sm2Signature, sm2PublicKey) const result = sm2Verify(sm2Message, sm2Signature, sm2PublicKey)
console.log('验签成功,结果:', result)
setSm2VerifyResult(result ? '验证成功' : '验证失败') setSm2VerifyResult(result ? '验证成功' : '验证失败')
} catch (error) { } catch (error) {
console.error('验签失败:', error)
alert('验证签名失败: ' + (error instanceof Error ? error.message : error)) alert('验证签名失败: ' + (error instanceof Error ? error.message : error))
} }
} }

View File

@ -8,8 +8,8 @@ declare module 'sm-crypto' {
generateKeyPairHex: (privateKey?: string) => SM2KeyPair; generateKeyPairHex: (privateKey?: string) => SM2KeyPair;
doEncrypt: (message: string, publicKey: string) => string; doEncrypt: (message: string, publicKey: string) => string;
doDecrypt: (ciphertext: string, privateKey: string) => string; doDecrypt: (ciphertext: string, privateKey: string) => string;
doSign: (message: string, privateKey: string) => string; doSignature: (message: string, privateKey: string, options?: { hash?: boolean, der?: boolean, userId?: string, publicKey?: string, pointPool?: any[] }) => string;
doVerify: (message: string, signature: string, publicKey: string) => boolean; doVerifySignature: (message: string, signature: string, publicKey: string, options?: { hash?: boolean, der?: boolean, userId?: string }) => boolean;
}; };
export const sm3: { export const sm3: {

View File

@ -77,11 +77,8 @@ export const sm2Sign = (message: string, privateKey: string): string => {
// 将 hex 字符串转换为字节数组 // 将 hex 字符串转换为字节数组
const messageBytes = new Uint8Array(hexToArrayBuffer(message)) 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 return signature
} }
@ -102,10 +99,7 @@ export const sm2Verify = (message: string, signature: string, publicKey: string)
// 将 hex 字符串转换为字节数组 // 将 hex 字符串转换为字节数组
const messageBytes = new Uint8Array(hexToArrayBuffer(message)) 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 return result
} }