added decryptString function and updated fromB64 to parse normal string
This commit is contained in:
parent
7b317afb78
commit
2a1f916794
2 changed files with 23 additions and 1 deletions
|
@ -107,6 +107,15 @@ export async function decryptB64(data: string, nonce: string, key: string) {
|
|||
return await toB64(decrypted);
|
||||
}
|
||||
|
||||
export async function decryptString(data: string, nonce: string, key: string) {
|
||||
await sodium.ready;
|
||||
const decrypted = await decrypt(await fromB64(data),
|
||||
await fromB64(nonce),
|
||||
await fromB64(key));
|
||||
|
||||
return sodium.to_string(decrypted);
|
||||
}
|
||||
|
||||
export async function encrypt(data: Uint8Array, key?: Uint8Array) {
|
||||
await sodium.ready;
|
||||
const uintkey: Uint8Array = key ? key : sodium.crypto_secretbox_keygen();
|
||||
|
@ -173,7 +182,16 @@ export async function boxSealOpen(input: string, publicKey: string, secretKey: s
|
|||
|
||||
export async function fromB64(input: string) {
|
||||
await sodium.ready;
|
||||
return sodium.from_base64(input, sodium.base64_variants.ORIGINAL);
|
||||
let result;
|
||||
try {
|
||||
result = sodium.from_base64(input, sodium.base64_variants.ORIGINAL);
|
||||
} catch (e) {
|
||||
result = await fromB64(await toB64(await fromString(input)));
|
||||
|
||||
}
|
||||
finally {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export async function toB64(input: Uint8Array) {
|
||||
|
|
|
@ -74,6 +74,10 @@ export class Crypto {
|
|||
return libsodium.decryptB64(data, nonce, key)
|
||||
}
|
||||
|
||||
async decryptString(data, nonce, key) {
|
||||
return libsodium.decryptString(data, nonce, key)
|
||||
}
|
||||
|
||||
async encryptToB64(data, key) {
|
||||
return libsodium.encryptToB64(data, key);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue