perf: 优化加密传输 (#1739)

* perf: 优化加密

* perf: 优化加密传输

Co-authored-by: ibuler <ibuler@qq.com>
This commit is contained in:
fit2bot
2022-05-10 17:41:14 +08:00
committed by GitHub
parent 441c1eb91d
commit e78e0cf132
5 changed files with 30 additions and 69 deletions

View File

@@ -1,40 +0,0 @@
<template>
<PasswordInput
:value="value"
:attrs="attrs"
@input="handleInput"
/>
</template>
<script>
import PasswordInput from './PasswordInput'
import { encryptPassword } from '@/utils/jms'
export default {
name: 'EncryptedPassword',
components: { PasswordInput },
props: {
value: {
type: String,
default: ''
}
},
data() {
return {
attrs: {
}
}
},
computed: {
},
methods: {
handleInput(value) {
const cipher = encryptPassword(value)
this.$emit('input', cipher)
}
}
}
</script>
<style lang="scss" scoped>
</style>

View File

@@ -1,7 +1,6 @@
import DatetimeRangePicker from './DatetimeRangePicker'
import Link from './Link'
import PasswordInput from './PasswordInput'
import EncryptedPassword from './EncryptedPassword'
import Select2 from './Select2'
import Swicher from './Swicher'
import UploadField from './UploadField'
@@ -24,8 +23,7 @@ export default {
WeekCronSelect,
UpdateToken,
JsonEditor,
Text,
EncryptedPassword
Text
}
export {
@@ -40,6 +38,5 @@ export {
WeekCronSelect,
UpdateToken,
JsonEditor,
Text,
EncryptedPassword
Text
}

View File

@@ -16,7 +16,7 @@
<script>
import AutoDataForm from '@/components/AutoDataForm'
import { getUpdateObjURL } from '@/utils/common'
import { encryptPassword } from '@/utils/jms'
import { encryptPassword } from '@/utils/crypto'
export default {
name: 'GenericCreateUpdateForm',
@@ -223,7 +223,7 @@ export default {
},
encryptedFields: {
type: Array,
default: () => ['password', 'token']
default: () => ['password', 'token', 'private_key']
}
},
data() {

View File

@@ -1,5 +1,6 @@
import { JSEncrypt } from 'jsencrypt'
import CryptoJS from 'crypto-js'
import VueCookie from 'vue-cookie'
export function fillKey(key) {
let keySize = 128
@@ -8,16 +9,16 @@ export function fillKey(key) {
key = key.slice(0, 32)
keySize = keySize * 2
}
key = key.slice(0, keySize)
const filledKeyLength = keySize / 8
if (key.length >= filledKeyLength) {
return key.slice(0, filledKeyLength)
}
const filledKey = Buffer.alloc(keySize / 8)
const keys = Buffer.from(key)
if (keys.length < filledKey.length) {
for (let i = 0; i < filledKey.length; i++) {
filledKey[i] = keys[i]
}
} else {
return keys
for (let i = 0; i < keys.length; i++) {
filledKey[i] = keys[i]
}
return filledKey
}
export function aesEncrypt(text, originKey) {
@@ -34,5 +35,23 @@ export function rsaEncrypt(text, pubKey) {
return jsEncrypt.encrypt(text)
}
export function getCookie(name) {
return VueCookie.get(name)
}
export function encryptPassword(password) {
if (!password) {
return ''
}
const aesKey = (Math.random() + 1).toString(36).substring(2)
// public key 是 base64 存储的
const rsaPublicKeyText = getCookie('jms_public_key')
.replaceAll('"', '')
const rsaPublicKey = atob(rsaPublicKeyText)
const keyCipher = rsaEncrypt(aesKey, rsaPublicKey)
const passwordCipher = aesEncrypt(password, aesKey)
return `${keyCipher}:${passwordCipher}`
}
window.aesEncrypt = aesEncrypt

View File

@@ -1,7 +1,5 @@
import store from '@/store'
import { constantRoutes } from '@/router'
import { JSEncrypt } from 'jsencrypt'
import VueCookie from 'vue-cookie'
export function openTaskPage(taskId) {
window.open(`/#/ops/celery/task/${taskId}/log/`, '', 'width=900,height=600')
@@ -134,16 +132,3 @@ export function getConstRouteName() {
addRoutes(names, constRoutes)
return names
}
export function encryptPassword(password) {
if (!password) {
return ''
}
var rsaPublicKeyText = VueCookie.get('jms_public_key')
.replaceAll('"', '')
var rsaPublicKey = atob(rsaPublicKeyText)
var jsencrypt = new JSEncrypt()
jsencrypt.setPublicKey(rsaPublicKey)
var value = jsencrypt.encrypt(password)
return value
}