Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function aeadEncrypt(key, header, plaintext, footer, nonce) {
// build nonce
const nlen = sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES;
const nkey = nonce || sodium.randombytes_buf(nlen);
const _nonce = sodium.crypto_generichash(nlen, plaintext, nkey);
nonce = Buffer.from(_nonce);
// encrypt
let ad;
try {
ad = utils.pae(header, nonce, footer);
} catch (ex) {
return done(ex);
}
const _ciphertext = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(plaintext, ad, null, nonce, key.raw());
const ciphertext = Buffer.from(_ciphertext);
// format
async publicKeyHash(): Promise {
await sodium.ready;
return b58cencode(
sodium.crypto_generichash(20, new Uint8Array(this._publicKey)),
pref[this.curve].pkh
);
}
public async getAddressFromPublicKey(publicKey: string): Promise {
await sodium.ready
const payload: Uint8Array = sodium.crypto_generichash(20, Buffer.from(publicKey, 'hex'))
const address: string = bs58check.encode(Buffer.concat([this.tezosPrefixes.tz1, Buffer.from(payload)]))
return address
}
async sign(bytes: string, watermark?: Uint8Array) {
let bb = hex2buf(bytes);
if (typeof watermark !== 'undefined') {
bb = mergebuf(watermark, bb);
}
const bytesHash = toBuffer(sodium.crypto_generichash(32, bb));
return this._key.sign(bytes, bytesHash);
}
export function generichash(data: Uint8Array, bytesize: number = 32): Uint8Array {
return sodium.crypto_generichash(bytesize, data, null);
}
public async signWithPrivateKey(privateKey: Buffer, transaction: RawTezosTransaction): Promise {
await sodium.ready
const watermark: string = '03'
const watermarkedForgedOperationBytesHex: string = watermark + transaction.binaryTransaction
const watermarkedForgedOperationBytes: Buffer = Buffer.from(watermarkedForgedOperationBytesHex, 'hex')
const hashedWatermarkedOpBytes: Buffer = sodium.crypto_generichash(32, watermarkedForgedOperationBytes)
const opSignature: Buffer = sodium.crypto_sign_detached(hashedWatermarkedOpBytes, privateKey)
const signedOpBytes: Buffer = Buffer.concat([Buffer.from(transaction.binaryTransaction, 'hex'), Buffer.from(opSignature)])
return signedOpBytes.toString('hex')
}