Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Added TypeScript type definitions and various updates #8

Merged
merged 2 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflow/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: tests
on: push
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ '8', '10', '12', '13' ]
jef marked this conversation as resolved.
Show resolved Hide resolved
name: Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- name: Install depedencies
run: npm run install
- name: Run tests
run: npm run pretest && npm run test
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/*
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const overheadLength: number;
export function seal(message: Uint8Array, publicKey: Uint8Array): Uint8Array;
export function sealOpen(message: Uint8Array, publicKey: Uint8Array, secretKey: Uint8Array): Uint8Array;
46 changes: 24 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import nacl from 'tweetnacl'
import {blake2bInit, blake2bUpdate, blake2bFinal} from 'blakejs'
'use strict';
const tweetSodium = module.exports;
jef marked this conversation as resolved.
Show resolved Hide resolved
import nacl from 'tweetnacl';
import {blake2bInit, blake2bUpdate, blake2bFinal} from 'blakejs';

// Authenticated sealing only prepends the nonce to the ciphertext. Anonymous
// sealing also prepends a random public key.
export const overheadLength = nacl.box.overheadLength + nacl.box.publicKeyLength
tweetSodium.overheadLength = nacl.box.overheadLength + nacl.box.publicKeyLength;

// Generates a 24 byte nonce that is a blake2b digest of the ephemeral
// public key and the reipient's public key.
Expand All @@ -14,11 +16,11 @@ export const overheadLength = nacl.box.overheadLength + nacl.box.publicKeyLength
// - epk - ephemeral public key Uint8Array
// - publicKey - recipient's public key Uint8Array
function sealNonce(epk, publicKey) {
let hash = blake2bInit(nacl.box.nonceLength, false)
let hash = blake2bInit(nacl.box.nonceLength, false);

blake2bUpdate(hash, epk)
blake2bUpdate(hash, publicKey)
return blake2bFinal(hash)
blake2bUpdate(hash, epk);
blake2bUpdate(hash, publicKey);
return blake2bFinal(hash);
}

// Encrypt a message for a recipient.
Expand All @@ -28,19 +30,19 @@ function sealNonce(epk, publicKey) {
// Parameters:
// - message - message Uint8Array to encrypt.
// - publicKey - recipient's public key Uint8Array.
export function seal(message, publicKey) {
const ekp = nacl.box.keyPair()
tweetSodium.seal = (message, publicKey) => {
const ekp = nacl.box.keyPair();

let out = new Uint8Array(message.length + overheadLength)
out.set(ekp.publicKey, 0)
let out = new Uint8Array(message.length + tweetSodium.overheadLength);
out.set(ekp.publicKey, 0);

const nonce = sealNonce(ekp.publicKey, publicKey)
const nonce = sealNonce(ekp.publicKey, publicKey);

const ct = nacl.box(message, nonce, publicKey, ekp.secretKey)
out.set(ct, nacl.box.publicKeyLength)
const ct = nacl.box(message, nonce, publicKey, ekp.secretKey);
out.set(ct, nacl.box.publicKeyLength);

return out
}
return out;
};

// Decrypt the ciphertext message using the secret key.
//
Expand All @@ -49,10 +51,10 @@ export function seal(message, publicKey) {
// Parameters:
// - ciphertext - encrypted message Uint8Array.
// - secretKey - secret key Uint8Array.
export function sealOpen(ciphertext, publicKey, secretKey) {
const epk = ciphertext.slice(0, nacl.box.publicKeyLength)
const nonce = sealNonce(epk, publicKey)
ciphertext = ciphertext.slice(nacl.box.publicKeyLength)
tweetSodium.sealOpen = (ciphertext, publicKey, secretKey) => {
const epk = ciphertext.slice(0, nacl.box.publicKeyLength);
const nonce = sealNonce(epk, publicKey);
ciphertext = ciphertext.slice(nacl.box.publicKeyLength);

return nacl.box.open(ciphertext, nonce, epk, secretKey)
}
return nacl.box.open(ciphertext, nonce, epk, secretKey);
};
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType} from 'tsd';
import tweetSodium = require('./index');

expectType<Uint8Array>(tweetSodium.seal(new Uint8Array(), new Uint8Array()));
expectType<Uint8Array>(tweetSodium.sealOpen(new Uint8Array(), new Uint8Array(), new Uint8Array()));
expectType<number>(tweetSodium.overheadLength);
Loading