You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

iso-crypto

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

iso-crypto - npm Package Compare versions

Comparing version
1.1.3
to
1.2.0
+2
dist/types.d.ts
export { Algorithms, Ciphers, Curves, Encodings, Modes, Sizes, } from './iso/lib/types.js';
//# sourceMappingURL=types.d.ts.map
export { Algorithms, Ciphers, Curves, Encodings, Modes, Sizes } from './iso/lib/types.js';
//# sourceMappingURL=types.js.map
+1
-0

@@ -6,2 +6,3 @@ export * from './ecc.js';

export * from './random.js';
export * from './types.js';
//# sourceMappingURL=index.d.ts.map

@@ -6,2 +6,3 @@ export * from './ecc.js';

export * from './random.js';
export * from './types.js';
//# sourceMappingURL=index.js.map
+16
-14

@@ -6,4 +6,6 @@ import { decode, encode } from '#encode';

import { eccMeta } from '../lib/size-meta.js';
import { defaultCurve, defaultEncryption } from '../lib/types.js';
import { Algorithms, defaultCurve, defaultEncryption, Encodings } from '../lib/types.js';
import { decompressEccPublicKey } from './compression.js';
const BITS_PER_BYTE = 8;
const HEX_SIZE = 16;
const { crypto } = globalThis;

@@ -21,13 +23,13 @@ const curveToKeyParams = (curve)=>({

text: key.d,
encoding: 'base64url'
encoding: Encodings.BASE64URL
}), eccMeta(curve).bytes);
};
const getPublicKey = (privateKey, curve)=>{
const hex = encode(decode(privateKey), 'hex');
const hex = encode(decode(privateKey), Encodings.HEX);
return derivePublicKey(BigInt(`0x${hex}`), curves[curve]);
};
const bigIntToBase64Url = (x, bytes)=>encode(padBytes(decode({
text: x.toString(16),
encoding: 'hex'
}), bytes), 'base64url');
text: x.toString(HEX_SIZE),
encoding: Encodings.HEX
}), bytes), Encodings.BASE64URL);
const derivePublicKeyBase64 = (privateKey, curve)=>{

@@ -47,4 +49,4 @@ const { x, y } = getPublicKey(privateKey, curve);

...padBytes(decode({
text: x.toString(16),
encoding: 'hex'
text: x.toString(HEX_SIZE),
encoding: Encodings.HEX
}), bytes)

@@ -60,3 +62,3 @@ ]);

kty: 'EC',
d: encode(bufferPrivateKey, 'base64url'),
d: encode(bufferPrivateKey, Encodings.BASE64URL),
...derivePublicKeyBase64(bufferPrivateKey, curve)

@@ -75,3 +77,3 @@ }, curveParams, true, [

hash: 'SHA-256',
length: bytes * 8
length: bytes * BITS_PER_BYTE
}, true, [

@@ -98,3 +100,3 @@ 'sign'

encryption,
hash: 'raw'
hash: Algorithms.RAW
}),

@@ -105,7 +107,7 @@ crypto.subtle.exportKey('jwk', secretKey.privateEc)

text: jwk.y,
encoding: 'base64url'
encoding: Encodings.BASE64URL
}).reverse()[0] & 1;
const publicX = decode({
text: jwk.x,
encoding: 'base64url'
encoding: Encodings.BASE64URL
});

@@ -133,5 +135,5 @@ const { bytes } = eccMeta(curve);

encryption,
hash: 'raw'
hash: Algorithms.RAW
});
};
//# sourceMappingURL=browser.js.map

@@ -13,3 +13,3 @@ import { type InputText } from '../lib/types.js';

*/
export declare const compressEccPublicKey: (publicKey: InputText, curve?: import("../lib/types.js").Curve) => Uint8Array;
export declare const compressEccPublicKey: (publicKey: InputText, curve?: "p256" | "p384" | "p521") => Uint8Array;
/**

@@ -24,3 +24,3 @@ * Decompress an ECC Public Key.

*/
export declare const decompressEccPublicKey: (publicKey: InputText, curve?: import("../lib/types.js").Curve) => Uint8Array;
export declare const decompressEccPublicKey: (publicKey: InputText, curve?: "p256" | "p384" | "p521") => Uint8Array;
//# sourceMappingURL=compression.d.ts.map

