How to use the safe-buffer.Buffer.from function in safe-buffer

To help you get started, we’ve selected a few safe-buffer 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 freewil / scmp / benchmark / crypto-check.js View on Github external
'use strict'

const crypto = require('crypto')
const Benchmark = require('benchmark')

const scmpCompare = require('../lib/scmpCompare')
const compareFn = crypto.timingSafeEqual || scmpCompare

// `safe-buffer` in case `Buffer.from` in newer versions of node aren't available
const Buffer = require('safe-buffer').Buffer

const HASH1 = Buffer.from('e727d1464ae12436e899a726da5b2f11d8381b26', 'hex')
const HASH2 = Buffer.from('f727d1464ae12436e899a726da5b2f11d8381b26', 'hex')

const suite = new Benchmark.Suite()
suite.add('crypto check each fn call', function () {
  if (crypto.timingSafeEqual) {
    return crypto.timingSafeEqual(HASH1, HASH2)
  }
  return scmpCompare(HASH1, HASH2)
})
  .add('crypto check once', function () {
    return compareFn(HASH1, HASH2)
  })
  .on('cycle', function (event) {
    console.log(String(event.target))
  })
  .on('complete', function () {
github andrerfneves / lightning-decoder / src / lib / bolt11.js View on Github external
}
    // after the tag code, 2 words are used to store the length (in 5 bit words) of the tag data
    // (also left padded, most integers are left padded while buffers are right padded)
    tagWords = tagWords.concat([0].concat(intBEToWords(words.length)).slice(-2))
    // then append the tag data words
    tagWords = tagWords.concat(words)
  })

  // the data part of the bech32 is TIMESTAMP || TAGS || SIGNATURE
  // currently dataWords = TIMESTAMP || TAGS
  let dataWords = timestampWords.concat(tagWords)

  // the preimage for the signing data is the buffer of the prefix concatenated
  // with the buffer conversion of the data words excluding the signature
  // (right padded with 0 bits)
  let toSign = Buffer.concat([Buffer.from(prefix, 'utf8'), Buffer.from(convert(dataWords, 5, 8))])
  // single SHA256 hash for the signature
  let payReqHash = sha256(toSign)

  // signature is 64 bytes (32 byte r value and 32 byte s value concatenated)
  // PLUS one extra byte appended to the right with the recoveryID in [0,1,2,3]
  // Then convert to 5 bit words with right padding 0 bits.
  let sigWords
  if (canReconstruct) {
    /* Since BOLT11 does not require a payee_node_key tag in the specs,
    most parsers will have to recover the pubkey from the signature
    To ensure the tag data has been provided in the right order etc.
    we should check that the data we got and the node key given match when
    reconstructing a payment request from given signature and recoveryID.
    However, if a privatekey is given, the caller is the privkey owner.
    Earlier we check if the private key matches the payee node key IF they
    gave one. */
github opporty-com / Plasma-Cash / plasma-core / lib / p2p / ecies.js View on Github external
_encryptMessage(data, sharedMacData = null) {
    const privateKey = util.genPrivateKey();
    const x = ecdhX(this._remotePublicKey, privateKey);
    const key = concatKDF(x, 32);
    const ekey = key.slice(0, 16); // encryption key
    const mkey = crypto.createHash('sha256').update(key.slice(16, 32)).digest(); // MAC key

    // encrypt
    const IV = crypto.randomBytes(16);
    const cipher = crypto.createCipheriv('aes-128-ctr', ekey, IV);
    const encryptedData = cipher.update(data);
    const dataIV = Buffer.concat([IV, encryptedData]);

    // create tag
    if (!sharedMacData) {
      sharedMacData = Buffer.from([]);
    }
    const tag = crypto.createHmac('sha256', mkey).update(Buffer.concat([dataIV, sharedMacData])).digest();

    const publicKey = secp256k1.publicKeyCreate(privateKey, false);
    return Buffer.concat([publicKey, dataIV, tag]);
  }
github expressjs / body-parser / test / raw.js View on Github external
it('should support gzip encoding', function (done) {
      var test = request(this.server).post('/')
      test.set('Content-Encoding', 'gzip')
      test.set('Content-Type', 'application/octet-stream')
      test.write(Buffer.from('1f8b080000000000000bcb4bcc4db57db16e170099a4bad608000000', 'hex'))
      test.expect(200, 'buf:6e616d653de8aeba', done)
    })
github mcollina / msgpack5 / test / timestamps.js View on Github external
t.test('decoding ' + item, function (t) {
      var buf = Buffer.from(expected)
      var dt = encoder.decode(buf)
      t.equal(dt.toString(), item.toString(), 'must decode correctly\nDecoded:\t' + dt * 1 + '\nExp:\t' + item * 1)
      t.end()
    })
github serialport / node-serialport / test / serialport.js View on Github external
port.write(Buffer.from('123'), () => {
            assert.equal(spy.callCount, 1);
            assert.deepEqual(port.binding.lastWrite, Buffer.from('abc123'));
            done();
          });
          port.uncork();
github bitshares / bitsharesjs / lib / serializer / src / types.js View on Github external
fromObject(object) {
            v.required(object);
            if (Buffer.isBuffer(object)) return object;

            return Buffer.from(object, "hex");
        },
        toObject(object, debug = {}) {
github getyoti / yoti-node-sdk / src / yoti_common / index.js View on Github external
function decipherProfile(cipherText, key, iv) {
  const decipher = forge.cipher.createDecipher('AES-CBC', key);
  const data = forge.util.createBuffer();

  data.putBytes(cipherText);

  decipher.start({ iv });
  decipher.update(data);
  decipher.finish();

  const cipherTextAsBytes = decipher.output.getBytes();

  const attributeList = protoRoot.decodeAttributeList(Buffer.from(forge.util.encode64(cipherTextAsBytes), 'base64'));
  return attributeList;
}
github pablomarle / networkmaps / node_modules / mysql / lib / protocol / Auth.js View on Github external
function xor(a, b) {
  a = Buffer.from(a, 'binary');
  b = Buffer.from(b, 'binary');
  var result = Buffer.allocUnsafe(a.length);
  for (var i = 0; i < a.length; i++) {
    result[i] = (a[i] ^ b[i]);
  }
  return result;
}
Auth.xor = xor;
github crypto-browserify / sha.js / hash.js View on Github external
Hash.prototype.update = function (data, enc) {
  if (typeof data === 'string') {
    enc = enc || 'utf8'
    data = Buffer.from(data, enc)
  }

  var block = this._block
  var blockSize = this._blockSize
  var length = data.length
  var accum = this._len

  for (var offset = 0; offset < length;) {
    var assigned = accum % blockSize
    var remainder = Math.min(length - offset, blockSize - assigned)

    for (var i = 0; i < remainder; i++) {
      block[assigned + i] = data[offset + i]
    }

    accum += remainder