From 67330c1b47b7f4439e18d333b4b6fdb29c6ec402 Mon Sep 17 00:00:00 2001 From: limingwei Date: Mon, 22 May 2023 19:14:41 +0800 Subject: [PATCH] ref(console-login): Extract the encryption section into a separate utility class --- threadpool/console/src/store/modules/user.js | 30 ++-------------- threadpool/console/src/utils/aes-util.js | 37 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 threadpool/console/src/utils/aes-util.js diff --git a/threadpool/console/src/store/modules/user.js b/threadpool/console/src/store/modules/user.js index 59bd3d28..4b73f6b7 100755 --- a/threadpool/console/src/store/modules/user.js +++ b/threadpool/console/src/store/modules/user.js @@ -1,8 +1,7 @@ import {login} from '@/api/user'; import {getToken, removeToken, setToken} from '@/utils/auth'; import router, {resetRouter} from '@/router'; -import {Buffer} from 'buffer' -import crypto from 'crypto' +import {genKey, encrypt} from '@/utils/aes-util'; const state = { token: getToken(), @@ -35,8 +34,8 @@ const actions = { login({commit}, userInfo) { const {username, password} = userInfo; return new Promise((resolve, reject) => { - let key = actions.genKey(); - let encodePassword = actions.encrypt(password, key) + let key = genKey(); + let encodePassword = encrypt(password, key) key = key.split("").reverse().join("") login({username: username.trim(), password: encodePassword, tag: key, rememberMe: 1}) .then((response) => { @@ -122,29 +121,6 @@ const actions = { resolve(); }); }, - genKey() { - let chars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" - let result = ''; - for (let i = 16; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)]; - return result; - }, - 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 - } - }, }; export default { diff --git a/threadpool/console/src/utils/aes-util.js b/threadpool/console/src/utils/aes-util.js new file mode 100644 index 00000000..d4d78736 --- /dev/null +++ b/threadpool/console/src/utils/aes-util.js @@ -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 + } +} \ No newline at end of file