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

@shardus/crypto-utils

Package Overview
Dependencies
Maintainers
6
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@shardus/crypto-utils - npm Package Compare versions

Comparing version 4.1.2 to 4.1.3

7

build/src/index.d.ts

@@ -75,3 +75,3 @@ /// <reference types="node" />

export declare function decrypt(payload: string, curveSk: curveSecretKey | Buffer, curvePk: curvePublicKey | Buffer): {
isValid: any;
isValid: boolean;
message: string;

@@ -121,4 +121,5 @@ };

* @param pk
* @returns the new signed object with the `sign` field. The original object is mutated as well.
*/
export declare function signObj(obj: SignedObject, sk: secretKey | Buffer, pk: publicKey | Buffer): void;
export declare function signObj(obj: object, sk: secretKey | Buffer, pk: publicKey | Buffer): SignedObject;
/**

@@ -146,3 +147,3 @@ * Returns true if the hash of the input was signed by the owner of the pk

*/
export declare function _ensureBuffer(input: string | Buffer, name?: string): Buffer;
export declare function _ensureBuffer(input: string | Buffer, name?: string): Buffer | string;
/**

@@ -149,0 +150,0 @@ *

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.bufferToHex = exports._getAuthKey = exports.generateSharedKey = exports._ensureBuffer = exports.init = exports.verifyObj = exports.verify = exports.signObj = exports.sign = exports.setCustomStringifier = exports.authenticateObj = exports.authenticate = exports.tagObj = exports.tag = exports.decrypt = exports.encrypt = exports.convertPkToCurve = exports.convertSkToCurve = exports.generateKeypair = exports.hashObj = exports.hash = exports.randomBytes = exports.stringifierName = exports.stringify = void 0;
const sodium = require('sodium-native');
const xor = require('buffer-xor');
const fastStableStringify = require('fast-stable-stringify');
exports.stringify = fastStableStringify;
const sodium_native_1 = __importDefault(require("sodium-native"));
const buffer_xor_1 = __importDefault(require("buffer-xor"));
const fast_stable_stringify_1 = __importDefault(require("fast-stable-stringify"));
exports.stringify = fast_stable_stringify_1.default;
exports.stringifierName = 'fast-stable-stringify';

@@ -22,3 +25,3 @@ /**

const buf = Buffer.allocUnsafe(bytes);
sodium.randombytes_buf(buf);
sodium_native_1.default.randombytes_buf(buf);
return buf.toString('hex');

@@ -47,3 +50,3 @@ }

const digest = Buffer.allocUnsafe(32);
sodium.crypto_generichash(digest, buf, HASH_KEY);
sodium_native_1.default.crypto_generichash(digest, buf, HASH_KEY);
let output;

@@ -111,5 +114,5 @@ switch (fmt) {

function generateKeypair() {
const publicKey = Buffer.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES);
const secretKey = Buffer.allocUnsafe(sodium.crypto_sign_SECRETKEYBYTES);
sodium.crypto_sign_keypair(publicKey, secretKey);
const publicKey = Buffer.allocUnsafe(sodium_native_1.default.crypto_sign_PUBLICKEYBYTES);
const secretKey = Buffer.allocUnsafe(sodium_native_1.default.crypto_sign_SECRETKEYBYTES);
sodium_native_1.default.crypto_sign_keypair(publicKey, secretKey);
return {

@@ -127,5 +130,5 @@ publicKey: publicKey.toString('hex'),

const skBuf = _ensureBuffer(sk);
const curveSkBuf = Buffer.allocUnsafe(sodium.crypto_box_SECRETKEYBYTES);
const curveSkBuf = Buffer.allocUnsafe(sodium_native_1.default.crypto_box_SECRETKEYBYTES);
try {
sodium.crypto_sign_ed25519_sk_to_curve25519(curveSkBuf, skBuf);
sodium_native_1.default.crypto_sign_ed25519_sk_to_curve25519(curveSkBuf, skBuf);
}

@@ -144,5 +147,5 @@ catch (e) {

const pkBuf = _ensureBuffer(pk);
const curvePkBuf = Buffer.allocUnsafe(sodium.crypto_box_PUBLICKEYBYTES);
const curvePkBuf = Buffer.allocUnsafe(sodium_native_1.default.crypto_box_PUBLICKEYBYTES);
try {
sodium.crypto_sign_ed25519_pk_to_curve25519(curvePkBuf, pkBuf);
sodium_native_1.default.crypto_sign_ed25519_pk_to_curve25519(curvePkBuf, pkBuf);
}

@@ -165,6 +168,6 @@ catch (e) {

const curvePkBuf = _ensureBuffer(curvePk, 'Public key');
const ciphertext = Buffer.allocUnsafe(messageBuf.length + sodium.crypto_box_MACBYTES);
const nonce = Buffer.allocUnsafe(sodium.crypto_box_NONCEBYTES);
sodium.randombytes_buf(nonce);
sodium.crypto_box_easy(ciphertext, messageBuf, nonce, curvePkBuf, curveSkBuf);
const ciphertext = Buffer.allocUnsafe(messageBuf.length + sodium_native_1.default.crypto_box_MACBYTES);
const nonce = Buffer.allocUnsafe(sodium_native_1.default.crypto_box_NONCEBYTES);
sodium_native_1.default.randombytes_buf(nonce);
sodium_native_1.default.crypto_box_easy(ciphertext, messageBuf, nonce, curvePkBuf, curveSkBuf);
const payload = [ciphertext.toString('hex'), nonce.toString('hex')];

@@ -186,4 +189,4 @@ return JSON.stringify(payload);

const publicKey = _ensureBuffer(curvePk, 'Public key');
const message = Buffer.allocUnsafe(ciphertext.length - sodium.crypto_box_MACBYTES);
const isValid = sodium.crypto_box_open_easy(message, ciphertext, nonce, publicKey, secretKey);
const message = Buffer.allocUnsafe(ciphertext.length - sodium_native_1.default.crypto_box_MACBYTES);
const isValid = sodium_native_1.default.crypto_box_open_easy(message, ciphertext, nonce, publicKey, secretKey);
return { isValid, message: message.toString('utf8') };

@@ -199,8 +202,8 @@ }

const messageBuf = Buffer.from(message, 'utf8');
const nonceBuf = Buffer.allocUnsafe(sodium.crypto_auth_BYTES);
sodium.randombytes_buf(nonceBuf);
const nonceBuf = Buffer.allocUnsafe(sodium_native_1.default.crypto_auth_BYTES);
sodium_native_1.default.randombytes_buf(nonceBuf);
const nonce = nonceBuf.toString('hex');
const keyBuf = _getAuthKey(sharedKey, nonce);
const tagBuf = Buffer.allocUnsafe(sodium.crypto_auth_BYTES);
sodium.crypto_auth(tagBuf, messageBuf, keyBuf);
const tagBuf = Buffer.allocUnsafe(sodium_native_1.default.crypto_auth_BYTES);
sodium_native_1.default.crypto_auth(tagBuf, messageBuf, keyBuf);
const tag = tagBuf.toString('hex');

@@ -237,8 +240,8 @@ return tag + nonce;

function authenticate(message, tag, sharedKey) {
const nonce = tag.substring(sodium.crypto_auth_BYTES * 2);
tag = tag.substring(0, sodium.crypto_auth_BYTES * 2);
const nonce = tag.substring(sodium_native_1.default.crypto_auth_BYTES * 2);
tag = tag.substring(0, sodium_native_1.default.crypto_auth_BYTES * 2);
const tagBuf = _ensureBuffer(tag, 'Tag');
const keyBuf = _getAuthKey(sharedKey, nonce);
const messageBuf = Buffer.from(message, 'utf8');
return sodium.crypto_auth_verify(tagBuf, messageBuf, keyBuf);
return sodium_native_1.default.crypto_auth_verify(tagBuf, messageBuf, keyBuf);
}

@@ -315,5 +318,5 @@ exports.authenticate = authenticate;

}
const sig = Buffer.allocUnsafe(inputBuf.length + sodium.crypto_sign_BYTES);
const sig = Buffer.allocUnsafe(inputBuf.length + sodium_native_1.default.crypto_sign_BYTES);
try {
sodium.crypto_sign(sig, inputBuf, skBuf);
sodium_native_1.default.crypto_sign(sig, inputBuf, skBuf);
}

@@ -332,2 +335,3 @@ catch (e) {

* @param pk
* @returns the new signed object with the `sign` field. The original object is mutated as well.
*/