@@ -5,3 +5,5 @@ import { decode, encode } from '#encode';

import { eccMeta } from '../lib/size-meta.js';
import { defaultCurve } from '../lib/types.js';
import { defaultCurve, Encodings } from '../lib/types.js';
const HEX_SIZE = 16;
const DECOMPRESSED_KEY_PREFIX = 4;
export const compressEccPublicKey = (publicKey, curve = defaultCurve)=>{

@@ -28,9 +30,9 @@ const decoded = decode(publicKey);

const odd = !!(decoded[0] & 1);
const y = deriveYCoordinate(BigInt(`0x${encode(x, 'hex')}`), odd, curves[curve]);
const y = deriveYCoordinate(BigInt(`0x${encode(x, Encodings.HEX)}`), odd, curves[curve]);
return new Uint8Array([
4,
DECOMPRESSED_KEY_PREFIX,
...x,
...padBytes(decode({
text: y.toString(16),
encoding: 'hex'
text: y.toString(HEX_SIZE),
encoding: Encodings.HEX
}), bytes)

@@ -37,0 +39,0 @@ ]);

@@ -6,4 +6,4 @@ import { createECDH } from 'node:crypto';

import { eccMeta } from '../lib/size-meta.js';
import { defaultCurve, defaultEncryption } from '../lib/types.js';
const getECDH = (curve)=>createECDH(curve === 'p256' ? 'prime256v1' : `sec${curve}r1`);
import { Algorithms, Curves, defaultCurve, defaultEncryption } from '../lib/types.js';
const getECDH = (curve)=>createECDH(curve === Curves.P256 ? 'prime256v1' : `sec${curve}r1`);
export const generateEccPrivateKey = async (curve = defaultCurve)=>{

@@ -28,3 +28,3 @@ const ecdh = getECDH(curve);

encryption,
hash: 'raw'
hash: Algorithms.RAW
});

@@ -45,5 +45,5 @@ return {

encryption,
hash: 'raw'
hash: Algorithms.RAW
});
};
//# sourceMappingURL=node.js.map
import { inputToEncoding } from './lib/input-to-encoding.js';
import { defaultEncoding } from './lib/types.js';
import { defaultEncoding, Encodings } from './lib/types.js';
const BITS_PER_HEX = 4;
const HEX_SIZE = 16;
const encoder = new TextEncoder();

@@ -7,6 +9,10 @@ const decoder = new TextDecoder();

const base64url = (text)=>text.replaceAll('=', '').replaceAll('+', '-').replaceAll('/', '_');
const EXTRA_EQUALS = '====';
const base64standard = (text)=>{
const charReplaced = text.replaceAll('-', '+').replaceAll('_', '/');
const appendEquals = charReplaced + '='.repeat((4 - text.length % 4) % 4);
return appendEquals.replace(/(?:={4})+$/u, '');
let appendEquals = charReplaced + '='.repeat((BITS_PER_HEX - text.length % BITS_PER_HEX) % BITS_PER_HEX);
while(appendEquals.endsWith(EXTRA_EQUALS)){
appendEquals = appendEquals.slice(0, -EXTRA_EQUALS.length);
}
return appendEquals;
};

@@ -17,16 +23,16 @@ const toBase64 = (buf)=>base64url(btoa(String.fromCodePoint(...new Uint8Array(buf))));

