import {sm2 as sm5, sm2 as sm4, sm2 as sm3, sm2} from "sm-crypto"

// 公钥Q
let publicKey2 = '041967638ca43d4577d8dba166bff4437fde944270101f398a95b846ec2f8177d09f8abc5d62b6cd2c7216274d7abe0c8e04b0bb691207a32dd2e12d6bd2798672'
// 私钥D
let privateKey1 = '27ce6eec39dbf3b564a77c4da1e129fe1ba01a92f6d61055a33ed14ffcbc949e'


// 加密：
export function doEncrypt(msgString) {
  let sm2 = require('sm-crypto').sm2;
  // 1 - C1C3C2；	0 - C1C2C3；	默认为1
  let cipherMode = 1

  // 加密结果
  let encryptData = sm2.doEncrypt(msgString, publicKey2, cipherMode);
  // 加密后的密文前需要添加04，后端才能正常解密
  // let encrypt = '04' + encryptData;
  return encryptData;
}

// 解密
export function doDecryptStr(enStr) {
  let sm2 = require('sm-crypto').sm2;
  // 1 - C1C3C2；	0 - C1C2C3；	默认为1
  let cipherMode = 1
  // 解密结果
  let doDecrypt = sm2.doDecrypt(enStr, privateKey1, cipherMode);
  // 解密后类型转换
  // let objData = JSON.parse(doDecrypt)
  return doDecrypt;
}

// 签名
export function doSign(msgString) {
  let sm2 = require('sm-crypto').sm2;
  // 1 - C1C3C2；	0 - C1C2C3；	默认为1
  let cipherMode = 1
  // 签名
  let sign = sm2.doSignature(msgString, privateKey1, {hash: true, der: false})
  return sign;
}

// 验签
export function doVerifySignature(msgString, sigValueHex) {
  let sm2 = require('sm-crypto').sm2;
  // 1 - C1C3C2；	0 - C1C2C3；	默认为1
  let cipherMode = 1
  // 签名
  let verifyResult = sm2.doVerifySignature(msgString, sigValueHex, publicKey2, {hash: true, der: false}) // 验签结果
  return verifyResult;
}

// 前端user信息加密
export function userinfoEncrypt(str) {
  let sm2 = require('sm-crypto').sm2;
  let cipherMode = 1
  // 加密
  let encryptData = sm2.doEncrypt(str, '0460ff8c8c306fe62f6f9d11c5c82c30d10bbbc703da094e423072cac7dc663c97fad52eccb34f311f47a07f280de157ba4f2aa659cabe749121384b9376ea2ed2', cipherMode);
  return encryptData
}
// 前端user信息解密
export function userinfoDecrypt(str) {
  let sm2 = require('sm-crypto').sm2;
  let cipherMode = 1
  // 加密
  let decryptData = sm2.doDecrypt(str, "27ce6eec39dbf3b564a77c4da1e129fe1ba01a92f6d61055a33ed14ffcbc949e", cipherMode);
  return decryptData
}

export function test() {
  let sm2 = require('sm-crypto').sm2;
  let cipherMode = 1
  // 加密
  let encryptData = sm2.doEncrypt('1122加密', '0460ff8c8c306fe62f6f9d11c5c82c30d10bbbc703da094e423072cac7dc663c97fad52eccb34f311f47a07f280de157ba4f2aa659cabe749121384b9376ea2ed2', cipherMode);
  let sm3 = require('sm-crypto').sm2;
  // 签名
  let sign = sm3.doSignature('1122加密', '6155d63ee27cbeca07f3e40c4f8856f1be8119fcbda1aadc7e0e595e52bad7bd')
  // 解密
  let sm4 = require('sm-crypto').sm2;
  let doDecrypt = sm4.doDecrypt(encryptData, privateKey1, cipherMode);
  alert('解密：' + doDecrypt)
  // 验签
  let sm5 = require('sm-crypto').sm2;
  let verifyResult = sm5.doVerifySignature(doDecrypt, sign, publicKey2) // 验签结果
  alert('验签：' + verifyResult)
}
