Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@stablelib/nacl

Package Overview
Dependencies
Maintainers
0
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stablelib/nacl - npm Package Compare versions

Comparing version 1.0.4 to 2.0.0

typedoc.json

35

lib/box.js

@@ -1,35 +0,28 @@

"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.openBox = exports.box = exports.precomputeSharedKey = exports.generateKeyPair = void 0;
const x25519_1 = require("@stablelib/x25519");
const xsalsa20_1 = require("@stablelib/xsalsa20");
const secretbox_1 = require("./secretbox");
const wipe_1 = require("@stablelib/wipe");
var x25519_2 = require("@stablelib/x25519");
Object.defineProperty(exports, "generateKeyPair", { enumerable: true, get: function () { return x25519_2.generateKeyPair; } });
import { scalarMult } from "@stablelib/x25519";
import { hsalsa } from "@stablelib/xsalsa20";
import { secretBox, openSecretBox } from "./secretbox";
import { wipe } from "@stablelib/wipe";
export { generateKeyPair } from "@stablelib/x25519";
const zeros16 = new Uint8Array(16);
function precomputeSharedKey(theirPublicKey, mySecretKey) {
export function precomputeSharedKey(theirPublicKey, mySecretKey) {
// Compute scalar multiplication result.
const key = (0, x25519_1.scalarMult)(mySecretKey, theirPublicKey);
const key = scalarMult(mySecretKey, theirPublicKey);
// Hash key with HSalsa function.
(0, xsalsa20_1.hsalsa)(key, zeros16, key);
hsalsa(key, zeros16, key);
return key;
}
exports.precomputeSharedKey = precomputeSharedKey;
function box(theirPublicKey, mySecretKey, nonce, data) {
export function box(theirPublicKey, mySecretKey, nonce, data) {
const sharedKey = precomputeSharedKey(theirPublicKey, mySecretKey);
const result = (0, secretbox_1.secretBox)(sharedKey, nonce, data);
(0, wipe_1.wipe)(sharedKey);
const result = secretBox(sharedKey, nonce, data);
wipe(sharedKey);
return result;
}
exports.box = box;
function openBox(theirPublicKey, mySecretKey, nonce, data) {
export function openBox(theirPublicKey, mySecretKey, nonce, data) {
const sharedKey = precomputeSharedKey(theirPublicKey, mySecretKey);
const result = (0, secretbox_1.openSecretBox)(sharedKey, nonce, data);
(0, wipe_1.wipe)(sharedKey);
const result = openSecretBox(sharedKey, nonce, data);
wipe(sharedKey);
return result;
}
exports.openBox = openBox;
//# sourceMappingURL=box.js.map

20

lib/nacl.js

@@ -1,24 +0,8 @@

"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Package nacl implements NaCl (Networking and Cryptography library) cryptography.
*/
__exportStar(require("./box"), exports);
__exportStar(require("./secretbox"), exports);
export * from "./box";
export * from "./secretbox";
//# sourceMappingURL=nacl.js.map

@@ -1,13 +0,11 @@

"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
const secretbox_1 = require("./secretbox");
const benchmark_1 = require("@stablelib/benchmark");
const buf8192 = (0, benchmark_1.byteSeq)(8192);
const buf1111 = (0, benchmark_1.byteSeq)(1111);
const key = (0, benchmark_1.byteSeq)(32);
const nonce = (0, benchmark_1.byteSeq)(24);
(0, benchmark_1.report)("secretBox 8K", (0, benchmark_1.benchmark)(() => (0, secretbox_1.secretBox)(key, nonce, buf8192), buf8192.length));
(0, benchmark_1.report)("secretBox 1111", (0, benchmark_1.benchmark)(() => (0, secretbox_1.secretBox)(key, nonce, buf1111), buf1111.length));
import { secretBox } from "./secretbox";
import { benchmark, report, byteSeq } from "@stablelib/benchmark";
const buf8192 = byteSeq(8192);
const buf1111 = byteSeq(1111);
const key = byteSeq(32);
const nonce = byteSeq(24);
report("secretBox 8K", benchmark(() => secretBox(key, nonce, buf8192), buf8192.length));
report("secretBox 1111", benchmark(() => secretBox(key, nonce, buf1111), buf1111.length));
//# sourceMappingURL=secretbox.bench.js.map

@@ -1,2 +0,2 @@

import { RandomSource } from "@stablelib/random";
import type { RandomSource } from "@stablelib/random";
export declare function secretBox(key: Uint8Array, nonce: Uint8Array, data: Uint8Array): Uint8Array;

@@ -3,0 +3,0 @@ export declare function openSecretBox(key: Uint8Array, nonce: Uint8Array, box: Uint8Array): Uint8Array | null;

@@ -1,11 +0,8 @@

"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateKey = exports.openSecretBox = exports.secretBox = void 0;
const xsalsa20_1 = require("@stablelib/xsalsa20");
const poly1305_1 = require("@stablelib/poly1305");
const wipe_1 = require("@stablelib/wipe");
const random_1 = require("@stablelib/random");
function secretBox(key, nonce, data) {
import { streamXOR, stream } from "@stablelib/xsalsa20";
import { oneTimeAuth, equal as authEqual } from "@stablelib/poly1305";
import { wipe } from "@stablelib/wipe";
import { randomBytes } from "@stablelib/random";
export function secretBox(key, nonce, data) {
if (nonce.length !== 24) {

@@ -22,3 +19,3 @@ throw new Error("secretBox nonce must be 24 bytes");

// will be used for encryption.
(0, xsalsa20_1.stream)(key, nonceCounter, firstBlock, 8);
stream(key, nonceCounter, firstBlock, 8);
// Allocate result, which will contain 16-byte authenticator

@@ -33,7 +30,7 @@ // concatenated with ciphertext.

if (data.length > 32) {
(0, xsalsa20_1.streamXOR)(key, nonceCounter, data.subarray(32), result.subarray(16 + 32), 8);
streamXOR(key, nonceCounter, data.subarray(32), result.subarray(16 + 32), 8);
}
// Calculate Poly1305 authenticator of encrypted data using
// authentication key in the first block of XSalsa20 stream.
const auth = (0, poly1305_1.oneTimeAuth)(firstBlock.subarray(0, 32), result.subarray(16));
const auth = oneTimeAuth(firstBlock.subarray(0, 32), result.subarray(16));
// Copy authenticator to the beginning of result.

@@ -44,11 +41,10 @@ for (let i = 0; i < auth.length; i++) {

// Clean auth.
(0, wipe_1.wipe)(auth);
wipe(auth);
// Clean first block.
(0, wipe_1.wipe)(firstBlock);
wipe(firstBlock);
// Clean nonceCounter.
(0, wipe_1.wipe)(nonceCounter);
wipe(nonceCounter);
return result;
}
exports.secretBox = secretBox;
function openSecretBox(key, nonce, box) {
export function openSecretBox(key, nonce, box) {
if (nonce.length !== 24) {

@@ -68,8 +64,8 @@ throw new Error("secretBox nonce must be 24 bytes");

// will be used for encryption.
(0, xsalsa20_1.stream)(key, nonceCounter, firstBlock, 8);
stream(key, nonceCounter, firstBlock, 8);
// Calculate Poly1305 authenticator of encrypted data using
// authentication key in the first block of XSalsa20 stream.
const auth = (0, poly1305_1.oneTimeAuth)(firstBlock.subarray(0, 32), box.subarray(16));
const auth = oneTimeAuth(firstBlock.subarray(0, 32), box.subarray(16));
// Check authenticator.
if (!(0, poly1305_1.equal)(auth, box.subarray(0, 16))) {
if (!authEqual(auth, box.subarray(0, 16))) {
// Authenticator is incorrect: ciphertext or authenticator

@@ -89,18 +85,16 @@ // was corrupted, maybe maliciously.

if (ciphertext.length > 32) {
(0, xsalsa20_1.streamXOR)(key, nonceCounter, ciphertext.subarray(32), result.subarray(32), 8);
streamXOR(key, nonceCounter, ciphertext.subarray(32), result.subarray(32), 8);
}
// Clean auth.
(0, wipe_1.wipe)(auth);
wipe(auth);
// Clean first block.
(0, wipe_1.wipe)(firstBlock);
wipe(firstBlock);
// Clean nonceCounter.
(0, wipe_1.wipe)(nonceCounter);
wipe(nonceCounter);
return result;
}
exports.openSecretBox = openSecretBox;
/** Generates a 32-byte random secret key. */
function generateKey(prng) {
return (0, random_1.randomBytes)(32, prng);
export function generateKey(prng) {
return randomBytes(32, prng);
}
exports.generateKey = generateKey;
//# sourceMappingURL=secretbox.js.map

@@ -1,7 +0,6 @@

"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
Object.defineProperty(exports, "__esModule", { value: true });
const secretbox_1 = require("./secretbox");
const hex_1 = require("@stablelib/hex");
import { describe, expect, it } from 'vitest';
import { secretBox, openSecretBox } from "./secretbox";
import { encode, decode } from "@stablelib/hex";
describe("secretBox", () => {

@@ -24,6 +23,6 @@ const key = new Uint8Array(32);

it("should generate correct secretbox", () => {
expect((0, hex_1.encode)((0, secretbox_1.secretBox)(key, nonce, data), true)).toBe(good);
expect(encode(secretBox(key, nonce, data), true)).toBe(good);
});
it("should open secretbox", () => {
const result = (0, secretbox_1.openSecretBox)(key, nonce, (0, hex_1.decode)(good));
const result = openSecretBox(key, nonce, decode(good));
expect(result).not.toBeNull();

@@ -33,7 +32,7 @@ expect(result).toEqual(data);

it("should not open invalid secretbox", () => {
const bad = (0, hex_1.decode)(good);
const bad = decode(good);
bad[50] = 0;
expect((0, secretbox_1.openSecretBox)(key, nonce, bad)).toBeNull();
expect(openSecretBox(key, nonce, bad)).toBeNull();
});
});
//# sourceMappingURL=secretbox.test.js.map
{
"name": "@stablelib/nacl",
"version": "1.0.4",
"version": "2.0.0",
"description": "Implementation of secretbox and box from NaCl (Networking and Cryptography Library)",
"main": "./lib/nacl.js",
"type": "module",
"typings": "./lib/nacl.d.ts",

@@ -18,17 +19,17 @@ "author": "Dmitry Chestnykh",

"build": "tsc",
"test": "jasmine JASMINE_CONFIG_PATH=../../configs/jasmine.json",
"test": "vitest run",
"bench": "node ./lib/secretbox.bench.js"
},
"dependencies": {
"@stablelib/poly1305": "^1.0.1",
"@stablelib/random": "^1.0.2",
"@stablelib/wipe": "^1.0.1",
"@stablelib/x25519": "^1.0.3",
"@stablelib/xsalsa20": "^1.0.2"
"@stablelib/poly1305": "^2.0.0",
"@stablelib/random": "^2.0.0",
"@stablelib/wipe": "^2.0.0",
"@stablelib/x25519": "^2.0.0",
"@stablelib/xsalsa20": "^2.0.0"
},
"devDependencies": {
"@stablelib/benchmark": "^1.0.1",
"@stablelib/hex": "^1.0.1"
"@stablelib/benchmark": "^2.0.0",
"@stablelib/hex": "^2.0.0"
},
"gitHead": "a402dc74f45c6a93a777a0e2840ce50ba68c3010"
"gitHead": "ecfe9109b3c05419fd3ffc16da6c8255b08ad64f"
}
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
import { describe, expect, it } from 'vitest';
import { secretBox, openSecretBox } from "./secretbox";

@@ -5,0 +6,0 @@ import { encode, decode } from "@stablelib/hex";

@@ -7,3 +7,4 @@ // Copyright (C) 2016 Dmitry Chestnykh

import { wipe } from "@stablelib/wipe";
import { randomBytes, RandomSource } from "@stablelib/random";
import type { RandomSource } from "@stablelib/random";
import { randomBytes } from "@stablelib/random";

@@ -10,0 +11,0 @@ export function secretBox(key: Uint8Array, nonce: Uint8Array, data: Uint8Array): Uint8Array {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc