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

@stablelib/nacl

Package Overview
Dependencies
Maintainers
1
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.3 to 1.0.4

29

lib/box.js

@@ -5,14 +5,15 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var x25519_1 = require("@stablelib/x25519");
var xsalsa20_1 = require("@stablelib/xsalsa20");
var secretbox_1 = require("./secretbox");
var wipe_1 = require("@stablelib/wipe");
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");
exports.generateKeyPair = x25519_2.generateKeyPair;
var zeros16 = new Uint8Array(16);
Object.defineProperty(exports, "generateKeyPair", { enumerable: true, get: function () { return x25519_2.generateKeyPair; } });
const zeros16 = new Uint8Array(16);
function precomputeSharedKey(theirPublicKey, mySecretKey) {
// Compute scalar multiplication result.
var key = x25519_1.scalarMult(mySecretKey, theirPublicKey);
const key = (0, x25519_1.scalarMult)(mySecretKey, theirPublicKey);
// Hash key with HSalsa function.
xsalsa20_1.hsalsa(key, zeros16, key);
(0, xsalsa20_1.hsalsa)(key, zeros16, key);
return key;

@@ -22,5 +23,5 @@ }

function box(theirPublicKey, mySecretKey, nonce, data) {
var sharedKey = precomputeSharedKey(theirPublicKey, mySecretKey);
var result = secretbox_1.secretBox(sharedKey, nonce, data);
wipe_1.wipe(sharedKey);
const sharedKey = precomputeSharedKey(theirPublicKey, mySecretKey);
const result = (0, secretbox_1.secretBox)(sharedKey, nonce, data);
(0, wipe_1.wipe)(sharedKey);
return result;

@@ -30,5 +31,5 @@ }

function openBox(theirPublicKey, mySecretKey, nonce, data) {
var sharedKey = precomputeSharedKey(theirPublicKey, mySecretKey);
var result = secretbox_1.openSecretBox(sharedKey, nonce, data);
wipe_1.wipe(sharedKey);
const sharedKey = precomputeSharedKey(theirPublicKey, mySecretKey);
const result = (0, secretbox_1.openSecretBox)(sharedKey, nonce, data);
(0, wipe_1.wipe)(sharedKey);
return result;

@@ -35,0 +36,0 @@ }

"use strict";
// Copyright (C) 2016 Dmitry Chestnykh
// MIT License. See LICENSE file for details.
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
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 });

@@ -11,4 +22,4 @@ /**

*/
__export(require("./box"));
__export(require("./secretbox"));
__exportStar(require("./box"), exports);
__exportStar(require("./secretbox"), exports);
//# sourceMappingURL=nacl.js.map

@@ -5,10 +5,10 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var secretbox_1 = require("./secretbox");
var benchmark_1 = require("@stablelib/benchmark");
var buf8192 = benchmark_1.byteSeq(8192);
var buf1111 = benchmark_1.byteSeq(1111);
var key = benchmark_1.byteSeq(32);
var nonce = benchmark_1.byteSeq(24);
benchmark_1.report("secretBox 8K", benchmark_1.benchmark(function () { return secretbox_1.secretBox(key, nonce, buf8192); }, buf8192.length));
benchmark_1.report("secretBox 1111", benchmark_1.benchmark(function () { return secretbox_1.secretBox(key, nonce, buf1111); }, buf1111.length));
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));
//# sourceMappingURL=secretbox.bench.js.map