@@ -339,3 +343,3 @@ function signObj(obj, sk, pk) {

// If it's an array, we don't want to try to sign it
if (obj.length !== undefined) {
if (Array.isArray(obj)) {
throw new TypeError('Input cannot be an array.');

@@ -348,2 +352,3 @@ }

obj.sign = { owner: signPk, sig };
return obj;
}

@@ -364,4 +369,4 @@ exports.signObj = signObj;

try {
const opened = Buffer.allocUnsafe(sigBuf.length - sodium.crypto_sign_BYTES);
sodium.crypto_sign_open(opened, sigBuf, pkBuf);
const opened = Buffer.allocUnsafe(sigBuf.length - sodium_native_1.default.crypto_sign_BYTES);
sodium_native_1.default.crypto_sign_open(opened, sigBuf, pkBuf);
const verified = opened.toString('hex');

@@ -447,4 +452,4 @@ return verified === msg;

const curvePkBuf = _ensureBuffer(curvePk);
const keyBuf = Buffer.allocUnsafe(sodium.crypto_scalarmult_BYTES);
sodium.crypto_scalarmult(keyBuf, curveSkBuf, curvePkBuf);
const keyBuf = Buffer.allocUnsafe(sodium_native_1.default.crypto_scalarmult_BYTES);
sodium_native_1.default.crypto_scalarmult(keyBuf, curveSkBuf, curvePkBuf);
return keyBuf;

@@ -461,3 +466,3 @@ }

const nonceBuf = _ensureBuffer(nonce);
const resultBuf = xor(sharedKeyBuf, nonceBuf);
const resultBuf = (0, buffer_xor_1.default)(sharedKeyBuf, nonceBuf);
return resultBuf;

@@ -464,0 +469,0 @@ }

