mirror of https://github.com/longtai-cn/hippo4j
Extract the encryption section into a separate utility class (#1319)
* feat(login): #1300 Encrypted transmission of login password * feat(login): #1300 Modify the comments to English * ref(login): #1300 Restore static resources * ref(console-login): Extract the encryption section into a separate utility class --------- Co-authored-by: limingwei <limingwei@century-cn.com> Co-authored-by: Serenity <SerenitySir@outlook.com>pull/1320/head
parent
fa122e4267
commit
bf9572f261
@ -0,0 +1,37 @@
|
||||
import {Buffer} from 'buffer'
|
||||
import crypto from 'crypto'
|
||||
|
||||
/**
|
||||
* generate key
|
||||
* @returns {string} key(length 16)
|
||||
*/
|
||||
export function genKey() {
|
||||
let chars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
let result = '';
|
||||
for (let i = 16; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* encode message
|
||||
* @param msg message
|
||||
* @param key key
|
||||
* @returns {string} encoded message
|
||||
*/
|
||||
export function encrypt(msg, key) {
|
||||
try {
|
||||
let pwd = Buffer.from(key)
|
||||
let iv = crypto.randomBytes(12)
|
||||
let cipher = crypto.createCipheriv('aes-128-gcm', pwd, iv)
|
||||
let enc = cipher.update(msg, 'utf8', 'base64')
|
||||
enc += cipher.final('base64')
|
||||
let tags = cipher.getAuthTag()
|
||||
enc = Buffer.from(enc, 'base64')
|
||||
let totalLength = iv.length + enc.length + tags.length
|
||||
let bufferMsg = Buffer.concat([iv, enc, tags], totalLength)
|
||||
return bufferMsg.toString('base64')
|
||||
} catch (e) {
|
||||
console.log("Encrypt is error", e)
|
||||
return null
|
||||
}
|
||||
}
|
Loading…
Reference in new issue