How to use the libsodium-wrappers.to_hex 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 samuelmaddock / metastream / packages / metastream-app / src / platform / web / rtc-coordinator.ts View on Github external
private async authenticatePeer(peer: SimplePeer.Instance, peerId?: string) {
    const peerPublicKey = peerId ? sodium.from_hex(peerId) : undefined
    const userPublicKey = await mutualHandshake(peer, localUser().id, peerPublicKey)

    if (!userPublicKey) {
      const addr = `${(peer as any).remoteAddress}:${(peer as any).remotePort}`
      peer.destroy()
      throw new NetworkError(
        NetworkErrorCode.PeerAuthenticationFailure,
        `Failed to authenticate with peer [${addr}]`
      )
    }

    const userId = sodium.to_hex(userPublicKey)
    const netId = new NetUniqueId(userPublicKey)
    const conn = new RTCPeerConn(netId, peer)

    console.debug(`Authenticated peer ${userId}`, conn)
    this.emit('connection', conn)
  }
}
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / identity.ts View on Github external
if (publicKey && privateKey) {
      localKeyPair = {
        publicKey: sodium.from_hex(publicKey),
        privateKey: sodium.from_hex(privateKey),
        keyType: 'curve25519'
      }
    }
  }

  if (!localKeyPair) {
    localKeyPair = keyPair()

    try {
      localStorage.setItem(PUBKEY_PROP, sodium.to_hex(localKeyPair.publicKey))
      localStorage.setItem(SECKEY_PROP, sodium.to_hex(localKeyPair.privateKey))
    } catch (e) {}
  }

  return localKeyPair
}
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / crypto.ts View on Github external
export const equal = (a: Data, b: Data) => sodium.to_hex(a) === sodium.to_hex(b)
github samuelmaddock / metastream / packages / metastream-signal-server / src / index.ts View on Github external
private validateAuth(client: Client, challenge: string) {
    if (client.status !== ClientStatus.PendingAuth) return

    const secret =
      typeof challenge === 'string' &&
      sodium.from_base64(challenge, sodium.base64_variants.URLSAFE_NO_PADDING)

    const secretHex = secret && sodium.to_hex(secret)
    const authSecretHex = client.authSecret && sodium.to_hex(client.authSecret)

    if (secret && authSecretHex === secretHex) {
      client.status = ClientStatus.Authed

      if (typeof client.pendingRoom === 'string') {
        const room = client.pendingRoom
        client.pendingRoom = undefined
        this.createRoom(client, room)
      }
    } else {
      this.log(`Client [${client.id}] failed to solve challenge: ${authSecretHex} !== ${secretHex}`)
      client.socket.close()
    }
  }
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / identity.ts View on Github external
const privateKey = localStorage.getItem(SECKEY_PROP)

    if (publicKey && privateKey) {
      localKeyPair = {
        publicKey: sodium.from_hex(publicKey),
        privateKey: sodium.from_hex(privateKey),
        keyType: 'curve25519'
      }
    }
  }

  if (!localKeyPair) {
    localKeyPair = keyPair()

    try {
      localStorage.setItem(PUBKEY_PROP, sodium.to_hex(localKeyPair.publicKey))
      localStorage.setItem(SECKEY_PROP, sodium.to_hex(localKeyPair.privateKey))
    } catch (e) {}
  }

  return localKeyPair
}
github samuelmaddock / metastream / packages / metastream-signal-server / src / index.ts View on Github external
private validateAuth(client: Client, challenge: string) {
    if (client.status !== ClientStatus.PendingAuth) return

    const secret =
      typeof challenge === 'string' &&
      sodium.from_base64(challenge, sodium.base64_variants.URLSAFE_NO_PADDING)

    const secretHex = secret && sodium.to_hex(secret)
    const authSecretHex = client.authSecret && sodium.to_hex(client.authSecret)

    if (secret && authSecretHex === secretHex) {
      client.status = ClientStatus.Authed

      if (typeof client.pendingRoom === 'string') {
        const room = client.pendingRoom
        client.pendingRoom = undefined
        this.createRoom(client, room)
      }
    } else {
      this.log(`Client [${client.id}] failed to solve challenge: ${authSecretHex} !== ${secretHex}`)
      client.socket.close()
    }
  }