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

@thirdweb-dev/crypto

Package Overview
Dependencies
Maintainers
8
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thirdweb-dev/crypto - npm Package Compare versions

Comparing version 0.0.0-dev-e1d23a7-20231116100754 to 0.2.0-nightly-4c5e40ae-20231117092939

dist/declarations/src/utils/uint8array-extras.d.ts

134

dist/thirdweb-dev-crypto.cjs.dev.js

@@ -5,6 +5,23 @@ 'use strict';

var uint8arrayExtras = require('uint8array-extras');
var sha256$1 = require('@noble/hashes/sha256');
var jsSha3 = require('js-sha3');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
class TextProcessorCache {

@@ -386,2 +403,95 @@ #encoder;

// taken from: https://github.com/sindresorhus/uint8array-extras
const objectToString = Object.prototype.toString;
const uint8ArrayStringified = "[object Uint8Array]";
function isUint8Array(value) {
if (!value) {
return false;
}
if (value.constructor === Uint8Array) {
return true;
}
return objectToString.call(value) === uint8ArrayStringified;
}
function assertUint8Array(value) {
if (!isUint8Array(value)) {
throw new TypeError(`Expected \`Uint8Array\`, got \`${typeof value}\``);
}
}
function concatUint8Arrays(arrays, totalLength) {
if (arrays.length === 0) {
return new Uint8Array(0);
}
totalLength ??= arrays.reduce((accumulator, currentValue) => accumulator + currentValue.length, 0);
const returnValue = new Uint8Array(totalLength);
let offset = 0;
for (const array of arrays) {
assertUint8Array(array);
returnValue.set(array, offset);
offset += array.length;
}
return returnValue;
}
function assertString(value) {
if (typeof value !== "string") {
throw new TypeError(`Expected \`string\`, got \`${typeof value}\``);
}
}
function base64ToBase64Url(base64) {
return base64.replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
}
function base64UrlToBase64(base64url) {
return base64url.replaceAll("-", "+").replaceAll("_", "/");
}
// Reference: https://phuoc.ng/collection/this-vs-that/concat-vs-push/
const MAX_BLOCK_SIZE = 65_535;
function uint8ArrayToBase64(array) {
let {
urlSafe = false
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
assertUint8Array(array);
let base64;
if (array.length < MAX_BLOCK_SIZE) {
// Required as `btoa` and `atob` don't properly support Unicode: https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
base64 = globalThis.btoa(String.fromCodePoint.apply(this, array));
} else {
base64 = "";
for (const value of array) {
base64 += String.fromCodePoint(value);
}
base64 = globalThis.btoa(base64);
}
return urlSafe ? base64ToBase64Url(base64) : base64;
}
function base64ToUint8Array(base64String) {
assertString(base64String);
return Uint8Array.from(globalThis.atob(base64UrlToBase64(base64String)), x => x.codePointAt(0));
}
const byteToHexLookupTable = Array.from({
length: 256
}, (_, index) => index.toString(16).padStart(2, "0"));
function uint8ArrayToHex(array) {
assertUint8Array(array);
// Concatenating a string is faster than using an array.
let hexString = "";
for (let index = 0; index < array.length; index++) {
hexString += byteToHexLookupTable[array[index]];
}
return hexString;
}
async function universalCrypto() {
if ("crypto" in globalThis) {
return globalThis.crypto;
}
// otherwise we are in node 18 so we can use `webcrypto` off of the "node:crypto" package and treat it as native
// trick bundlers so that they leave this alone :)
const pto = "pto";
// this becomes `node:crypto` at runtime
return (await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })("node" + ":cry" + pto)).webcrypto;
}
/**

@@ -399,2 +509,3 @@ * This is an implementation of the CryptoJS AES decryption scheme, without actually relying on crypto-js.

} = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
const crypto = await universalCrypto();
const {

@@ -411,3 +522,3 @@ key,

// return the plaintext from ArrayBuffer
return new TextDecoder().decode(plainBuffer);
return getCachedTextDecoder().decode(plainBuffer);
} catch (e) {

@@ -419,3 +530,3 @@ throw new Error("Decrypt failed");

let salt = null;
let ciphertext = uint8arrayExtras.base64ToUint8Array(cryptoJSCipherBase64);
let ciphertext = base64ToUint8Array(cryptoJSCipherBase64);
const [head, body] = splitUint8Array(ciphertext, HEAD_SIZE_DWORD * 4);

@@ -434,2 +545,3 @@

async function dangerouslyDeriveParameters(password, salt, keySizeDWORD, ivSizeDWORD, iterations) {
const crypto = await universalCrypto();
const passwordUint8Array = getCachedTextEncoder().encode(password);

@@ -448,7 +560,7 @@ const keyPlusIV = dangerousEVPKDF(passwordUint8Array, salt, keySizeDWORD + ivSizeDWORD, iterations);

while (derivedKey.byteLength < keySizeDWORD * 4) {
block = new Uint8Array(arrayBuffer(uint8arrayExtras.concatUint8Arrays([block, passwordUint8Array, saltUint8Array])));
block = new Uint8Array(arrayBuffer(concatUint8Arrays([block, passwordUint8Array, saltUint8Array])));
for (let i = 1; i < iterations; i++) {
block = new Uint8Array(arrayBuffer(block));
}
derivedKey = uint8arrayExtras.concatUint8Arrays([derivedKey, block]);
derivedKey = concatUint8Arrays([derivedKey, block]);
}

@@ -472,2 +584,3 @@ return derivedKey;

async function aesDecrypt(ciphertext, password) {
const crypto = await universalCrypto();
// encode password as UTF-8

@@ -477,3 +590,3 @@ const pwUtf8 = getCachedTextEncoder().encode(password);

const pwHash = await crypto.subtle.digest("SHA-256", pwUtf8);
const cipherUint8Array = uint8arrayExtras.base64ToUint8Array(ciphertext);
const cipherUint8Array = base64ToUint8Array(ciphertext);

@@ -535,2 +648,3 @@ // iv

async function aesEncrypt(plaintext, password) {
const crypto = await universalCrypto();
const textEncoder = getCachedTextEncoder();

@@ -560,3 +674,3 @@ // encode password as UTF-8

// iv+ciphertext base64-encoded
return uint8arrayExtras.uint8ArrayToBase64(uint8arrayExtras.concatUint8Arrays([iv, new Uint8Array(ctBuffer)]));
return uint8ArrayToBase64(concatUint8Arrays([iv, new Uint8Array(ctBuffer)]));
}

@@ -578,3 +692,3 @@

}
return new Uint8Array(await crypto.subtle.digest("SHA-256", encodedValue));
return new Uint8Array(await (await universalCrypto()).subtle.digest("SHA-256", encodedValue));
}

@@ -588,3 +702,3 @@

async function sha256Hex(value) {
return uint8arrayExtras.uint8ArrayToHex(await sha256(value));
return uint8ArrayToHex(await sha256(value));
}

@@ -607,3 +721,3 @@

function sha256HexSync(value) {
return uint8arrayExtras.uint8ArrayToHex(sha256Sync(value));
return uint8ArrayToHex(sha256Sync(value));
}

@@ -610,0 +724,0 @@

@@ -5,6 +5,23 @@ 'use strict';

var uint8arrayExtras = require('uint8array-extras');
var sha256$1 = require('@noble/hashes/sha256');
var jsSha3 = require('js-sha3');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
class TextProcessorCache {

@@ -386,2 +403,95 @@ #encoder;

// taken from: https://github.com/sindresorhus/uint8array-extras
const objectToString = Object.prototype.toString;
const uint8ArrayStringified = "[object Uint8Array]";
function isUint8Array(value) {
if (!value) {
return false;
}
if (value.constructor === Uint8Array) {
return true;
}
return objectToString.call(value) === uint8ArrayStringified;
}
function assertUint8Array(value) {
if (!isUint8Array(value)) {
throw new TypeError(`Expected \`Uint8Array\`, got \`${typeof value}\``);
}
}
function concatUint8Arrays(arrays, totalLength) {
if (arrays.length === 0) {
return new Uint8Array(0);
}
totalLength ??= arrays.reduce((accumulator, currentValue) => accumulator + currentValue.length, 0);
const returnValue = new Uint8Array(totalLength);
let offset = 0;
for (const array of arrays) {
assertUint8Array(array);
returnValue.set(array, offset);
offset += array.length;
}
return returnValue;
}
function assertString(value) {
if (typeof value !== "string") {
throw new TypeError(`Expected \`string\`, got \`${typeof value}\``);
}
}
function base64ToBase64Url(base64) {
return base64.replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
}
function base64UrlToBase64(base64url) {
return base64url.replaceAll("-", "+").replaceAll("_", "/");
}
// Reference: https://phuoc.ng/collection/this-vs-that/concat-vs-push/
const MAX_BLOCK_SIZE = 65_535;
function uint8ArrayToBase64(array) {
let {
urlSafe = false
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
assertUint8Array(array);
let base64;
if (array.length < MAX_BLOCK_SIZE) {
// Required as `btoa` and `atob` don't properly support Unicode: https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
base64 = globalThis.btoa(String.fromCodePoint.apply(this, array));
} else {
base64 = "";
for (const value of array) {
base64 += String.fromCodePoint(value);
}
base64 = globalThis.btoa(base64);
}
return urlSafe ? base64ToBase64Url(base64) : base64;
}
function base64ToUint8Array(base64String) {
assertString(base64String);
return Uint8Array.from(globalThis.atob(base64UrlToBase64(base64String)), x => x.codePointAt(0));
}
const byteToHexLookupTable = Array.from({
length: 256
}, (_, index) => index.toString(16).padStart(2, "0"));
function uint8ArrayToHex(array) {
assertUint8Array(array);
// Concatenating a string is faster than using an array.
let hexString = "";
for (let index = 0; index < array.length; index++) {
hexString += byteToHexLookupTable[array[index]];
}
return hexString;
}
async function universalCrypto() {
if ("crypto" in globalThis) {
return globalThis.crypto;
}
// otherwise we are in node 18 so we can use `webcrypto` off of the "node:crypto" package and treat it as native
// trick bundlers so that they leave this alone :)
const pto = "pto";
// this becomes `node:crypto` at runtime
return (await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })("node" + ":cry" + pto)).webcrypto;
}
/**

@@ -399,2 +509,3 @@ * This is an implementation of the CryptoJS AES decryption scheme, without actually relying on crypto-js.

} = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
const crypto = await universalCrypto();
const {

@@ -411,3 +522,3 @@ key,

// return the plaintext from ArrayBuffer
return new TextDecoder().decode(plainBuffer);
return getCachedTextDecoder().decode(plainBuffer);
} catch (e) {

@@ -419,3 +530,3 @@ throw new Error("Decrypt failed");

let salt = null;
let ciphertext = uint8arrayExtras.base64ToUint8Array(cryptoJSCipherBase64);
let ciphertext = base64ToUint8Array(cryptoJSCipherBase64);
const [head, body] = splitUint8Array(ciphertext, HEAD_SIZE_DWORD * 4);

@@ -434,2 +545,3 @@

async function dangerouslyDeriveParameters(password, salt, keySizeDWORD, ivSizeDWORD, iterations) {
const crypto = await universalCrypto();
const passwordUint8Array = getCachedTextEncoder().encode(password);

@@ -448,7 +560,7 @@ const keyPlusIV = dangerousEVPKDF(passwordUint8Array, salt, keySizeDWORD + ivSizeDWORD, iterations);

while (derivedKey.byteLength < keySizeDWORD * 4) {
block = new Uint8Array(arrayBuffer(uint8arrayExtras.concatUint8Arrays([block, passwordUint8Array, saltUint8Array])));
block = new Uint8Array(arrayBuffer(concatUint8Arrays([block, passwordUint8Array, saltUint8Array])));
for (let i = 1; i < iterations; i++) {
block = new Uint8Array(arrayBuffer(block));
}
derivedKey = uint8arrayExtras.concatUint8Arrays([derivedKey, block]);
derivedKey = concatUint8Arrays([derivedKey, block]);
}

@@ -472,2 +584,3 @@ return derivedKey;

async function aesDecrypt(ciphertext, password) {
const crypto = await universalCrypto();
// encode password as UTF-8

@@ -477,3 +590,3 @@ const pwUtf8 = getCachedTextEncoder().encode(password);

const pwHash = await crypto.subtle.digest("SHA-256", pwUtf8);
const cipherUint8Array = uint8arrayExtras.base64ToUint8Array(ciphertext);
const cipherUint8Array = base64ToUint8Array(ciphertext);

@@ -535,2 +648,3 @@ // iv

async function aesEncrypt(plaintext, password) {
const crypto = await universalCrypto();
const textEncoder = getCachedTextEncoder();

@@ -560,3 +674,3 @@ // encode password as UTF-8

// iv+ciphertext base64-encoded
return uint8arrayExtras.uint8ArrayToBase64(uint8arrayExtras.concatUint8Arrays([iv, new Uint8Array(ctBuffer)]));
return uint8ArrayToBase64(concatUint8Arrays([iv, new Uint8Array(ctBuffer)]));
}

@@ -578,3 +692,3 @@

}
return new Uint8Array(await crypto.subtle.digest("SHA-256", encodedValue));
return new Uint8Array(await (await universalCrypto()).subtle.digest("SHA-256", encodedValue));
}

@@ -588,3 +702,3 @@

async function sha256Hex(value) {
return uint8arrayExtras.uint8ArrayToHex(await sha256(value));
return uint8ArrayToHex(await sha256(value));
}

@@ -607,3 +721,3 @@

function sha256HexSync(value) {
return uint8arrayExtras.uint8ArrayToHex(sha256Sync(value));
return uint8ArrayToHex(sha256Sync(value));
}

@@ -610,0 +724,0 @@

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

import { base64ToUint8Array, concatUint8Arrays, uint8ArrayToBase64, uint8ArrayToHex } from 'uint8array-extras';
import { sha256 as sha256$1 } from '@noble/hashes/sha256';

@@ -381,2 +380,95 @@ import { keccak_256 } from 'js-sha3';

// taken from: https://github.com/sindresorhus/uint8array-extras
const objectToString = Object.prototype.toString;
const uint8ArrayStringified = "[object Uint8Array]";
function isUint8Array(value) {
if (!value) {
return false;
}
if (value.constructor === Uint8Array) {
return true;
}
return objectToString.call(value) === uint8ArrayStringified;
}
function assertUint8Array(value) {
if (!isUint8Array(value)) {
throw new TypeError(`Expected \`Uint8Array\`, got \`${typeof value}\``);
}
}
function concatUint8Arrays(arrays, totalLength) {
if (arrays.length === 0) {
return new Uint8Array(0);
}
totalLength ??= arrays.reduce((accumulator, currentValue) => accumulator + currentValue.length, 0);
const returnValue = new Uint8Array(totalLength);
let offset = 0;
for (const array of arrays) {
assertUint8Array(array);
returnValue.set(array, offset);
offset += array.length;
}
return returnValue;
}
function assertString(value) {
if (typeof value !== "string") {
throw new TypeError(`Expected \`string\`, got \`${typeof value}\``);
}
}
function base64ToBase64Url(base64) {
return base64.replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
}
function base64UrlToBase64(base64url) {
return base64url.replaceAll("-", "+").replaceAll("_", "/");
}
// Reference: https://phuoc.ng/collection/this-vs-that/concat-vs-push/
const MAX_BLOCK_SIZE = 65_535;
function uint8ArrayToBase64(array) {
let {
urlSafe = false
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
assertUint8Array(array);
let base64;
if (array.length < MAX_BLOCK_SIZE) {
// Required as `btoa` and `atob` don't properly support Unicode: https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
base64 = globalThis.btoa(String.fromCodePoint.apply(this, array));
} else {
base64 = "";
for (const value of array) {
base64 += String.fromCodePoint(value);
}
base64 = globalThis.btoa(base64);
}
return urlSafe ? base64ToBase64Url(base64) : base64;
}
function base64ToUint8Array(base64String) {
assertString(base64String);
return Uint8Array.from(globalThis.atob(base64UrlToBase64(base64String)), x => x.codePointAt(0));
}
const byteToHexLookupTable = Array.from({
length: 256
}, (_, index) => index.toString(16).padStart(2, "0"));
function uint8ArrayToHex(array) {
assertUint8Array(array);
// Concatenating a string is faster than using an array.
let hexString = "";
for (let index = 0; index < array.length; index++) {
hexString += byteToHexLookupTable[array[index]];
}
return hexString;
}
async function universalCrypto() {
if ("crypto" in globalThis) {
return globalThis.crypto;
}
// otherwise we are in node 18 so we can use `webcrypto` off of the "node:crypto" package and treat it as native
// trick bundlers so that they leave this alone :)
const pto = "pto";
// this becomes `node:crypto` at runtime
return (await import("node" + ":cry" + pto)).webcrypto;
}
/**

@@ -394,2 +486,3 @@ * This is an implementation of the CryptoJS AES decryption scheme, without actually relying on crypto-js.

} = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
const crypto = await universalCrypto();
const {

@@ -406,3 +499,3 @@ key,

// return the plaintext from ArrayBuffer
return new TextDecoder().decode(plainBuffer);
return getCachedTextDecoder().decode(plainBuffer);
} catch (e) {

@@ -428,2 +521,3 @@ throw new Error("Decrypt failed");

async function dangerouslyDeriveParameters(password, salt, keySizeDWORD, ivSizeDWORD, iterations) {
const crypto = await universalCrypto();
const passwordUint8Array = getCachedTextEncoder().encode(password);

@@ -465,2 +559,3 @@ const keyPlusIV = dangerousEVPKDF(passwordUint8Array, salt, keySizeDWORD + ivSizeDWORD, iterations);

async function aesDecrypt(ciphertext, password) {
const crypto = await universalCrypto();
// encode password as UTF-8

@@ -527,2 +622,3 @@ const pwUtf8 = getCachedTextEncoder().encode(password);

async function aesEncrypt(plaintext, password) {
const crypto = await universalCrypto();
const textEncoder = getCachedTextEncoder();

@@ -569,3 +665,3 @@ // encode password as UTF-8

}
return new Uint8Array(await crypto.subtle.digest("SHA-256", encodedValue));
return new Uint8Array(await (await universalCrypto()).subtle.digest("SHA-256", encodedValue));
}

@@ -572,0 +668,0 @@

7

package.json
{
"name": "@thirdweb-dev/crypto",
"version": "0.0.0-dev-e1d23a7-20231116100754",
"version": "0.2.0-nightly-4c5e40ae-20231117092939",
"main": "dist/thirdweb-dev-crypto.cjs.js",

@@ -31,4 +31,3 @@ "module": "dist/thirdweb-dev-crypto.esm.js",

"@noble/hashes": "^1.3.2",
"js-sha3": "^0.9.2",
"uint8array-extras": "^0.4.0"
"js-sha3": "^0.9.2"
},

@@ -52,3 +51,3 @@ "devDependencies": {

"format": "prettier --write 'src/**/*'",
"lint": "eslint src/",
"lint": "eslint src/ && bunx publint --strict --level warning",
"fix": "eslint src/ --fix",

@@ -55,0 +54,0 @@ "clean": "rm -rf dist/",

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