How to use the libp2p-crypto.randomBytes function in libp2p-crypto

To help you get started, we’ve selected a few libp2p-crypto 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 libp2p / js-libp2p / test / peer-discovery / index.node.js View on Github external
const getConfig = (peerInfo) => mergeOptions(baseOptions, {
      peerInfo,
      modules: {
        peerDiscovery: [MulticastDNS]
      },
      config: {
        peerDiscovery: {
          autoDial: false,
          mdns: {
            enabled: true,
            interval: 200, // discover quickly
            // use a random tag to prevent CI collision
            serviceTag: crypto.randomBytes(10).toString('hex')
          }
        }
      }
    })
github libp2p / js-libp2p / src / pnet / index.js View on Github external
async protect (connection) {
    assert(connection, Errors.NO_HANDSHAKE_CONNECTION)

    // Exchange nonces
    log('protecting the connection')
    const localNonce = crypto.randomBytes(NONCE_LENGTH)

    const shake = handshake(connection)
    shake.write(localNonce)

    const result = await shake.reader.next(NONCE_LENGTH)
    const remoteNonce = result.value.slice()
    shake.rest()

    // Create the boxing/unboxing pipe
    log('exchanged nonces')
    const [internal, external] = duplexPair()
    pipe(
      external,
      // Encrypt all outbound traffic
      createBoxStream(localNonce, this.psk),
      shake.stream,
github libp2p / js-libp2p-keychain / src / keychain.js View on Github external
static generateOptions () {
    const options = Object.assign({}, defaultOptions)
    const saltLength = Math.ceil(NIST.minSaltLength / 3) * 3 // no base64 padding
    options.dek.salt = crypto.randomBytes(saltLength).toString('base64')
    return options
  }
github peer-base / peer-base / test / shared.spec.js View on Github external
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect

const MemoryDatastore = require('interface-datastore').MemoryDatastore
const crypto = require('libp2p-crypto')

const Shared = require('../src/collaboration/shared')
const Store = require('../src/collaboration/store')
const CRDT = require('../src/collaboration/crdt')
const Keys = require('../src/keys')
require('./utils/fake-crdt')

const key = crypto.randomBytes(16)
const iv = crypto.randomBytes(16)

const storeOptions = {
  maxDeltaRetention: 0,
  deltaTrimTimeoutMS: 0,
  createCipher: () => {
    return new Promise((resolve, reject) => {
      crypto.aes.create(Buffer.from(key), Buffer.from(iv), (err, key) => {
        if (err) {
          return reject(err)
        }
        resolve(key)
      })
    })
  }
}
github peer-base / peer-base / test / shared.spec.js View on Github external
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect

const MemoryDatastore = require('interface-datastore').MemoryDatastore
const crypto = require('libp2p-crypto')

const Shared = require('../src/collaboration/shared')
const Store = require('../src/collaboration/store')
const CRDT = require('../src/collaboration/crdt')
const Keys = require('../src/keys')
require('./utils/fake-crdt')

const key = crypto.randomBytes(16)
const iv = crypto.randomBytes(16)

const storeOptions = {
  maxDeltaRetention: 0,
  deltaTrimTimeoutMS: 0,
  createCipher: () => {
    return new Promise((resolve, reject) => {
      crypto.aes.create(Buffer.from(key), Buffer.from(iv), (err, key) => {
        if (err) {
          return reject(err)
        }
        resolve(key)
      })
    })
  }
}
github libp2p / js-iprs-record / test / validator.spec.js View on Github external
PeerId.createFromPrivKey(key.bytes, (err, id) => {
    if (err) {
      return callback(err)
    }
    let rec
    try {
      rec = new Record(k, crypto.randomBytes(10), id)
    } catch (err) {
      return callback(err)
    }
    callback(null, rec)
  })
}
github peer-base / peer-base / test / collaboration-protocol.spec.js View on Github external
before(() => {
    const key = crypto.randomBytes(16)
    const iv = crypto.randomBytes(16)
    storeOptions = Object.assign({}, _storeOptions, {
      createCipher: () => {
        return new Promise((resolve, reject) => {
          crypto.aes.create(Buffer.from(key), Buffer.from(iv), (err, key) => {
            if (err) {
              return reject(err)
            }
            resolve(key)
          })
        })
      }
    })
  })
github peer-base / peer-base / test / collaboration-protocol.spec.js View on Github external
before(() => {
    const key = crypto.randomBytes(16)
    const iv = crypto.randomBytes(16)
    storeOptions = Object.assign({}, _storeOptions, {
      createCipher: () => {
        return new Promise((resolve, reject) => {
          crypto.aes.create(Buffer.from(key), Buffer.from(iv), (err, key) => {
            if (err) {
              return reject(err)
            }
            resolve(key)
          })
        })
      }
    })
  })
github libp2p / js-libp2p / src / ping / index.js View on Github external
async function ping (node, peer) {
  log('dialing %s to %s', PROTOCOL, peer.id.toB58String())

  const { stream } = await node.dialProtocol(peer, PROTOCOL)

  const start = new Date()
  const data = crypto.randomBytes(PING_LENGTH)

  const [result] = await pipe(
    [data],
    stream,
    toBuffer,
    collect
  )
  const end = Date.now()

  if (!data.equals(result)) {
    throw errCode(new Error('Received wrong ping ack'), 'ERR_WRONG_PING_ACK')
  }

  return end - start
}
github libp2p / js-libp2p-floodsub / src / utils.js View on Github external
exports.randomSeqno = () => {
  return crypto.randomBytes(20)
}