Skip to content

Commit

Permalink
fix: use crypto-js intead of fucking crypto-es
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Sep 9, 2023
1 parent f2ae6c4 commit 0a7dbb0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@nuxtjs/stylelint-module": "^5.1.0",
"@pinia-plugin-persistedstate/nuxt": "^1.1.1",
"@pinia/nuxt": "^0.4.11",
"@types/crypto-js": "^4.1.2",
"@types/md5": "^2.3.2",
"@types/node": "^20.5.9",
"@types/validator": "^13.11.1",
Expand All @@ -41,7 +42,7 @@
"@vueuse/nuxt": "^10.4.1",
"animate.css": "^4.1.1",
"ant-design-vue": "~4.0.2",
"crypto-es": "^2.0.4",
"crypto-js": "^4.1.1",
"dayjs": "^1.11.9",
"echarts": "^5.4.3",
"eslint": "^8.48.0",
Expand Down
17 changes: 12 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions utils/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import crypto from 'crypto-es'
import cryptoJS from 'crypto-js'
import type { Serializer } from 'pinia-plugin-persistedstate'

const getKey = () => {
const runtimeConfig = useRuntimeConfig()
return runtimeConfig.public.encrypt.cookiesKey
}

export function getCookiesSerializer(): Serializer {
return {
serialize: (value) => {
const runtimeConfig = useRuntimeConfig()
const key = runtimeConfig.public.encrypt.cookiesKey
const key = getKey()
const data = JSON.stringify(value)
return crypto.AES.encrypt(data, key).toString(crypto.format.Hex)
return cryptoJS.AES.encrypt(data, key).toString()
},
deserialize: (value: string) => {
const runtimeConfig = useRuntimeConfig()
const key = runtimeConfig.public.encrypt.cookiesKey
const data = crypto.AES.decrypt(
crypto.enc.Hex.parse(value),
key
).toString(crypto.enc.Utf8)
const key = getKey()
const data = cryptoJS.AES.decrypt(value, key).toString(cryptoJS.enc.Utf8)
return JSON.parse(data)
}
}
Expand Down

0 comments on commit 0a7dbb0

Please sign in to comment.