decrypt notes

This commit is contained in:
2023-05-22 20:48:53 +02:00
parent d45ceb9b41
commit b7e5da2354
11 changed files with 152 additions and 24 deletions

15
src/utils/crypto.ts Normal file
View File

@@ -0,0 +1,15 @@
import CryptoJS from 'crypto-js'
const encryptionPrefix = 'contexted|'
const salt = 'salt'
export const calculateClientKey = (passphrase: string): ClientKey => {
return CryptoJS.PBKDF2(passphrase, salt, { keySize: 256 / 32 }).toString()
}
export const decrypt = (encryptedMessage: string, key: string): string => {
const decryptedMessage = CryptoJS.AES.decrypt(encryptedMessage, key).toString(CryptoJS.enc.Utf8)
if (!decryptedMessage.startsWith(encryptionPrefix))
throw new Error("Message doesn't have valid encryption")
return decryptedMessage.substring(encryptionPrefix.length)
}