Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const recipient = recipsOuter.recipients.find(r => {
return r.header.kid === keys.public58
})
if (!recipient) {
throw new Error('No corresponding recipient key found')
}
let pk = sodium.crypto_sign_ed25519_pk_to_curve25519(keys.public)
let sk = sodium.crypto_sign_ed25519_sk_to_curve25519(keys.private)
let encrytpedKey = b64dec(recipient.encrypted_key)
let nonce = recipient.header.iv ? b64dec(recipient.header.iv) : null
let encSender = recipient.header.sender ? b64dec(recipient.header.sender) : null
let senderVK = null
let cek = null
if (nonce && encSender) {
senderVK = sodium.to_string(sodium.crypto_box_seal_open(encSender, pk, sk))
const senderPK = sodium.crypto_sign_ed25519_pk_to_curve25519(bs58.decode(senderVK))
cek = sodium.crypto_box_open_easy(encrytpedKey, nonce, senderPK, sk)
} else {
cek = sodium.crypto_box_seal_open(encrytpedKey, pk, sk)
}
switch (recipsOuter.alg) {
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}`)
private solveChallenge(keyPair: KeyPair, data: string) {
const challenge = sodium.from_base64(data, sodium.base64_variants.URLSAFE_NO_PADDING)
const nonce = sodium.crypto_box_seal_open(challenge, keyPair.publicKey, keyPair.privateKey)
const decoded = sodium.to_base64(nonce, sodium.base64_variants.URLSAFE_NO_PADDING)
this.send({
t: MessageType.AuthResponse,
c: decoded
})
}
export const unseal = (cipher: Data, publicKey: Key, secretKey: Key) => {
if (cipher.length < sodium.crypto_box_SEALBYTES) return null
let msg
try {
msg = sodium.crypto_box_seal_open(cipher, publicKey, secretKey)
} catch (e) {
return null
}
return msg
}
export function sealDecrypt(cipherText: Uint8Array, recipientKeys: SodiumKeyPair): Uint8Array {
return sodium.crypto_box_seal_open(cipherText, recipientKeys.publicKey, recipientKeys.privateKey);
}
if (!recipient) {
throw new Error('No corresponding recipient key found')
}
let pk = sodium.crypto_sign_ed25519_pk_to_curve25519(keys.public)
let sk = sodium.crypto_sign_ed25519_sk_to_curve25519(keys.private)
let encrytpedKey = b64dec(recipient.encrypted_key)
let nonce = recipient.header.iv ? b64dec(recipient.header.iv) : null
let encSender = recipient.header.sender ? b64dec(recipient.header.sender) : null
let senderVK = null
let cek = null
if (nonce && encSender) {
senderVK = sodium.to_string(sodium.crypto_box_seal_open(encSender, pk, sk))
const senderPK = sodium.crypto_sign_ed25519_pk_to_curve25519(bs58.decode(senderVK))
cek = sodium.crypto_box_open_easy(encrytpedKey, nonce, senderPK, sk)
} else {
cek = sodium.crypto_box_seal_open(encrytpedKey, pk, sk)
}
switch (recipsOuter.alg) {
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)