decrypt notes
This commit is contained in:
15
src/utils/crypto.ts
Normal file
15
src/utils/crypto.ts
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user