All files / src/nacl fast.ts

100% Statements 30/30
100% Branches 2/2
100% Functions 7/7
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                              8x       7x           4x     4x               4x     7x           7x       6x                 1x     4x     7x       12x 12x   12x     7x         12x           7x 4x 4x   4x     7x 12x 12x   12x   12x           7x 5x 5x   5x   5x    
/*
 * Copyright © 2019 Lisk Foundation
 *
 * See the LICENSE file at the top-level directory of this distribution
 * for licensing information.
 *
 * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
 * no part of this software, including this file, may be copied, modified,
 * propagated, or distributed except according to the terms contained in the
 * LICENSE file.
 *
 * Removal or modification of this copyright notice is prohibited.
 *
 */
// tslint:disable-next-line no-implicit-dependencies
import * as sodium from 'sodium-native';
 
import { NaclInterface } from './nacl_types';
 
export const box: NaclInterface['box'] = (
	messageInBytes,
	nonceInBytes,
	convertedPublicKey,
	convertedPrivateKey,
) => {
	const cipherBytes = Buffer.alloc(
		messageInBytes.length + sodium.crypto_box_MACBYTES,
	);
	sodium.crypto_box_easy(
		cipherBytes,
		messageInBytes,
		nonceInBytes,
		convertedPublicKey,
		convertedPrivateKey,
	);
 
	return cipherBytes;
};
 
export const openBox: NaclInterface['openBox'] = (
	cipherBytes,
	nonceBytes,
	convertedPublicKey,
	convertedPrivateKey,
) => {
	const plainText = Buffer.alloc(
		cipherBytes.length - sodium.crypto_box_MACBYTES,
	);
	// Returns false if decryption fails
	if (
		!sodium.crypto_box_open_easy(
			plainText,
			cipherBytes,
			nonceBytes,
			convertedPublicKey,
			convertedPrivateKey,
		)
	) {
		throw new Error('Failed to decrypt message');
	}
 
	return plainText;
};
 
export const signDetached: NaclInterface['signDetached'] = (
	messageBytes,
	privateKeyBytes,
) => {
	const signatureBytes = Buffer.alloc(sodium.crypto_sign_BYTES);
	sodium.crypto_sign_detached(signatureBytes, messageBytes, privateKeyBytes);
 
	return signatureBytes;
};
 
export const verifyDetached: NaclInterface['verifyDetached'] = (
	messageBytes,
	signatureBytes,
	publicKeyBytes,
) =>
	sodium.crypto_sign_verify_detached(
		signatureBytes,
		messageBytes,
		publicKeyBytes,
	);
 
export const getRandomBytes: NaclInterface['getRandomBytes'] = length => {
	const nonce = Buffer.alloc(length);
	sodium.randombytes_buf(nonce);
 
	return nonce;
};
 
export const getKeyPair: NaclInterface['getKeyPair'] = hashedSeed => {
	const publicKeyBytes = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES);
	const privateKeyBytes = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES);
 
	sodium.crypto_sign_seed_keypair(publicKeyBytes, privateKeyBytes, hashedSeed);
 
	return {
		publicKeyBytes,
		privateKeyBytes,
	};
};
 
export const getPublicKey: NaclInterface['getPublicKey'] = privateKey => {
	const publicKeyBytes = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES);
	const privateKeyBytes = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES);
 
	sodium.crypto_sign_seed_keypair(publicKeyBytes, privateKeyBytes, privateKey);
 
	return publicKeyBytes;
};