Skip to content

Commit

Permalink
supplement gmsm-sm2js sm2互操作示例
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun committed Apr 18, 2024
1 parent c14303d commit 1491629
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,64 @@
## SM2
目前实现:签名结果为**r || s**的拼接;加密结果为**C1 || C3 || C2**拼接,且C1没有点格式前缀字节。为了与其它系统兼容,需要进一步处理。具体使用方法,请参考[sm2_test.js](https://github.com/emmansun/sm4js/blob/master/src/sm2_test.js "sm2_test.js")

[gmsm-sm2js](https://github.com/emmansun/sm2js)互操作示例:
```javascript
const test = require('tape')
const rs = require('jsrsasign')
const sm2 = require('gmsm-sm2js')
const sjclsm = require('gmsm-sm4js')
const sjcl = require('sjcl-with-all')
sjclsm.bindKDF(sjcl)
sjclsm.bindSM3(sjcl)
sjclsm.bindSM2(sjcl)

const sm2PrivateKeyEncryptedPKCS8 = `
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIH2MGEGCSqGSIb3DQEFDTBUMDQGCSqGSIb3DQEFDDAnBBDa6ckWJNP3QBD7MIF8
4nVqAgEQAgEQMA0GCSqBHM9VAYMRAgUAMBwGCCqBHM9VAWgCBBDMUgr+5Y/XN2g9
mPGiISzGBIGQytwK98/ET4WrS0H7AsUri6FTqztrzAvgzFl3+s9AsaYtUlzE3EzE
x6RWxo8kpKO2yj0a/Jh9WZCD4XAcoZ9aMopiWlOdpXJr/iQlMGdirCYIoF37lHMc
jZHNffmk4ii7NxCfjrzpiFq4clYsNMXeSEnq1tuOEur4kYcjHYSIFc9bPG656a60
+SIJsJuPFi0f
-----END ENCRYPTED PRIVATE KEY-----`

test('Parse PKCS8 encrypted SM2 private key and work with sjcl based implementation', function (t) {
const key = rs.KEYUTIL.getKeyFromEncryptedPKCS8PEM(sm2PrivateKeyEncryptedPKCS8, 'Password1')
const SM2PublicKey = sjcl.ecc.sm2.publicKey
const pk = new SM2PublicKey(
sjcl.codec.hex.toBits(
key.pubKeyHex.substring(2)
)
)
const serialized = pk.serialize()
t.equals(serialized.type, 'sm2')
t.equals(serialized.secretKey, false)
t.equals(
serialized.point,
'8356e642a40ebd18d29ba3532fbd9f3bbee8f027c3f6f39a5ba2f870369f9988981f5efe55d1c5cdf6c0ef2b070847a14f7fdf4272a8df09c442f3058af94ba1'
)
t.equals(serialized.curve, 'sm2p256v1')

const keys = sjcl.ecc.sm2.generateKeys(
sjcl.codec.hex.toBits(key.prvKeyHex)
)
const testString = 'send reinforcements, we\'re going to advance'

// gmsm-sm2js encrypt, gmsm-sm4js decrypt
let ciphertext = sm2.encrypt(key.pubKeyHex, testString)
let plaintext = keys.sec.decrypt(sjcl.codec.hex.toBits(ciphertext.substring(2)))
t.equals(sjcl.codec.utf8String.fromBits(plaintext), testString)

// gmsm-sm4js encrypt, gmsm-sm2js decrypt
ciphertext = keys.pub.encrypt(testString)
ciphertext = '04'+sjcl.codec.hex.fromBits(ciphertext)
plaintext = sm2.decryptHex(key.prvKeyHex, ciphertext)
t.equals(Buffer.from(plaintext, 'hex').toString('ascii'), testString)

t.end()
})
```

## SM3
位于**sjcl.hash.sm3**中,使用方式和其它哈希算法相同。具体使用方法,请参考[sm3_test.js](https://github.com/emmansun/sm4js/blob/master/src/sm3_test.js "sm3_test.js")

Expand Down
4 changes: 2 additions & 2 deletions src/sm2.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function bindSM2 (sjcl) {
sjcl.ecc.sm2 = {
/** sm2 publicKey.
* @constructor
* @augments sjcl.ecc.basicKey.publicKey
* @param {point} point the point on the sm2 curve
*/
publicKey: function (point) {
this._curve = sm2Curve
Expand Down Expand Up @@ -96,7 +96,7 @@ function bindSM2 (sjcl) {
},
/** sm2 secretKey
* @constructor
* @augments sjcl.ecc.basicKey.secretKey
* @param {bn|bitArray} exponent The private key big number
*/
secretKey: function (exponent) {
if (exponent instanceof Array) {
Expand Down

0 comments on commit 1491629

Please sign in to comment.