How to use the libsodium-wrappers.from_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 TankerHQ / sdk-js / packages / crypto / src / __tests__ / hash.spec.js View on Github external
function fromHex(str: string): Uint8Array {
  if (typeof str !== 'string')
    throw new TypeError('"str" is not a string');

  return sodium.from_hex(str);
}
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)
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / identity.ts View on Github external
await sodium.ready

  if (ephemeral) {
    return keyPair()
  }

  let localKeyPair: KeyPair | undefined

  {
    const publicKey = localStorage.getItem(PUBKEY_PROP)
    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 Zefau / ioBroker.roomba / library.js View on Github external
decrypt(key, message)
	{
		try
		{
			message = _sodium.from_hex(message);
			return _sodium.to_string(_sodium.crypto_secretbox_open_easy(message.slice(_sodium.crypto_secretbox_NONCEBYTES), message.slice(0, _sodium.crypto_secretbox_NONCEBYTES), _sodium.from_hex(key)));
		}
		catch(e)
		{
			this._adapter.log.warn(JSON.stringify(e.message));
			return false;
		}
	}