How to use libp2p-crypto - 10 common examples

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 richardschneider / ipfs-encryption / test / peerid.js View on Github external
it('encoded public key with JWT', (done) => {
    const jwk = {
      kty: 'RSA',
      n: 'tkiqPxzBWXgZpdQBd14o868a30F3Sc43jwWQG3caikdTHOo7kR14o-h12D45QJNNQYRdUty5eC8ItHAB4YIH-Oe7DIOeVFsnhinlL9LnILwqQcJUeXENNtItDIM4z1ji1qta7b0mzXAItmRFZ-vkNhHB6N8FL1kbS3is_g2UmX8NjxAwvgxjyT5e3_IO85eemMpppsx_ZYmSza84P6onaJFL-btaXRq3KS7jzXkzg5NHKigfjlG7io_RkoWBAghI2smyQ5fdu-qGpS_YIQbUnhL9tJLoGrU72MufdMBZSZJL8pfpz8SB9BBGDCivV0VpbvV2J6En26IsHL_DN0pbIw',
      e: 'AQAB',
      alg: 'RS256',
      kid: '2011-04-29'
    }
    // console.log('jwk', jwk)
    const rsa = new rsaClass.RsaPublicKey(jwk)
    // console.log('rsa', rsa)
    rsa.hash((err, keyId) => {
      // console.log('err', err)
      // console.log('keyId', keyId)
      // console.log('id decoded', multihash.decode(keyId))
      const kids = multihash.toB58String(keyId)
      // console.log('id', kids)
      expect(kids).to.equal(peer.toB58String())
      done()
    })
  })
github richardschneider / ipfs-encryption / test / peerid.js View on Github external
it('encoded public key with DER', (done) => {
    const jwk = rsaUtils.pkixToJwk(publicKeyDer)
    // console.log('jwk', jwk)
    const rsa = new rsaClass.RsaPublicKey(jwk)
    // console.log('rsa', rsa)
    rsa.hash((err, keyId) => {
      // console.log('err', err)
      // console.log('keyId', keyId)
      // console.log('id decoded', multihash.decode(keyId))
      const kids = multihash.toB58String(keyId)
      // console.log('id', kids)
      expect(kids).to.equal(peer.toB58String())
      done()
    })
  })
github richardschneider / ipfs-encryption / test / peerid.js View on Github external
it('encoded public key with DER', (done) => {
    const jwk = rsaUtils.pkixToJwk(publicKeyDer)
    // console.log('jwk', jwk)
    const rsa = new rsaClass.RsaPublicKey(jwk)
    // console.log('rsa', rsa)
    rsa.hash((err, keyId) => {
      // console.log('err', err)
      // console.log('keyId', keyId)
      // console.log('id decoded', multihash.decode(keyId))
      const kids = multihash.toB58String(keyId)
      // console.log('id', kids)
      expect(kids).to.equal(peer.toB58String())
      done()
    })
  })
github libp2p / js-libp2p-keychain / test / peerid.js View on Github external
it('decoded public key', async () => {
    // get protobuf version of the public key
    const publicKeyProtobuf = peer.marshalPubKey()
    const publicKey = crypto.keys.unmarshalPublicKey(publicKeyProtobuf)
    publicKeyDer = publicKey.marshal()

    // get protobuf version of the private key
    const privateKeyProtobuf = peer.marshalPrivKey()
    const key = await promisify(crypto.keys.unmarshalPrivateKey, {
      context: crypto.keys
    })(privateKeyProtobuf)
    expect(key).to.exist()
  })
github libp2p / js-libp2p-crypto-secp256k1 / test / secp256k1.spec.js View on Github external
/* eslint-env mocha */
'use strict'

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

const libp2pCrypto = require('libp2p-crypto')
const keysPBM = libp2pCrypto.keys.keysPBM
const randomBytes = libp2pCrypto.randomBytes
const crypto = require('../src/crypto')(randomBytes)

describe('secp256k1 keys', () => {
  let key
  const secp256k1 = require('../src')(keysPBM, randomBytes)

  before(async () => {
    key = await secp256k1.generateKeyPair()
  })

  it('generates a valid key', async () => {
    expect(key).to.be.an.instanceof(secp256k1.Secp256k1PrivateKey)
    expect(key.public).to.be.an.instanceof(secp256k1.Secp256k1PublicKey)

    const digest = await key.hash()
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 ipfs / js-ipfs / src / core / ipns / republisher.js View on Github external
await this._republishEntry(privateKey)
    } catch (err) {
      const errMsg = 'cannot republish entry for the node\'s private key'

      log.error(errMsg)
      return
    }

    // keychain needs pass to get the cryptographic keys
    if (pass) {
      try {
        const keys = await this._keychain.listKeys()

        for (const key in keys) {
          const pem = await this._keychain.exportKey(key.name, pass)
          const privKey = await crypto.keys.import(pem, pass)

          await this._republishEntry(privKey)
        }
      } catch (err) {
        log.error(err)
      }
    }
  }
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 textileio / encryptoid / src / main.js View on Github external
button.addEventListener('click', (e) => {
      output.innerHTML = ''
      if (!password.validity.valid) {
        return
      }
      e.preventDefault()
      // Compute a derived key to use in AES encryption algorithm
      // We aren't ever storing passwords, so no need to worry about salt
      const key = crypto.pbkdf2(password.value, 'encryptoid', 5000, 24, 'sha2-256')
      // We're only using the key once, so a fixed IV should be ok
      const iv = Buffer.from([...Array(16).keys()])
      // Create AES encryption object
      crypto.aes.create(Buffer.from(key), iv, (err, cipher) => {
        if (!err) {
          if (isDecrypting) {
            cipher.decrypt(Buffer.from(message.value, 'base64'), async (err, plaintext) => {
              if (!err) {
                const info = `Your super secret message is:
                `
                const msg = `${plaintext.toString('utf-8')}`
                const create = `<br><a href="${base}">create your own...</a>`
                output.innerText = info + '"' + msg + '"'
                output.innerHTML = output.innerHTML + create
              }
            })
          } else {
            cipher.encrypt(Buffer.from(message.value), async (err, encrypted) =&gt; {
              if (!err) {
                const hashed = (await ipfs.files.add(encrypted))[0]