How to use bcrypto - 10 common examples

To help you get started, we’ve selected a few bcrypto 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 handshake-org / hsd / lib / mining / template.js View on Github external
input.witness.pushData(Buffer.alloc(8, 0x00));
    input.witness.pushData(Buffer.alloc(8, 0x00));
    input.witness.compile();

    cb.inputs.push(input);

    // Reward output.
    const output = new Output();
    output.address.fromPubkeyhash(Buffer.alloc(20, 0x00));
    output.value = this.getReward();

    cb.outputs.push(output);

    // Setup coinbase flags (variable size).
    input.witness.setData(0, this.coinbaseFlags);
    input.witness.setData(1, random.randomBytes(8));
    input.witness.compile();

    // Setup output address (variable size).
    output.address = this.address;

    // Add any claims.
    for (const claim of this.claims) {
      const input = new Input();

      input.witness.items.push(claim.blob);

      cb.inputs.push(input);

      let flags = 0;

      if (claim.weak)
github handshake-org / faucet-tool / lib / mnemonic.js View on Github external
getPhrase() {
    if (this.phrase)
      return this.phrase;

    // Include the first `ENT / 32` bits
    // of the hash (the checksum).
    const wbits = this.bits + (this.bits / 32);

    // Get entropy and checksum.
    const entropy = this.getEntropy();
    const chk = sha256.digest(entropy);

    // Append the hash to the entropy to
    // make things easy when grabbing
    // the checksum bits.
    const size = Math.ceil(wbits / 8);
    const data = Buffer.allocUnsafe(size);
    entropy.copy(data, 0);
    chk.copy(data, entropy.length);

    // Build the mnemonic by reading
    // 11 bit indexes from the entropy.
    const list = Mnemonic.getWordlist(this.language);

    let phrase = [];
    for (let i = 0; i < wbits / 11; i++) {
      let index = 0;
github handshake-org / hsd / lib / primitives / tx.js View on Github external
const {base, witness} = this.getSizes();
    const raw = this.encode();

    assert(raw.length === base + witness);

    // Normal data.
    const ndata = raw.slice(0, base);

    // Witness data.
    const wdata = raw.slice(base, base + witness);

    // Left = HASH(normal-data) = normal txid
    const hash = blake2b.digest(ndata);

    // Right = HASH(witness-data)
    const wdhash = blake2b.digest(wdata);

    // WTXID = HASH(normal-txid || witness-data-hash)
    const whash = blake2b.root(hash, wdhash);

    if (!this.mutable) {
      this._hash = hash;
      this._wdhash = wdhash;
      this._whash = whash;
    }

    return [hash, wdhash, whash];
  }
github handshake-org / hsd / lib / protocol / cuckoo.js View on Github external
sipkey(hdr) {
    const hash = blake2b.digest(hdr, 32);

    // Legacy hashing only uses the first 128 bits.
    if (this.siphash32 === siphash32)
      return hash.slice(0, 16);

    return hash;
  }
github handshake-org / bcuckoo / lib / cuckoo.js View on Github external
sipkey(hdr) {
    const hash = blake2b.digest(hdr, 32);

    // Legacy hashing only uses the first 128 bits.
    if (this.siphash32 === siphash32)
      return hash.slice(0, 16);

    return hash;
  }
github handshake-org / faucet-tool / lib / address.js View on Github external
fromWitness(witness) {
    const [, pk] = witness.getPubkeyhashInput();

    // We're pretty much screwed here
    // since we can't get the version.
    if (pk) {
      this.hash = blake2b.digest(pk, 20);
      this.version = 0;
      return this;
    }

    const redeem = witness.getScripthashInput();

    if (redeem) {
      this.hash = blake2b.digest(redeem);
      this.version = 0;
      return this;
    }

    return null;
  }
github handshake-org / faucet-tool / lib / private.js View on Github external
}

    const hash = sha512.mac(seed, SEED_SALT);
    const left = hash.slice(0, 32);
    const right = hash.slice(32, 64);

    // Only a 1 in 2^127 chance of happening.
    if (!secp256k1.privateKeyVerify(left))
      throw new Error('Master private key is invalid.');

    this.depth = 0;
    this.parentFingerPrint = 0;
    this.childIndex = 0;
    this.chainCode = right;
    this.privateKey = left;
    this.publicKey = secp256k1.publicKeyCreate(left, true);

    return this;
  }
github handshake-org / goosig / etc / generate.js View on Github external
function encode(name, data, desc) {
  assert(typeof name === 'string');
  assert(Buffer.isBuffer(data));
  assert(!desc || typeof desc === 'string');

  const blake2b = BLAKE2b.digest(data);
  const sha256 = SHA256.digest(data);
  const sha3 = SHA3.digest(data);
  const hex = data.toString('hex');

  // Digit Sum (used for RSA-2048 -- see challengenumbers.txt)
  const num = BN.fromBuffer(data);
  const base10 = num.toString(10);

  let sum = 0;
  for (let i = 0; i < base10.length; i++)
    sum += Number(base10[i]);

  // Checksum (used for RSA-617 -- see rsa-fact.txt)
  const checksum = num.modrn(991889);

  let out = '';
github handshake-org / hsd / lib / node / node.js View on Github external
if (!key) {
      try {
        const data = fs.readFileSync(filename, 'utf8');
        key = Buffer.from(data.trim(), 'hex');
      } catch (e) {
        if (e.code !== 'ENOENT')
          throw e;
        key = this.genKey();
        fresh = true;
      }
    } else {
      fresh = true;
    }

    if (key.length !== 32 || !secp256k1.privateKeyVerify(key))
      throw new Error('Invalid identity key.');

    if (fresh) {
      // XXX Shouldn't be doing this.
      fs.mkdirpSync(this.config.prefix);
      fs.writeFileSync(filename, key.toString('hex') + '\n');
    }

    return key;
  }
github handshake-org / faucet-tool / lib / private.js View on Github external
fromSeed(seed) {
    assert(Buffer.isBuffer(seed));

    if (seed.length * 8 < common.MIN_ENTROPY
        || seed.length * 8 > common.MAX_ENTROPY) {
      throw new Error('Entropy not in range.');
    }

    const hash = sha512.mac(seed, SEED_SALT);
    const left = hash.slice(0, 32);
    const right = hash.slice(32, 64);

    // Only a 1 in 2^127 chance of happening.
    if (!secp256k1.privateKeyVerify(left))
      throw new Error('Master private key is invalid.');

    this.depth = 0;
    this.parentFingerPrint = 0;
    this.childIndex = 0;
    this.chainCode = right;
    this.privateKey = left;
    this.publicKey = secp256k1.publicKeyCreate(left, true);

    return this;
  }