1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import md5 from 'js-md5'
- const {
- SM4
- } = require('gm-crypto')
- export function encryptAndSign(appKey, appSecret, privateKey, content, crypt) {
- try {
-
- let timestamp = Date.now();
- let str = appKey + appSecret + timestamp
- const sign = md5(str);
-
-
- let key = ""
- for (let i = 0; i < privateKey.length; i++) {
- key += privateKey.charCodeAt(i).toString(16);
- }
- let data = {
- sign,
- timestamp
- }
- if (crypt == "decrypt") {
-
-
- data.decrypted = SM4.decrypt(content, key, {
- inputEncoding: 'hex',
- outputEncoding: 'utf8'
- })
-
- } else {
-
- data.encrypted = SM4.encrypt(content, key, {
- inputEncoding: 'utf8',
- outputEncoding: 'hex'
- })
-
- }
- return data
- } catch (error) {
- return null
- }
- }
- module.exports = {
- encryptAndSign,
- };
|