Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
case 'Authcrypt':
if (!senderVK) {
throw new Error('Sender public key not provided in Authcrypt message')
}
break
case 'Anoncrypt':
break
default:
throw new Error(`Unsupported pack algorithm: ${recipsOuter.alg}`)
}
const ciphertext = b64dec(wrapper.ciphertext)
nonce = b64dec(wrapper.iv)
const mac = b64dec(wrapper.tag)
const message = sodium.to_string(
sodium.crypto_aead_chacha20poly1305_ietf_decrypt_detached(
null, // nsec
ciphertext,
mac,
wrapper.protected, // ad
nonce, // npub
cek
)
)
return {
message,
sender_key: senderVK,
recipient_key: recipient.header.kid
}
})
function b64decStr (input) {
return sodium.to_string(sodium.from_base64(input, sodium.base64_variants.URLSAFE))
}
export function toString(bytes: Uint8Array): string {
if (!(bytes instanceof Uint8Array))
throw new TypeError('"bytes" is not a Uint8Array');
return sodium.to_string(bytes);
}
decryptMetadata(data) {
if (this.metadata) {
return this.metadata;
}
let header = sodium.from_base64(data.header, sodium.base64_variants.ORIGINAL_NO_PADDING);
let encryptedMetadata = sodium.from_base64(data.encryptedMetadata, sodium.base64_variants.ORIGINAL_NO_PADDING);
this.state = sodium.crypto_secretstream_xchacha20poly1305_init_pull(header, this.key);
let { message, tag } = sodium.crypto_secretstream_xchacha20poly1305_pull(this.state, encryptedMetadata);
message = JSON.parse(sodium.to_string(message));
if (tag !== sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) {
throw new Error('metadata chunk not ended with final tag');
}
this.metadata = Object.assign({}, data, message);
return this.metadata;
}