Skip to content

Commit

Permalink
try to use nodejs native symmetric encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Apr 16, 2024
1 parent 7821b19 commit 34bbe84
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gmsm-sm2js",
"version": "0.6.2",
"version": "0.6.3",
"description": "Pure Javascript implementation of the SM2/SM3/SM4 functions based on jsrsasign",
"keywords": [
"sm2",
Expand Down
50 changes: 50 additions & 0 deletions src/jsrsasign_patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ function patchSM3 () {
}

function patchSM4 () {
let crypto
let useNodeSM4 = false
try {
crypto = require('crypto')
useNodeSM4 = crypto.getCiphers().indexOf('sm4-cbc') >= 0
} catch (err) {
console.log('crypto support is disabled!')
}
/**
* encrypt raw string by specified key and algorithm<br/>
* @name encrypt
Expand Down Expand Up @@ -121,6 +129,27 @@ function patchSM4 () {
const hPlain = s
if (rs.aryval(param, 'key') !== undefined) hKey = param.key
// if (rs.aryval(param, 'enc') !== undefined) hEnc = param.enc
if (useNodeSM4) {
let cipherMode
switch (algName) {
case 'sm4-CBC':
cipherMode = 'sm4-cbc'
break
case 'des-EDE3-CBC':
cipherMode = 'des-ede3-cbc'
break
case 'aes128-CBC':
cipherMode = 'aes-128-cbc'
break
case 'aes256-CBC':
cipherMode = 'aes-256-cbc'
break
default:
throw new Error('unsupported algorithm: ' + algName)
}
const cipher = crypto.createCipheriv(cipherMode, Buffer.from(hKey, 'hex'), Buffer.from(param.iv, 'hex'));
return cipher.update(hPlain, 'hex', 'hex') + cipher.final('hex');
}
const wKey = C.enc.Hex.parse(hKey)
const wPlain = C.enc.Hex.parse(hPlain)
const wIV = C.enc.Hex.parse(param.iv)
Expand Down Expand Up @@ -174,6 +203,27 @@ function patchSM4 () {
const hEnc = hex
if (rs.aryval(param, 'key') !== undefined) hKey = param.key
// if (rs.aryval(param, 'enc') !== undefined) hEnc = param.enc
if (useNodeSM4) {
let cipherMode
switch (algName) {
case 'sm4-CBC':
cipherMode = 'sm4-cbc'
break
case 'des-EDE3-CBC':
cipherMode = 'des-ede3-cbc'
break
case 'aes128-CBC':
cipherMode = 'aes-128-cbc'
break
case 'aes256-CBC':
cipherMode = 'aes-256-cbc'
break
default:
throw new Error('unsupported algorithm: ' + algName)
}
const cipher = crypto.createDecipheriv(cipherMode, Buffer.from(hKey, 'hex'), Buffer.from(param.iv, 'hex'));
return cipher.update(hEnc, 'hex', 'hex') + cipher.final('hex');
}
const wKey = C.enc.Hex.parse(hKey)
const wEnc = C.enc.Hex.parse(hEnc)
const wIV = C.enc.Hex.parse(param.iv)
Expand Down

0 comments on commit 34bbe84

Please sign in to comment.