{
"name": "@shardus/crypto-utils",
"version": "4.1.2",
"version": "4.1.3",
"description": "Provides simple crypto functions, as used by the ULC Project.",

@@ -15,3 +15,4 @@ "main": "./build/src/index.js",

"check": "gts check",
"lint": "gts lint",
"lint": "eslint './src/**/*.ts'",
"lint-windows": "eslint ./src/**/*.ts",
"clean": "gts clean",

@@ -41,6 +42,6 @@ "compile": "tsc -p .",

"dependencies": {
"@shardus/types": "1.2.8",
"buffer-xor": "2.0.2",
"fast-stable-stringify": "1.0.0",
"sodium-native": "3.3.0",
"@shardus/types": "1.2.0"
"sodium-native": "3.3.0"
},

@@ -50,6 +51,14 @@ "devDependencies": {

"@types/node": "18.16.1",
"gts": "3.1.1",
"gts": "^3.1.1",
"@typescript-eslint/eslint-plugin": "^5.48.0",
"@typescript-eslint/parser": "^5.48.0",
"eslint-plugin-no-unsanitized": "^4.0.2",
"eslint-plugin-security": "^1.7.1",
"eslint-plugin-xss": "^0.1.12",
"np": "8.0.4",
"typescript": "4.5.5"
},
"overrides": {
"semver": "5.7.2 || 6.3.1 || 7.5.4"
},
"engines": {

@@ -56,0 +65,0 @@ "node": "18.16.1"

@@ -0,0 +0,0 @@ # shardus-crypto-utils

@@ -8,5 +8,5 @@ export type hexstring = string;

const sodium = require('sodium-native');
const xor = require('buffer-xor');
const fastStableStringify = require('fast-stable-stringify');
import sodium from 'sodium-native';
import xor from 'buffer-xor';
import fastStableStringify from 'fast-stable-stringify';

@@ -110,3 +110,3 @@ export let stringify = fastStableStringify as (input: unknown) => string;

}
function performHash(obj) {
function performHash(obj): string {
const input: string = stringify(obj);

@@ -164,3 +164,3 @@ const hashed = hash(input);

try {
sodium.crypto_sign_ed25519_sk_to_curve25519(curveSkBuf, skBuf);
sodium.crypto_sign_ed25519_sk_to_curve25519(curveSkBuf, skBuf as Buffer);
} catch (e) {

@@ -180,3 +180,3 @@ throw new Error('Could not convert given secret key to curve secret key.');

try {
sodium.crypto_sign_ed25519_pk_to_curve25519(curvePkBuf, pkBuf);
sodium.crypto_sign_ed25519_pk_to_curve25519(curvePkBuf, pkBuf as Buffer);
} catch (e) {

@@ -198,3 +198,3 @@ throw new Error('Could not convert given public key to curve public key.');

curvePk: curvePublicKey | Buffer
) {
): string {
const messageBuf = Buffer.from(message, 'utf8');

@@ -208,3 +208,3 @@ const curveSkBuf = _ensureBuffer(curveSk, 'Secret key');

sodium.randombytes_buf(nonce);
sodium.crypto_box_easy(ciphertext, messageBuf, nonce, curvePkBuf, curveSkBuf);
sodium.crypto_box_easy(ciphertext, messageBuf, nonce, curvePkBuf as Buffer, curveSkBuf as Buffer);
const payload = [ciphertext.toString('hex'), nonce.toString('hex')];

@@ -224,3 +224,3 @@ return JSON.stringify(payload);

curvePk: curvePublicKey | Buffer
) {
): { isValid: boolean; message: string } {
payload = JSON.parse(payload);

@@ -236,6 +236,6 @@ const ciphertext = _ensureBuffer(payload[0], 'Tag ciphertext');

message,
ciphertext,
nonce,
publicKey,
secretKey
ciphertext as Buffer,
nonce as Buffer,
publicKey as Buffer,
secretKey as Buffer
);

@@ -250,3 +250,3 @@ return { isValid, message: message.toString('utf8') };

*/
export function tag(message: string, sharedKey: sharedKey | Buffer) {
export function tag(message: string, sharedKey: sharedKey | Buffer): string {
const messageBuf = Buffer.from(message, 'utf8');

@@ -271,3 +271,3 @@

*/
export function tagObj(obj: TaggedObject, sharedKey: sharedKey | Buffer) {
export function tagObj(obj: TaggedObject, sharedKey: sharedKey | Buffer): void {
if (typeof obj !== 'object') {

@@ -305,3 +305,3 @@ throw new TypeError('Input must be an object.');

const messageBuf = Buffer.from(message, 'utf8');
return sodium.crypto_auth_verify(tagBuf, messageBuf, keyBuf);
return sodium.crypto_auth_verify(tagBuf as Buffer, messageBuf, keyBuf);
}

@@ -317,3 +317,3 @@

sharedKey: sharedKey | Buffer
) {
): boolean {
if (typeof obj !== 'object') {

@@ -340,3 +340,3 @@ throw new TypeError('Input must be an object.');

name: string
) {
): void {
stringify = method;

@@ -351,3 +351,3 @@ stringifierName = name;

*/
export function sign(input: hexstring | Buffer, sk: secretKey | Buffer) {
export function sign(input: hexstring | Buffer, sk: secretKey | Buffer): string {
let inputBuf: Buffer;

@@ -396,8 +396,9 @@ let skBuf: Buffer;

* @param pk
* @returns the new signed object with the `sign` field. The original object is mutated as well.
*/
export function signObj(
obj: SignedObject,
obj: object,
sk: secretKey | Buffer,
pk: publicKey | Buffer
) {
): SignedObject {
if (typeof obj !== 'object') {

@@ -407,3 +408,3 @@ throw new TypeError('Input must be an object.');

// If it's an array, we don't want to try to sign it
if (obj.length !== undefined) {
if (Array.isArray(obj)) {
throw new TypeError('Input cannot be an array.');

@@ -415,3 +416,4 @@ }

const signPk = Buffer.isBuffer(pk) ? bufferToHex(pk) : pk;
obj.sign = { owner: signPk, sig };
(obj as SignedObject).sign = { owner: signPk, sig };
return obj as SignedObject;
}

@@ -429,3 +431,3 @@

pk: publicKey | Buffer
) {
): boolean {
if (typeof msg !== 'string') {

@@ -438,3 +440,3 @@ throw new TypeError('Message to compare must be a string.');

const opened = Buffer.allocUnsafe(sigBuf.length - sodium.crypto_sign_BYTES);
sodium.crypto_sign_open(opened, sigBuf, pkBuf);
sodium.crypto_sign_open(opened, sigBuf as Buffer, pkBuf as Buffer);
const verified = opened.toString('hex');

@@ -453,3 +455,3 @@ return verified === msg;

*/
export function verifyObj(obj: SignedObject) {
export function verifyObj(obj: SignedObject): boolean {
if (typeof obj !== 'object') {

@@ -481,3 +483,3 @@ throw new TypeError('Input must be an object.');

*/
export function init(key: hexstring) {
export function init(key: hexstring): void {
if (!key) {

@@ -501,3 +503,3 @@ throw new Error('Hash key must be passed to module constructor.');

*/
export function _ensureBuffer(input: string | Buffer, name = 'Input') {
export function _ensureBuffer(input: string | Buffer, name = 'Input'): Buffer | string {
if (typeof input !== 'string') {

@@ -526,3 +528,3 @@ if (Buffer.isBuffer(input)) {

curvePk: curvePublicKey | Buffer
) {
): Buffer {
const curveSkBuf = _ensureBuffer(curveSk);

@@ -532,3 +534,3 @@ const curvePkBuf = _ensureBuffer(curvePk);

const keyBuf = Buffer.allocUnsafe(sodium.crypto_scalarmult_BYTES);
sodium.crypto_scalarmult(keyBuf, curveSkBuf, curvePkBuf);
sodium.crypto_scalarmult(keyBuf, curveSkBuf as Buffer, curvePkBuf as Buffer);
return keyBuf;

@@ -548,7 +550,7 @@ }

const nonceBuf = _ensureBuffer(nonce);
const resultBuf = xor(sharedKeyBuf, nonceBuf);
const resultBuf = xor(sharedKeyBuf as Buffer, nonceBuf as Buffer);
return resultBuf;
}
export function bufferToHex(buffer: Buffer) {
export function bufferToHex(buffer: Buffer): string {
return [...new Uint8Array(buffer)]

@@ -555,0 +557,0 @@ .map((byte) => byte.toString(16).padStart(2, '0'))

@@ -0,0 +0,0 @@ declare module 'sodium-native' {

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