import { sm2 } from 'sm.js'; import { CurveLabel, KeyParameters, KeyType, PrivateKey, PublicKey, Signature, SignatureScheme } from '../src/crypto'; import { ab2hexstring, ab2str, str2ab, str2hexstr } from '../src/utils'; describe('SM2 and SM3 cryptographics functions tests', () => { test('test SM3 hash', () => { const key: PrivateKey = PrivateKey.random(KeyType.SM2); const msg = 'test'; const encoded = str2hexstr(msg); const hash = key.computeHash(encoded, SignatureScheme.SM2withSM3); expect(hash).toEqual('55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23'); }); test('test SM2 sign and verify', () => { const msg = 'test'; const encoded = str2hexstr(msg); const pk = 'ab80a7ad086249c01e65c4d9bb6ce18de259dcfc218cd49f2455c539e9112ca3'; const privateKey = new PrivateKey(pk, KeyType.SM2, new KeyParameters(CurveLabel.SM2P256V1)); const signature = privateKey.sign(encoded, SignatureScheme.SM2withSM3); // tslint:disable-next-line:no-console console.log('signature', signature.value); const publicKey = privateKey.getPublicKey(); const result = publicKey.verify(encoded, signature); expect(result).toBeTruthy(); }); test('test SM2 verify java SDK generated signature', () => { const msg = 'test'; const encoded = str2hexstr(msg); const signature = new Signature( SignatureScheme.SM2withSM3, // tslint:disable-next-line:max-line-length '3132333435363738313233343536373800bc1d431f932afb7b809627f051c1b5c10ee22e470aea4623c3281231dd513779ca1171f4f3bbf414f089e8c963f715b3376007008206a2b9ebb252dd6883dbce' ); const publicKey = new PublicKey( '031220580679fda524f575ac48b39b9f74cb0a97993df4fac5798b04c702d07a39', KeyType.SM2, new KeyParameters(CurveLabel.SM2P256V1) ); const result = publicKey.verify(encoded, signature); expect(result).toBeTruthy(); }); });