新增前端密码加密工具
This commit is contained in:
18756
ruoyi-ui/package-lock.json
generated
Normal file
18756
ruoyi-ui/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -8,7 +8,8 @@
|
||||
"dev": "vue-cli-service serve",
|
||||
"build:prod": "vue-cli-service build",
|
||||
"build:stage": "vue-cli-service build --mode staging",
|
||||
"preview": "node build/index.js --preview"
|
||||
"preview": "node build/index.js --preview",
|
||||
"test:password-transfer": "node tests/password-transfer-api.test.js"
|
||||
},
|
||||
"keywords": [
|
||||
"vue",
|
||||
@@ -33,6 +34,7 @@
|
||||
"file-saver": "2.0.5",
|
||||
"fuse.js": "6.4.3",
|
||||
"highlight.js": "9.18.5",
|
||||
"crypto-js": "4.2.0",
|
||||
"js-beautify": "1.13.0",
|
||||
"js-cookie": "3.0.1",
|
||||
"jsencrypt": "3.0.0-rc.1",
|
||||
|
||||
14
ruoyi-ui/src/utils/passwordTransfer.js
Normal file
14
ruoyi-ui/src/utils/passwordTransfer.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import CryptoJS from 'crypto-js'
|
||||
|
||||
export function encryptPasswordFields(payload, fields, key) {
|
||||
const next = { ...payload }
|
||||
fields.forEach((field) => {
|
||||
if (next[field]) {
|
||||
next[field] = CryptoJS.AES.encrypt(next[field], CryptoJS.enc.Utf8.parse(key), {
|
||||
mode: CryptoJS.mode.ECB,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
}).toString()
|
||||
}
|
||||
})
|
||||
return next
|
||||
}
|
||||
55
ruoyi-ui/tests/password-transfer-api.test.js
Normal file
55
ruoyi-ui/tests/password-transfer-api.test.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const assert = require('assert')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const vm = require('vm')
|
||||
|
||||
function loadModule(filePath, stubs = {}) {
|
||||
const source = fs.readFileSync(filePath, 'utf8')
|
||||
const exportedNames = []
|
||||
const transformed = source
|
||||
.replace(/^import .*$/gm, '')
|
||||
.replace(/export function\s+([A-Za-z0-9_]+)\s*\(/g, (_, name) => {
|
||||
exportedNames.push(name)
|
||||
return `function ${name}(`
|
||||
})
|
||||
.replace(/export default\s+/g, 'module.exports = ')
|
||||
|
||||
const sandbox = {
|
||||
module: { exports: {} },
|
||||
exports: {},
|
||||
require,
|
||||
console,
|
||||
process: {
|
||||
env: {
|
||||
VUE_APP_PASSWORD_TRANSFER_KEY: '1234567890abcdef'
|
||||
}
|
||||
},
|
||||
...stubs
|
||||
}
|
||||
|
||||
vm.runInNewContext(
|
||||
`${transformed}\nmodule.exports = { ${exportedNames.join(', ')} };`,
|
||||
sandbox,
|
||||
{ filename: filePath }
|
||||
)
|
||||
|
||||
return sandbox.module.exports
|
||||
}
|
||||
|
||||
const passwordTransferModule = loadModule(
|
||||
path.resolve(__dirname, '../src/utils/passwordTransfer.js'),
|
||||
{ CryptoJS: require('crypto-js') }
|
||||
)
|
||||
|
||||
const { encryptPasswordFields } = passwordTransferModule
|
||||
|
||||
const encrypted = encryptPasswordFields(
|
||||
{ password: 'admin123', code: '8888' },
|
||||
['password'],
|
||||
'1234567890abcdef'
|
||||
)
|
||||
|
||||
assert.notStrictEqual(encrypted.password, 'admin123')
|
||||
assert.strictEqual(encrypted.code, '8888')
|
||||
|
||||
console.log('password-transfer-api test passed')
|
||||
Reference in New Issue
Block a user