File size: 1,719 Bytes
81d8726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const CryptoJS = require('crypto-js')
const rawCrypto = require('crypto')
const NodeRSA = require('node-rsa')
const { KeyDB } = require('./db-class')
const keyDB = new KeyDB().getInstance()

// rsa非对称 私钥解密
const RSADecryptAsync = async (ciphertext) => {
  if (!ciphertext) return Promise.reject(new Error('ciphertext is empty'))
  let { privateKey } = await keyDB.findOneAsync({})
  privateKey = await AESDecryptAsync(privateKey) // 先解密私钥
  const rsakey = new NodeRSA(privateKey)
  rsakey.setOptions({ encryptionScheme: 'pkcs1', environment: 'browser' }) // Must Set It When Frontend Use jsencrypt
  const plaintext = rsakey.decrypt(ciphertext, 'utf8')
  return plaintext
}

// aes对称 加密(default commonKey)
const AESEncryptAsync = async (text, key) => {
  if (!text) return Promise.reject(new Error('text is empty'))
  let { commonKey } = await keyDB.findOneAsync({})
  let ciphertext = CryptoJS.AES.encrypt(text, key || commonKey).toString()
  return ciphertext
}

// aes对称 解密(default commonKey)
const AESDecryptAsync = async (ciphertext, key) => {
  if (!ciphertext) return Promise.reject(new Error('ciphertext is empty'))
  let { commonKey } = await keyDB.findOneAsync({})
  let bytes = CryptoJS.AES.decrypt(ciphertext, key || commonKey)
  let originalText = bytes.toString(CryptoJS.enc.Utf8)
  return originalText
}

// sha1 加密(不可逆)
const SHA1Encrypt = (clearText) => {
  return rawCrypto.createHash('sha1').update(clearText).digest('hex')
}

const SHA256Encrypt = (clearText) => {
  return rawCrypto.createHash('sha256').update(clearText).digest('hex')
}

module.exports = {
  RSADecryptAsync,
  AESEncryptAsync,
  AESDecryptAsync,
  SHA1Encrypt,
  SHA256Encrypt
}