...buf
].map((x)=>x.toString(16).padStart(2, '0')).join('');
].map((x)=>x.toString(HEX_SIZE).padStart(2, '0')).join('');
const fromHex = (str)=>{
const length = str.length % 2 ? str.length + 1 : str.length;
return new Uint8Array((str.padStart(length, '0').match(/.{2}/gu) ?? []).map((byte)=>Number.parseInt(byte, 16)));
return new Uint8Array((str.padStart(length, '0').match(/.{2}/gu) ?? []).map((byte)=>Number.parseInt(byte, HEX_SIZE)));
};
export const decode = (input)=>{
const { encoding, text } = inputToEncoding(input);
if (encoding === 'raw') {
if (encoding === Encodings.RAW) {
return text;
}
if (encoding === 'utf8') {
if (encoding === Encodings.UTF8) {
return encoder.encode(text);
}
if (encoding === 'hex') {
if (encoding === Encodings.HEX) {
return fromHex(text);

@@ -38,10 +44,10 @@ }

const buffer = decode(input);
if (encoding === 'utf8') {
if (encoding === Encodings.UTF8) {
return decoder.decode(buffer);
}
if (encoding === 'hex') {
if (encoding === Encodings.HEX) {
return toHex(buffer);
}
const url = toBase64(buffer);
if (encoding === 'base64') {
if (encoding === Encodings.BASE64) {
return base64standard(url);

@@ -48,0 +54,0 @@ }

import { decode } from '#encode';
import { defaultHash } from '../lib/types.js';
import { Algorithms, defaultHash } from '../lib/types.js';
const { crypto } = globalThis;
const hashAlgorithm = ({ algorithm, size })=>{
if (algorithm === 'SHA1') {
if (algorithm === Algorithms.SHA1) {
return 'SHA-1';

@@ -12,3 +12,3 @@ }

const decoded = decode(input);
if (algorithm === 'raw') {
if (algorithm === Algorithms.RAW) {
return decoded;

@@ -15,0 +15,0 @@ }

import { createHash } from 'node:crypto';
import { decode } from '#encode';
import { defaultHash } from '../lib/types.js';
import { Algorithms, defaultHash } from '../lib/types.js';
const hashAlgorithm = ({ algorithm, size })=>{
if (algorithm === 'SHA1') {
if (algorithm === Algorithms.SHA1) {
return 'SHA1';

@@ -12,3 +12,3 @@ }

const decoded = decode(input);
if (algorithm === 'raw') {
if (algorithm === Algorithms.RAW) {
return decoded;

@@ -15,0 +15,0 @@ }

@@ -0,7 +1,9 @@

const BITS_PER_BYTE = 8;
const BYTE_PAIR = BITS_PER_BYTE * 2;
export const encryptionMeta = (encryption)=>({
secret: encryption.size / 8,
iv: 16
secret: encryption.size / BITS_PER_BYTE,
iv: BYTE_PAIR
});
export const eccMeta = (curve)=>{
const bytePairs = Number.parseInt(curve.slice(1), 10) / 16;
const bytePairs = Number.parseInt(curve.slice(1), 10) / BYTE_PAIR;
return {

@@ -8,0 +10,0 @@ bytes: Math.ceil(bytePairs) * 2

@@ -0,23 +1,61 @@

/**
* Allows string literals in place of enum.
*
* @template E - either entire enum or single value
*/
type EnumToString<E extends string> = `${E}`;
export declare const enum Ciphers {
AES = "AES"
}
export declare const enum Modes {
CBC = "CBC",
CTR = "CTR"
}
export declare const enum Sizes {
KEY_128 = 128,
KEY_160 = 160,
KEY_192 = 192,
KEY_256 = 256,
KEY_384 = 384,
KEY_512 = 512
}
export type Encryption = {
cipher: 'AES';
size: 128 | 192 | 256;
mode: 'CBC';
cipher: EnumToString<Ciphers.AES>;
size: Sizes.KEY_128 | Sizes.KEY_192 | Sizes.KEY_256;
mode: EnumToString<Modes.CBC>;
} | {
cipher: 'AES';
size: 128 | 192 | 256;
mode: 'CTR';
cipher: EnumToString<Ciphers.AES>;
size: Sizes.KEY_128 | Sizes.KEY_192 | Sizes.KEY_256;
mode: EnumToString<Modes.CTR>;
};
export declare const defaultEncryption: Encryption;
export declare const enum Algorithms {
SHA1 = "SHA1",
SHA2 = "SHA2",
RAW = "raw"
}
export type HashAlgorithm = {
algorithm: 'SHA1';
size?: 160;
algorithm: EnumToString<Algorithms.SHA1>;
size?: Sizes.KEY_160;
} | {
algorithm: 'SHA2';
size: 256 | 384 | 512;
algorithm: EnumToString<Algorithms.SHA2>;
size: Sizes.KEY_256 | Sizes.KEY_384 | Sizes.KEY_512;
};
export type Hash = 'raw' | HashAlgorithm;
export type Hash = EnumToString<Algorithms.RAW> | HashAlgorithm;
export declare const defaultHash: HashAlgorithm;
export type Encoding = 'base64' | 'base64url' | 'hex' | 'utf8';
export declare const enum Encodings {
BASE64 = "base64",
BASE64URL = "base64url",
HEX = "hex",
RAW = "raw",
UTF8 = "utf8"
}
export type Encoding = EnumToString<Encodings.BASE64 | Encodings.BASE64URL | Encodings.HEX | Encodings.UTF8>;
export declare const defaultEncoding: Encoding;
export type Curve = 'p256' | 'p384' | 'p521';
export declare const enum Curves {
P256 = "p256",
P384 = "p384",
P521 = "p521"
}
export type Curve = EnumToString<Curves>;
export declare const defaultCurve: Curve;

@@ -29,4 +67,5 @@ export type InputText = string | Uint8Array | {

text: Uint8Array;
encoding: 'raw';
encoding: EnumToString<Encodings.RAW>;
};
export {};
//# sourceMappingURL=types.d.ts.map

@@ -0,12 +1,50 @@

export var Ciphers = /*#__PURE__*/ function(Ciphers) {
Ciphers["AES"] = "AES";
return Ciphers;
}({});
export var Modes = /*#__PURE__*/ function(Modes) {
Modes["CBC"] = "CBC";
Modes["CTR"] = "CTR";
return Modes;
}({});
export var Sizes = /*#__PURE__*/ function(Sizes) {
Sizes[Sizes["KEY_128"] = 128] = "KEY_128";
Sizes[Sizes["KEY_160"] = 160] = "KEY_160";
Sizes[Sizes["KEY_192"] = 192] = "KEY_192";
Sizes[Sizes["KEY_256"] = 256] = "KEY_256";
Sizes[Sizes["KEY_384"] = 384] = "KEY_384";
Sizes[Sizes["KEY_512"] = 512] = "KEY_512";
return Sizes;
}({});
export const defaultEncryption = {
cipher: 'AES',
cipher: "AES",
size: 256,
mode: 'CTR'
mode: "CTR"
};
export var Algorithms = /*#__PURE__*/ function(Algorithms) {
Algorithms["SHA1"] = "SHA1";
Algorithms["SHA2"] = "SHA2";
Algorithms["RAW"] = "raw";
return Algorithms;
}({});
export const defaultHash = {
algorithm: 'SHA2',
algorithm: "SHA2",
size: 256
};
export const defaultEncoding = 'utf8';
export const defaultCurve = 'p256';
export var Encodings = /*#__PURE__*/ function(Encodings) {
Encodings["BASE64"] = "base64";
Encodings["BASE64URL"] = "base64url";
Encodings["HEX"] = "hex";
Encodings["RAW"] = "raw";
Encodings["UTF8"] = "utf8";
return Encodings;
}({});
export const defaultEncoding = "utf8";
export var Curves = /*#__PURE__*/ function(Curves) {
Curves["P256"] = "p256";
Curves["P384"] = "p384";
Curves["P521"] = "p521";
return Curves;
}({});
export const defaultCurve = "p256";
//# sourceMappingURL=types.js.map
{
"name": "iso-crypto",
"version": "1.1.3",
"version": "1.2.0",
"description": "Isomorphic cryptographic functions for browser and NodeJS.",

@@ -60,15 +60,16 @@ "engines": {

"@types/mocha": "^10.0.9",
"@types/node": "^22.7.7",
"@types/node": "^22.8.1",
"@types/sinon": "^17.0.3",
"c8": "^10.1.2",
"chai": "^5.1.1",
"chai": "^5.1.2",
"expect-type": "^1.1.0",
"mocha": "^10.7.0",
"mocha": "^10.7.3",
"sinon": "^19.0.2",
"typescript": "^5.6.3",
"@leyman/eslint-config": "^0.0.1",
"nx-tsc": "^0.0.10",
"nx-update-ts-references": "^0.0.5",
"mocha-chain": "^0.0.5"
"barrelify": "^1.2.1",
"@leyman/eslint-config": "^0.0.2",
"mocha-chain": "^0.0.6",
"nx-tsc": "^0.0.11",
"nx-update-ts-references": "^0.0.6"
}
}