diff --git a/src/App.tsx b/src/App.tsx index 28dce27..268c7db 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -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)) } } diff --git a/src/types/sm-crypto.d.ts b/src/types/sm-crypto.d.ts index 6839de6..c9c399a 100644 --- a/src/types/sm-crypto.d.ts +++ b/src/types/sm-crypto.d.ts @@ -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: { diff --git a/src/utils/algorithm/sm2.ts b/src/utils/algorithm/sm2.ts index aca7417..26d90f8 100644 --- a/src/utils/algorithm/sm2.ts +++ b/src/utils/algorithm/sm2.ts @@ -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 }