-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
47 lines (36 loc) · 937 Bytes
/
lib.js
File metadata and controls
47 lines (36 loc) · 937 Bytes
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
const axios = require('axios')
const JSEncrypt = require('node-jsencrypt')
const { sm2 } = require('sm-crypto')
/**
* public key
* @returns {String}
*/
exports.getPublicKey = async function () {
const host = 'https://core.authing.cn'
const api = `${host}/api/v2/.well-known`
const res = await axios.get(api)
return res.data.data.publicKey
}
/**
* @param {String} plainText
* @param {String} publicKey
* @returns
*/
exports.encryptNormal = function encryptNormal (plainText, publicKey) {
const jsencrypt = new JSEncrypt()
jsencrypt.setPublicKey(publicKey)
const encrypted = jsencrypt.encrypt(plainText)
return encrypted || ''
}
/**
* @param {String} plainText
* @param {String} publicKey
* @returns {String}
*/
exports.encryptSM2 = function encryptSM2 (plainText, publicKey) {
const encrypted = sm2.doEncrypt(plainText, publicKey)
if (encrypted) {
return `sm2:${encrypted}`
}
return ''
}