sm2 改进

This commit is contained in:
cheney 2026-04-18 14:58:39 +08:00
parent c9d66a77b1
commit 20c27b2e5b
4 changed files with 239 additions and 168 deletions

View File

@ -59,8 +59,10 @@ function App() {
const [sm2Message, setSm2Message] = useState('')
const [sm2Encrypted, setSm2Encrypted] = useState('')
const [sm2Decrypted, setSm2Decrypted] = useState('')
const [sm2SignMessage, setSm2SignMessage] = useState('')
const [sm2Signature, setSm2Signature] = useState('')
const [sm2VerifyResult, setSm2VerifyResult] = useState('')
const [sm2ActiveTab, setSm2ActiveTab] = useState('encrypt')
// 转换工具状态
const [hexInput, setHexInput] = useState('')
@ -172,8 +174,8 @@ function App() {
// SM2 签名
const handleSignSm2 = () => {
try {
console.log('开始签名,消息:', sm2Message, '私钥:', sm2PrivateKey)
const signature = sm2Sign(sm2Message, sm2PrivateKey)
console.log('开始签名,消息:', sm2SignMessage, '私钥:', sm2PrivateKey)
const signature = sm2Sign(sm2SignMessage, sm2PrivateKey)
console.log('签名成功,结果:', signature)
setSm2Signature(signature)
} catch (error) {
@ -185,8 +187,8 @@ function App() {
// SM2 验证签名
const handleVerifySm2 = () => {
try {
console.log('开始验签,消息:', sm2Message, '签名:', sm2Signature, '公钥:', sm2PublicKey)
const result = sm2Verify(sm2Message, sm2Signature, sm2PublicKey)
console.log('开始验签,消息:', sm2SignMessage, '签名:', sm2Signature, '公钥:', sm2PublicKey)
const result = sm2Verify(sm2SignMessage, sm2Signature, sm2PublicKey)
console.log('验签成功,结果:', result)
setSm2VerifyResult(result ? '验证成功' : '验证失败')
} catch (error) {
@ -382,8 +384,8 @@ function App() {
<label style={{ display: 'block', marginBottom: '5px', color: '#e0e0e0' }}></label>
<textarea
value={sm2PublicKey}
readOnly
placeholder="公钥将显示在这里"
onChange={(e) => setSm2PublicKey(e.target.value)}
placeholder="输入公钥 (hex) 或点击生成密钥对"
style={{
width: '100%',
padding: '10px',
@ -399,8 +401,50 @@ function App() {
</div>
</div>
{/* SM2 功能选项卡 */}
<div style={{ marginBottom: '20px' }}>
<div style={{ display: 'flex', borderBottom: '1px solid #333', marginBottom: '20px', backgroundColor: '#2d2d2d', borderRadius: '8px 8px 0 0', overflow: 'hidden' }}>
<button
className={`tab ${sm2ActiveTab === 'encrypt' ? 'active' : ''}`}
onClick={() => setSm2ActiveTab('encrypt')}
style={{
padding: '12px 24px',
border: 'none',
background: sm2ActiveTab === 'encrypt' ? '#333' : '#2d2d2d',
cursor: 'pointer',
fontSize: '16px',
fontWeight: sm2ActiveTab === 'encrypt' ? 'bold' : 'normal',
color: sm2ActiveTab === 'encrypt' ? '#f59e0b' : '#e0e0e0',
outline: 'none',
transition: 'all 0.3s ease',
borderBottom: sm2ActiveTab === 'encrypt' ? '3px solid #f59e0b' : 'none'
}}
>
/
</button>
<button
className={`tab ${sm2ActiveTab === 'sign' ? 'active' : ''}`}
onClick={() => setSm2ActiveTab('sign')}
style={{
padding: '12px 24px',
border: 'none',
background: sm2ActiveTab === 'sign' ? '#333' : '#2d2d2d',
cursor: 'pointer',
fontSize: '16px',
fontWeight: sm2ActiveTab === 'sign' ? 'bold' : 'normal',
color: sm2ActiveTab === 'sign' ? '#f59e0b' : '#e0e0e0',
outline: 'none',
transition: 'all 0.3s ease',
borderBottom: sm2ActiveTab === 'sign' ? '3px solid #f59e0b' : 'none'
}}
>
/
</button>
</div>
{/* 加密/解密 */}
<div style={{ marginBottom: '20px', padding: '15px', backgroundColor: '#2d2d2d', borderRadius: '8px' }}>
{sm2ActiveTab === 'encrypt' && (
<div style={{ padding: '15px', backgroundColor: '#2d2d2d', borderRadius: '8px' }}>
<h3 style={{ color: '#e0e0e0', marginBottom: '10px' }}>/</h3>
<div className="form-group" style={{ marginBottom: '10px' }}>
<label style={{ display: 'block', marginBottom: '5px', color: '#e0e0e0' }}></label>
@ -496,10 +540,31 @@ function App() {
</button>
</div>
</div>
)}
{/* 签名/验证 */}
<div style={{ marginBottom: '20px', padding: '15px', backgroundColor: '#2d2d2d', borderRadius: '8px' }}>
{sm2ActiveTab === 'sign' && (
<div style={{ padding: '15px', backgroundColor: '#2d2d2d', borderRadius: '8px' }}>
<h3 style={{ color: '#e0e0e0', marginBottom: '10px' }}>/</h3>
<div className="form-group" style={{ marginBottom: '10px' }}>
<label style={{ display: 'block', marginBottom: '5px', color: '#e0e0e0' }}></label>
<textarea
value={sm2SignMessage}
onChange={(e) => setSm2SignMessage(e.target.value)}
placeholder="输入要签名的消息 (hex)"
style={{
width: '100%',
padding: '10px',
border: '1px solid #333',
borderRadius: '4px',
backgroundColor: '#3d3d3d',
color: '#e0e0e0',
fontSize: '14px',
resize: 'vertical',
minHeight: '100px'
}}
/>
</div>
<div className="form-group" style={{ marginBottom: '10px' }}>
<label style={{ display: 'block', marginBottom: '5px', color: '#e0e0e0' }}></label>
<textarea
@ -575,6 +640,8 @@ function App() {
</button>
</div>
</div>
)}
</div>
</div>
)}

View File

@ -10,6 +10,7 @@ declare module 'sm-crypto' {
doDecrypt: (ciphertext: string, privateKey: string) => string;
doSignature: (message: string | Uint8Array, privateKey: string, options?: { hash?: boolean, der?: boolean, userId?: string, publicKey?: string, pointPool?: any[] }) => string;
doVerifySignature: (message: string | Uint8Array, signature: string, publicKey: string, options?: { hash?: boolean, der?: boolean, userId?: string }) => boolean;
getPublicKeyFromPrivateKey: (privateKey: string) => string;
};
export const sm3: {

View File

@ -15,12 +15,15 @@ export const sm2GenerateKeyPair = (): { privateKey: string; publicKey: string }
// SM2 从私钥计算公钥
export const sm2GetPublicKeyFromPrivateKey = (privateKey: string): string => {
// 验证私钥是否为有效的 hex 字符串
if (!privateKey) {
throw new Error('Private key is required')
}
if (!isValidHex(privateKey)) {
throw new Error('Invalid private key: must be hex string')
}
const keypair = sm2.generateKeyPairHex(privateKey)
return keypair.publicKey
const publicKey = sm2.getPublicKeyFromPrivateKey(privateKey)
return publicKey
}
// SM2 加密

View File

@ -1 +1 @@
export const VERSION = "V1.0-20260418053331";
export const VERSION = "V1.0-20260418065443";