How to use the libsodium-wrappers.from_string 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 whs / ipfs-encrypted-share / src / lib / browserencrypt.js View on Github external
return Promise.all([PromiseFileReader.readAsArrayBuffer(file), sodium.ready]).then((res) => {
		let buffer = res[0];
		res = null;

		let key = sodium.crypto_secretstream_xchacha20poly1305_keygen();
		let { state, header } = sodium.crypto_secretstream_xchacha20poly1305_init_push(key);

		// encrypt the metadata
		let piecesCount = Math.ceil(buffer.byteLength / FILE_CHUNK_SIZE);
		let encryptedMetadata = {
			filename: file.name,
			size: file.size,
			pieces: piecesCount,
		};
		let metadataBuffer = sodium.from_string(JSON.stringify(encryptedMetadata));
		let metadataEncrypted = sodium.crypto_secretstream_xchacha20poly1305_push(
			state,
			metadataBuffer,
			null,
			sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL
		);
		metadataBuffer = null;

		// then split the files into pieces
		let promise = new Promise((resolve, reject) => {
			let pieces = [];
			let offset = 0;
			let chunk = () => {
				console.log(`Piece ${offset + 1}/${piecesCount}`);
				let isFinal = offset === piecesCount - 1;
				let length = FILE_CHUNK_SIZE;
github samuelmaddock / metastream / packages / metastream-signal-server / example.js View on Github external
async function main() {
  await sodium.ready

  keypair = sodium.crypto_box_seed_keypair(sodium.from_string('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'))

  let server = await createServer({ port })
  console.log(`Server listening at 127.0.0.1:${port}`)

  const clientA = await createClient({
    peerOpts,
    server: `ws://127.0.0.1:${port}`
  })

  await clientA.createRoom(keypair)

  const clientB = await createClient({
    peerOpts,
    server: `ws://127.0.0.1:${port}`
  })
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / authenticate.ts View on Github external
import sodium from 'libsodium-wrappers'
import { NetConnection } from 'network'
import { KeyPair, Key } from '../types'
import { Duplex } from 'stream'
import { waitEvent } from '@metastream/signal-server/lib/util'
import * as crypto from './crypto'

const SUCCESS = sodium.from_string('success')

const PACKET_TIMEOUT = 10e3

/** Performs mutual authentication with the remote peer. */
export async function mutualHandshake(socket: Duplex, keyPair: KeyPair, serverPublicKey?: Key) {
  let sharedKey: Key | undefined
  let verifiedPeerKey: Key | undefined

  function createSharedKey(peerPublicKey: Key) {
    sharedKey = crypto.scalarMultiplication(keyPair.privateKey, peerPublicKey)
  }

  function encrypt(data: Uint8Array) {
    if (!sharedKey) return null
    const nonce = crypto.nonce()
    const box = crypto.encrypt(data, nonce, sharedKey)
github TankerHQ / sdk-js / packages / crypto / src / utils.js View on Github external
export function fromString(str: string): Uint8Array {
  if (typeof str !== 'string')
    throw new TypeError('"str" is not a string');

  return sodium.from_string(str);
}
github whs / ipfs-encrypted-share / src / lib / ipfs.js View on Github external
export const upload = (endpoint, files, onProgress = () => {}) => {
	let body = new FormData();

	for (let item of files) {
		if (item.content instanceof Blob) {
			body.append(item.path, item.content, item.path);
		} else {
			body.append(item.path, new Blob([from_string(item.content)]), item.path);
		}
	}

	return axios({
		method: 'POST',
		url: '/api/v0/add',
		baseURL: endpoint,
		params: {
			recursive: 'true',
		},
		data: body,
		responseType: 'text',
		onUploadProgress: (progress) => {
			onProgress((progress.loaded / progress.total) * 100);
		},
	}).then((res) => {