How to use the libsodium-wrappers.crypto_generichash function in libsodium-wrappers

To help you get started, we’ve selected a few libsodium-wrappers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sjudson / paseto.js / lib / protocol / V2.js View on Github external
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
github ecadlabs / taquito / packages / taquito-signer / src / ec-key.ts View on Github external
async publicKeyHash(): Promise {
    await sodium.ready;
    return b58cencode(
      sodium.crypto_generichash(20, new Uint8Array(this._publicKey)),
      pref[this.curve].pkh
    );
  }
github airgap-it / airgap-coin-lib / src / protocols / tezos / TezosProtocol.ts View on Github external
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
  }
github ecadlabs / taquito / packages / taquito-signer / src / taquito-signer.ts View on Github external
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);
  }
github TankerHQ / sdk-js / packages / crypto / src / hash.js View on Github external
export function generichash(data: Uint8Array, bytesize: number = 32): Uint8Array {
  return sodium.crypto_generichash(bytesize, data, null);
}
github airgap-it / airgap-coin-lib / src / protocols / tezos / TezosProtocol.ts View on Github external
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')
  }