@hashgraph/cryptography
Advanced tools
Comparing version 1.0.7 to 1.0.8
{ | ||
"name": "@hashgraph/cryptography", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Cryptographic utilities and primitives for the Hedera™ Hashgraph SDK", | ||
"main": "lib/index.cjs", | ||
"types": "lib/index.d.ts", | ||
"browser": "src/index.js", | ||
"browser": { | ||
"src/index.js": "src/index.js", | ||
"src/encoding/bas64.js": "src/encoding/bas64.browser.js", | ||
"src/encoding/hex.js": "src/encoding/hex.browser.js", | ||
"src/encoding/utf8.js": "src/encoding/utf8.browser.js", | ||
"src/primitive/aes.js": "src/primitive/aes.browser.js", | ||
"src/primitive/sha256.js": "src/primitive/sha256.browser.js", | ||
"src/primitive/pbkdf2.js": "src/primitive/pbkdf2.browser.js", | ||
"src/primitive/hmac.js": "src/primitive/hmac.browser.js" | ||
}, | ||
"module": "src/index.js", | ||
@@ -45,3 +54,3 @@ "exports": { | ||
"test": "run-s test:node test:browser:chrome test:browser:firefox", | ||
"test:node": "mocha -r @babel/register -r chai/register-expect 'test/unit/**/*.js'", | ||
"test:node": "env HEDERA_SDK_TEST='' mocha -r @babel/register -r chai/register-expect 'test/unit/**/*.js'", | ||
"test:browser:chrome": "vite serve --port 9001 test/ & sleep 2; mocha-webdriver-runner --headless-chrome http://localhost:9001/; kill %1", | ||
@@ -48,0 +57,0 @@ "test:browser:firefox": "vite serve --port 9002 test/ & sleep 2; mocha-webdriver-runner --headless-firefox http://localhost:9002/; kill %1", |
@@ -6,8 +6,3 @@ /** | ||
export function decode(text) { | ||
if (typeof Buffer !== "undefined") { | ||
return Buffer.from(text, "base64"); | ||
} | ||
// note: assumes <atob> is available in the global scope if <Buffer> is not | ||
return Uint8Array.from(atob(text), (c) => c.charCodeAt(0)); | ||
return Buffer.from(text, "base64"); | ||
} | ||
@@ -20,8 +15,3 @@ | ||
export function encode(data) { | ||
if (typeof Buffer !== "undefined") { | ||
return Buffer.from(data).toString("base64"); | ||
} | ||
// note: assumes <btoa> is available in the global scope if <Buffer> is not | ||
return btoa(String.fromCharCode.apply(null, Array.from(data))); | ||
return Buffer.from(data).toString("base64"); | ||
} |
@@ -15,13 +15,3 @@ /** | ||
export function encode(data) { | ||
if (typeof Buffer !== "undefined") { | ||
return Buffer.from(data).toString("hex"); | ||
} | ||
let string = ""; | ||
for (const byte of data) { | ||
string += byteToHex[byte]; | ||
} | ||
return string; | ||
return Buffer.from(data).toString("hex"); | ||
} | ||
@@ -35,12 +25,3 @@ | ||
const str = text.startsWith("0x") ? text.substring(2) : text; | ||
if (typeof Buffer !== "undefined") { | ||
return Buffer.from(str, "hex"); | ||
} | ||
const result = str.match(/.{1,2}/gu); | ||
return new Uint8Array( | ||
(result == null ? [] : result).map((byte) => parseInt(byte, 16)) | ||
); | ||
return Buffer.from(str, "hex"); | ||
} |
@@ -6,8 +6,3 @@ /** | ||
export function decode(data) { | ||
if (typeof Buffer !== "undefined") { | ||
return Buffer.from(data).toString("utf8"); | ||
} | ||
// eslint-disable-next-line node/no-unsupported-features/node-builtins | ||
return new TextDecoder().decode(data); | ||
return Buffer.from(data).toString("utf8"); | ||
} | ||
@@ -20,8 +15,3 @@ | ||
export function encode(text) { | ||
if (typeof Buffer !== "undefined") { | ||
return Buffer.from(text, "utf8"); | ||
} | ||
// eslint-disable-next-line node/no-unsupported-features/node-builtins | ||
return new TextEncoder().encode(text); | ||
return Buffer.from(text, "utf8"); | ||
} |
@@ -14,45 +14,9 @@ export const CipherAlgorithm = { | ||
export async function createCipheriv(algorithm, key, iv, data) { | ||
if (typeof Buffer !== "undefined") { | ||
const cipher = (await import("crypto")).createCipheriv( | ||
algorithm, | ||
key.slice(0, 16), | ||
iv | ||
); | ||
const cipher = (await import("crypto")).createCipheriv( | ||
algorithm, | ||
key.slice(0, 16), | ||
iv | ||
); | ||
return Buffer.concat([cipher.update(data), cipher["final"]()]); | ||
} else { | ||
let algorithm_; | ||
switch (algorithm) { | ||
case CipherAlgorithm.Aes128Ctr: | ||
algorithm_ = { | ||
name: "AES-CTR", | ||
counter: iv, | ||
length: 128, | ||
}; | ||
break; | ||
case CipherAlgorithm.Aes128Cbc: | ||
algorithm_ = { | ||
name: "AES-CBC", | ||
length: 128, | ||
}; | ||
break; | ||
default: | ||
throw new Error( | ||
"(BUG) non-exhaustive switch statement for CipherAlgorithm" | ||
); | ||
} | ||
const key_ = await window.crypto.subtle.importKey( | ||
"raw", | ||
key, | ||
algorithm_.name, | ||
false, | ||
["encrypt"] | ||
); | ||
return new Uint8Array( | ||
await window.crypto.subtle.encrypt(algorithm_, key_, data) | ||
); | ||
} | ||
return Buffer.concat([cipher.update(data), cipher["final"]()]); | ||
} | ||
@@ -68,45 +32,9 @@ | ||
export async function createDecipheriv(algorithm, key, iv, data) { | ||
if (typeof Buffer !== "undefined") { | ||
const decipher = (await import("crypto")).createDecipheriv( | ||
algorithm, | ||
key.slice(0, 16), | ||
iv | ||
); | ||
const decipher = (await import("crypto")).createDecipheriv( | ||
algorithm, | ||
key.slice(0, 16), | ||
iv | ||
); | ||
return Buffer.concat([decipher.update(data), decipher["final"]()]); | ||
} else { | ||
let algorithm_; | ||
switch (algorithm) { | ||
case CipherAlgorithm.Aes128Ctr: | ||
algorithm_ = { | ||
name: "AES-CTR", | ||
counter: iv, | ||
length: 128, | ||
}; | ||
break; | ||
case CipherAlgorithm.Aes128Cbc: | ||
algorithm_ = { | ||
name: "AES-CBC", | ||
iv, | ||
}; | ||
break; | ||
default: | ||
throw new Error( | ||
"(BUG) non-exhaustive switch statement for CipherAlgorithm" | ||
); | ||
} | ||
const key_ = await window.crypto.subtle.importKey( | ||
"raw", | ||
key, | ||
algorithm_.name, | ||
false, | ||
["decrypt"] | ||
); | ||
return new Uint8Array( | ||
await window.crypto.subtle.decrypt(algorithm_, key_, data) | ||
); | ||
} | ||
return Buffer.concat([decipher.update(data), decipher["final"]()]); | ||
} |
@@ -23,25 +23,2 @@ import * as utf8 from "../encoding/utf8.js"; | ||
if (typeof Buffer === "undefined") { | ||
try { | ||
const key_ = await window.crypto.subtle.importKey( | ||
"raw", | ||
key, | ||
{ | ||
name: "HMAC", | ||
hash: algorithm, | ||
}, | ||
false, | ||
["sign"] | ||
); | ||
return new Uint8Array( | ||
await window.crypto.subtle.sign("HMAC", key_, value) | ||
); | ||
} catch { | ||
throw new Error( | ||
"Fallback if SubtleCrypto fails is not implemented" | ||
); | ||
} | ||
} | ||
const crypto = await import("crypto"); | ||
@@ -48,0 +25,0 @@ |
@@ -22,34 +22,2 @@ import { HashAlgorithm } from "./hmac.js"; | ||
if (typeof Buffer === "undefined") { | ||
try { | ||
const key = await window.crypto.subtle.importKey( | ||
"raw", | ||
pass, | ||
{ | ||
name: "PBKDF2", | ||
hash: algorithm, | ||
}, | ||
false, | ||
["deriveBits"] | ||
); | ||
return new Uint8Array( | ||
await window.crypto.subtle.deriveBits( | ||
{ | ||
name: "PBKDF2", | ||
hash: algorithm, | ||
salt: nacl, | ||
iterations, | ||
}, | ||
key, | ||
length << 3 | ||
) | ||
); | ||
} catch { | ||
throw new Error( | ||
"(BUG) Non-Exhaustive switch statement for algorithms" | ||
); | ||
} | ||
} | ||
const util = await import("util"); | ||
@@ -61,7 +29,7 @@ const crypto = await import("crypto"); | ||
case HashAlgorithm.Sha256: | ||
return pbkdf2(password, nacl, iterations, length, "sha256"); | ||
return pbkdf2(pass, nacl, iterations, length, "sha256"); | ||
case HashAlgorithm.Sha384: | ||
return pbkdf2(password, nacl, iterations, length, "sha384"); | ||
return pbkdf2(pass, nacl, iterations, length, "sha384"); | ||
case HashAlgorithm.Sha512: | ||
return pbkdf2(password, nacl, iterations, length, "sha512"); | ||
return pbkdf2(pass, nacl, iterations, length, "sha512"); | ||
default: | ||
@@ -68,0 +36,0 @@ throw new Error( |
@@ -6,10 +6,4 @@ /** | ||
export async function digest(data) { | ||
// try subtle crypto if it exists | ||
if (typeof crypto !== "undefined" && typeof crypto.subtle !== "undefined") { | ||
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest | ||
return new Uint8Array(await crypto.subtle.digest("SHA-256", data)); | ||
} | ||
// fallback to trying node-crypto which could be polyfilled by the browser environment | ||
return (await import("crypto")).createHash("sha256").update(data).digest(); | ||
} |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
291076
94
11028
0