@@ -5,6 +5,7 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var xsalsa20_1 = require("@stablelib/xsalsa20");
var poly1305_1 = require("@stablelib/poly1305");
var wipe_1 = require("@stablelib/wipe");
var random_1 = require("@stablelib/random");
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) {

@@ -14,5 +15,5 @@ if (nonce.length !== 24) {

}
var firstBlock = new Uint8Array(64);
const firstBlock = new Uint8Array(64);
// Allocate place for nonce and counter.
var nonceCounter = new Uint8Array(24 + 8);
const nonceCounter = new Uint8Array(24 + 8);
// Set first bytes to nonce. Last 8 bytes will be counter.

@@ -23,8 +24,8 @@ nonceCounter.set(nonce);

// will be used for encryption.
xsalsa20_1.stream(key, nonceCounter, firstBlock, 8);
(0, xsalsa20_1.stream)(key, nonceCounter, firstBlock, 8);
// Allocate result, which will contain 16-byte authenticator
// concatenated with ciphertext.
var result = new Uint8Array(16 + data.length);
const result = new Uint8Array(16 + data.length);
// Encrypt first 32 bytes of data with last 32 bytes of generated stream.
for (var i = 0; i < 32 && i < data.length; i++) {
for (let i = 0; i < 32 && i < data.length; i++) {
result[16 + i] = data[i] ^ firstBlock[32 + i];

@@ -34,17 +35,17 @@ }

if (data.length > 32) {
xsalsa20_1.streamXOR(key, nonceCounter, data.subarray(32), result.subarray(16 + 32), 8);
(0, xsalsa20_1.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.
var auth = poly1305_1.oneTimeAuth(firstBlock.subarray(0, 32), result.subarray(16));
const auth = (0, poly1305_1.oneTimeAuth)(firstBlock.subarray(0, 32), result.subarray(16));
// Copy authenticator to the beginning of result.
for (var i = 0; i < auth.length; i++) {
for (let i = 0; i < auth.length; i++) {
result[i] = auth[i];
}
// Clean auth.
wipe_1.wipe(auth);
(0, wipe_1.wipe)(auth);
// Clean first block.
wipe_1.wipe(firstBlock);
(0, wipe_1.wipe)(firstBlock);
// Clean nonceCounter.
wipe_1.wipe(nonceCounter);
(0, wipe_1.wipe)(nonceCounter);
return result;

@@ -60,5 +61,5 @@ }

}
var firstBlock = new Uint8Array(64);
const firstBlock = new Uint8Array(64);
// Allocate place for nonce and counter.
var nonceCounter = new Uint8Array(24 + 8);
const nonceCounter = new Uint8Array(24 + 8);
// Set first bytes to nonce. Last 8 bytes will be counter.

@@ -69,8 +70,8 @@ nonceCounter.set(nonce);

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

@@ -81,7 +82,7 @@ // was corrupted, maybe maliciously.

// Authenticator verifies, so we can decrypt ciphertext.
var ciphertext = box.subarray(16);
const ciphertext = box.subarray(16);
// Allocate result array.
var result = new Uint8Array(ciphertext.length);
const result = new Uint8Array(ciphertext.length);
// Decrypt first 32 bytes of box with last 32 bytes of generated stream.
for (var i = 0; i < 32 && i < ciphertext.length; i++) {
for (let i = 0; i < 32 && i < ciphertext.length; i++) {
result[i] = ciphertext[i] ^ firstBlock[32 + i];

@@ -91,10 +92,10 @@ }

if (ciphertext.length > 32) {
xsalsa20_1.streamXOR(key, nonceCounter, ciphertext.subarray(32), result.subarray(32), 8);
(0, xsalsa20_1.streamXOR)(key, nonceCounter, ciphertext.subarray(32), result.subarray(32), 8);
}
// Clean auth.
wipe_1.wipe(auth);
(0, wipe_1.wipe)(auth);
// Clean first block.
wipe_1.wipe(firstBlock);
(0, wipe_1.wipe)(firstBlock);
// Clean nonceCounter.
wipe_1.wipe(nonceCounter);
(0, wipe_1.wipe)(nonceCounter);
return result;

@@ -105,5 +106,5 @@ }

function generateKey(prng) {
return random_1.randomBytes(32, prng);
return (0, random_1.randomBytes)(32, prng);
}
exports.generateKey = generateKey;
//# sourceMappingURL=secretbox.js.map

@@ -5,34 +5,34 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var secretbox_1 = require("./secretbox");
var hex_1 = require("@stablelib/hex");
describe("secretBox", function () {
var key = new Uint8Array(32);
for (var i = 0; i < key.length; i++) {
const secretbox_1 = require("./secretbox");
const hex_1 = require("@stablelib/hex");
describe("secretBox", () => {
const key = new Uint8Array(32);
for (let i = 0; i < key.length; i++) {
key[i] = 1;
}
var nonce = new Uint8Array(24);
for (var i = 0; i < nonce.length; i++) {
const nonce = new Uint8Array(24);
for (let i = 0; i < nonce.length; i++) {
nonce[i] = 2;
}
var data = new Uint8Array(64);
for (var i = 0; i < data.length; i++) {
const data = new Uint8Array(64);
for (let i = 0; i < data.length; i++) {
data[i] = 3;
}
var good = "8442bc313f4626f1359e3b50122b6ce6fe66ddfe7d39d14e637eb4fd5b45beadab55198" +
const good = "8442bc313f4626f1359e3b50122b6ce6fe66ddfe7d39d14e637eb4fd5b45beadab55198" +
"df6ab5368439792a23c87db70acb6156dc5ef957ac04f6276cf6093b84be77ff0849cc" +
"33e34b7254d5a8f65ad";
it("should generate correct secretbox", function () {
expect(hex_1.encode(secretbox_1.secretBox(key, nonce, data), true)).toBe(good);
it("should generate correct secretbox", () => {
expect((0, hex_1.encode)((0, secretbox_1.secretBox)(key, nonce, data), true)).toBe(good);
});
it("should open secretbox", function () {
var result = secretbox_1.openSecretBox(key, nonce, hex_1.decode(good));
it("should open secretbox", () => {
const result = (0, secretbox_1.openSecretBox)(key, nonce, (0, hex_1.decode)(good));
expect(result).not.toBeNull();
expect(result).toEqual(data);
});
it("should not open invalid secretbox", function () {
var bad = hex_1.decode(good);
it("should not open invalid secretbox", () => {
const bad = (0, hex_1.decode)(good);
bad[50] = 0;
expect(secretbox_1.openSecretBox(key, nonce, bad)).toBeNull();
expect((0, secretbox_1.openSecretBox)(key, nonce, bad)).toBeNull();
});
});
//# sourceMappingURL=secretbox.test.js.map
{
"name": "@stablelib/nacl",
"version": "1.0.3",
"version": "1.0.4",
"description": "Implementation of secretbox and box from NaCl (Networking and Cryptography Library)",

@@ -23,5 +23,5 @@ "main": "./lib/nacl.js",

"@stablelib/poly1305": "^1.0.1",
"@stablelib/random": "^1.0.1",
"@stablelib/random": "^1.0.2",
"@stablelib/wipe": "^1.0.1",
"@stablelib/x25519": "^1.0.2",
"@stablelib/x25519": "^1.0.3",
"@stablelib/xsalsa20": "^1.0.2"

@@ -33,3 +33,3 @@ },

},
"gitHead": "d8b909d49245c040e5e4a59ff8c9b269fc1781ee"
"gitHead": "a402dc74f45c6a93a777a0e2840ce50ba68c3010"
}
{
"extends": "../../configs/tsconfig.json",
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"removeComments": false,
"preserveConstEnums": true,
"moduleResolution": "node",
"newLine": "LF",
"sourceMap": true,
"declaration": true,
"outDir": "lib",
"lib": [
"es5",
"es2015.promise",
"dom",
"scripthost"
]
},

@@ -23,0 +6,0 @@ "exclude": [

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