Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function encryptSecretForRevealer(
messageSecret: Uint8Array,
nonce: Uint8Array,
revealerPublicKey: Uint8Array,
secretKey: Uint8Array
) {
return nacl.box(messageSecret, nonce, revealerPublicKey, secretKey);
}
encryptMessage(message, nonce, publicKey) {
try {
if (!(publicKey instanceof Uint8Array)) {
publicKey = naclUtil.decodeBase64(publicKey);
}
if (!(nonce instanceof Uint8Array)) {
nonce = naclUtil.decodeUTF8(nonce);
}
if (!(message instanceof Uint8Array)) {
message = naclUtil.decodeUTF8(message);
}
return naclUtil.encodeBase64(nacl.box(message, nonce, publicKey, this.encryptionKeyPair.secretKey));
} catch (e) {
return '';
}
}
.reduce(function(memo, publicKey) {
var nonce = nacl.randomBytes(nacl.box.nonceLength)
memo[nacl.util.encodeBase64(publicKey)] = {
nonce: nacl.util.encodeBase64(nonce),
encryptedKey: nacl.util.encodeBase64(nacl.box(
key,
nonce,
publicKey,
ephemeralKey.secretKey
))
}
return memo
}, {})
ids.forEach(function (id, index) {
debug('Adding recipient ' + id)
var nonce = nacl.randomBytes(24)
var publicKey = publicKeyFromId(id)
debug('Using nonce ' + hex(nonce))
var decryptInfo = {
senderID: senderInfo.id,
recipientID: id,
fileInfo: fileInfo
}
decryptInfo.fileInfo = nacl.util.encodeBase64(nacl.box(
nacl.util.decodeUTF8(JSON.stringify(decryptInfo.fileInfo)),
nonce,
publicKey,
senderInfo.secretKey
))
decryptInfo = nacl.util.encodeBase64(nacl.box(
nacl.util.decodeUTF8(JSON.stringify(decryptInfo)),
nonce,
publicKey,
ephemeral.secretKey
))
header.decryptInfo[nacl.util.encodeBase64(nonce)] = decryptInfo
})
asymEncrypt (msg, toPublic, { nonce } = {}) {
nonce = nonce || Keyring.randomNonce()
toPublic = nacl.util.decodeBase64(toPublic)
if (typeof msg === 'string') {
msg = nacl.util.decodeUTF8(msg)
}
const ephemneralKeypair = nacl.box.keyPair()
const ciphertext = nacl.box(msg, nonce, toPublic, ephemneralKeypair.secretKey)
return {
nonce: nacl.util.encodeBase64(nonce),
ephemeralFrom: nacl.util.encodeBase64(ephemneralKeypair.publicKey),
ciphertext: nacl.util.encodeBase64(ciphertext)
}
}
function encryptPacket(bugout, pk, packet) {
if (bugout.peers[bugout.address(pk)]) {
var nonce = nacl.randomBytes(nacl.box.nonceLength);
packet = bencode.encode({
"n": nonce,
"ek": bs58.encode(Buffer.from(bugout.keyPairEncrypt.publicKey)),
"e": nacl.box(packet, nonce, bs58.decode(bugout.peers[bugout.address(pk)].ek), bugout.keyPairEncrypt.secretKey),
});
} else {
throw bugout.address(pk) + " not seen - no encryption key.";
}
return packet;
}
function encrypt (data, theirPublicKey, mySecretKey) {
data = naclUtil.decodeUTF8(data)
theirPublicKey = convert.publicKey(naclUtil.decodeBase64(theirPublicKey))
mySecretKey = convert.secretKey(naclUtil.decodeBase64(mySecretKey))
const nonce = nacl.randomBytes(nacl.box.nonceLength)
data = nacl.box(data, nonce, theirPublicKey, mySecretKey)
return {
data: naclUtil.encodeBase64(data),
nonce: naclUtil.encodeBase64(nonce)
}
}
export function encryptFor(messageWithoutNonce, theirPublicKey, mySecretKey) {
const theirPublicKeyUint8Array = convertBase64StringToUint8Array(theirPublicKey);
const mySecretKeyUint8Array = convertBase64StringToUint8Array(mySecretKey);
const nonce = newNonce();
const message = convertStringToUint8Array(messageWithoutNonce);
const box = NaCl.box(
message,
nonce,
theirPublicKeyUint8Array,
mySecretKeyUint8Array);
const fullMessage = new Uint8Array(nonce.length + box.length);
fullMessage.set(nonce);
fullMessage.set(message, nonce.length);
const base64FullMessage = convertUint8ArrayToBase64String(fullMessage);
return base64FullMessage;
}
export const box: NaclInterface['box'] = (
messageInBytes,
nonceInBytes,
convertedPublicKey,
convertedPrivateKey,
) =>
Buffer.from(
tweetnacl.box(
messageInBytes,
nonceInBytes,
convertedPublicKey,
convertedPrivateKey,
),
);