sm2

86 阅读1分钟

/**
 * txt:要加密的字段
 *
 * @return {string}
 */
const sm2 = require('sm-crypto').sm2
export const encrypt = txt => {
  const cipherMode = 1 // 1 - C1C3C2,0 - C1C2C3,默认为1
  const publicKey = '040c0504187b0a4c84eaad0d467a719715c82310d0ef29935afe958449add5e914e0a890c30db1d70ec4faa2d6199941b3b0e7b72d5573bdb6a2d0448e84f4e4eb' // 一般由后端定义
  let encryptData = sm2.doEncrypt(txt, publicKey, cipherMode) // 加密结果
  return encryptData
}


// 解密
export const doDecryptStr = txt => {
  const cipherMode = 1 // 1 - C1C3C2,0 - C1C2C3,默认为1
  const privateKey1 = 'd0ae78d015e76ce9974a4836cbbe5e347089d78244e0f2940109e5f08d3273cf' // 一般由后端定义
  let doDecrypt = sm2.doDecrypt(txt, privateKey1, cipherMode) // 
  return doDecrypt
}


// ajax返回的数组展示经过这一步遍历
// accountLoginName、accountLoginPwd、accountName、accountPhone脱敏字段
export const showDecryptData = function (arr) {
  arr.forEach(item => {
    if (item.accountLoginName) {
      item.accountLoginName = doDecryptStr(item.accountLoginName)
    }
    if (item.accountName) {
      item.accountName = doDecryptStr(item.accountName)
    }
    if (item.accountPhone) {
      item.accountPhone = doDecryptStr(item.accountPhone)
    }
  })
  return arr;
}

// ajax返回的数组展示经过这一步遍历
//applyPersonName、phone脱敏字段
export const showDecryptData1 = function (arr) {
  arr.forEach(item => {
    if (item.applyPersonName) {
      item.applyPersonName = doDecryptStr(item.applyPersonName)
    }
    if (item.phone) {
      item.phone = doDecryptStr(item.phone)
    }

  })
  return arr;
}