Skip to content

Commit

Permalink
use Web Crypto API; AES-GCM for encrypt and decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuan committed Aug 18, 2024
1 parent c8e4a67 commit 4416030
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 70 deletions.
141 changes: 87 additions & 54 deletions ui/package-lock.json

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

2 changes: 0 additions & 2 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"@types/crypto-js": "^4.2.2",
"axios": "^1.6.8",
"crypto-js": "^4.2.0",
"vue": "^3.4.21",
"vue-router": "^4.3.2"
},
Expand Down
12 changes: 6 additions & 6 deletions ui/src/components/Home.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script setup lang="ts">
import { computed, ref } from 'vue';
import { httpClient } from '@/http/client';
import * as crypto from 'crypto-js';
import { useRouter } from 'vue-router'
import * as crypto from '@/crypto/crypto'
const content = ref("");
const password = ref("");
Expand Down Expand Up @@ -34,19 +33,20 @@ const onCreate = async (event: Event) => {
return
}
const key = crypto.lib.WordArray.random(32).toString()
const keyHex = crypto.secureRandomString(32)
const ivHex = crypto.secureRandomString(16)
const encrypted = crypto.AES.encrypt(content.value, key);
const encrypted = await crypto.encrypt(content.value, keyHex, ivHex)
const payload = {
content: encrypted.toString(),
content: encrypted,
password: password.value,
max_access_count: maxAccessCount.value || 0,
}
const { data } = await httpClient.post<CreateMessageResponse>("/api/messages", payload)
const messagePath = router.resolve({name: 'view', params: {id: data.id, key: key}}).href
const messagePath = router.resolve({name: 'view', params: {id: data.id, key: keyHex, iv: ivHex}}).href
messageURL.value = new URL(messagePath, window.location.origin).href;
}
Expand Down
Loading

0 comments on commit 4416030

Please sign in to comment.