@secrez/crypto
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "@secrez/crypto", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"license": "MIT", | ||
@@ -8,9 +8,2 @@ "publishConfig": { | ||
}, | ||
"scripts": { | ||
"lint": "eslint -c .eslintrc 'src/**/*.js' 'test/**/*.js'", | ||
"all-tests": "find test/** -name '*.test.js' | xargs ./node_modules/.bin/mocha -R spec", | ||
"test-only": "cross-env NODE_ENV=test ./node_modules/.bin/mocha test/*.test.js test/**/*.test.js --exit", | ||
"test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text ./node_modules/.bin/_mocha test/*.test.js test/**/*.test.js --exit", | ||
"posttest": "nyc check-coverage --statements 99 --branches 85 --functions 99 --lines 99" | ||
}, | ||
"dependencies": { | ||
@@ -25,11 +18,9 @@ "base-x": "^3.0.8", | ||
"devDependencies": { | ||
"@secrez/utils": "workspace:~1.0.2", | ||
"@secrez/utils": "~1.0.3", | ||
"chai": "^4.2.0", | ||
"cross-env": "^7.0.2", | ||
"eslint": "^6.8.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"mocha": "^7.1.2", | ||
"nyc": "^15.1.0" | ||
}, | ||
"description": "The Secrez core library.", | ||
"description": "The Secrez crypto library.", | ||
"main": "src/index.js", | ||
@@ -46,3 +37,9 @@ "repository": { | ||
"node": ">=10.0.0" | ||
}, | ||
"scripts": { | ||
"all-tests": "find test/** -name '*.test.js' | xargs ./node_modules/.bin/mocha -R spec", | ||
"test-only": "cross-env NODE_ENV=test ./node_modules/.bin/mocha test/*.test.js test/**/*.test.js --exit", | ||
"test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.test.js test/**/*.test.js --exit", | ||
"posttest": "nyc check-coverage --statements 99 --branches 85 --functions 99 --lines 99" | ||
} | ||
} | ||
} |
@@ -5,3 +5,3 @@ # @secrez/crypto | ||
@secrez/crypto is the basic crypto library. It includes only functions that can be called also in a browser. | ||
@secrez/crypto is the basic crypto library. It includes only functions that can be called also in a browser. | ||
@@ -16,19 +16,22 @@ In @secrez/core it is integrated with methods who works only server side, in order to be used in Secrez. | ||
__0.1.5__ | ||
* adds `bufferToUnti8Array` | ||
**0.1.5** | ||
__0.1.4__ | ||
* adds more methods to encrypt and decrypt buffers too | ||
- adds `bufferToUnti8Array` | ||
__0.1.2__ | ||
* moving all methods that don't work in a browser back to @secrez/core | ||
**0.1.4** | ||
__0.1.0__ | ||
* moved from @secrez/core to this separate library | ||
- adds more methods to encrypt and decrypt buffers too | ||
**0.1.2** | ||
- moving all methods that don't work in a browser back to @secrez/core | ||
**0.1.0** | ||
- moved from @secrez/core to this separate library | ||
## Test coverage | ||
``` | ||
35 passing (382ms) | ||
35 passing (391ms) | ||
2 pending | ||
@@ -40,3 +43,3 @@ | ||
All files | 100 | 89.36 | 100 | 100 | | ||
index.js | 100 | 89.36 | 100 | 100 | 42-53,82,108-119,188-189,208,368-372 | ||
index.js | 100 | 89.36 | 100 | 100 | 34-45,78,104-115,195-199,224,401-405 | ||
----------|---------|----------|---------|---------|-------------------------------------- | ||
@@ -46,3 +49,2 @@ | ||
## Copyright | ||
@@ -49,0 +51,0 @@ |
451
src/index.js
@@ -1,39 +0,31 @@ | ||
const crypto = require('crypto') | ||
const {Keccak} = require('sha3') | ||
const basex = require('base-x') | ||
const shamir = require('shamir') | ||
const {bytesToBase64, base64ToBytes} = require('byte-base64') | ||
const {TextEncoder, TextDecoder} = require('util') | ||
const utf8Encoder = new TextEncoder() | ||
const utf8Decoder = new TextDecoder() | ||
const μs = require('microseconds') | ||
const crypto = require("crypto"); | ||
const { Keccak } = require("sha3"); | ||
const basex = require("base-x"); | ||
const shamir = require("shamir"); | ||
const { bytesToBase64, base64ToBytes } = require("byte-base64"); | ||
const { TextEncoder, TextDecoder } = require("util"); | ||
const utf8Encoder = new TextEncoder(); | ||
const utf8Decoder = new TextDecoder(); | ||
const μs = require("microseconds"); | ||
const SAFE_ENC = { | ||
'+': '-', | ||
'/': '_', | ||
'=': '' | ||
} | ||
"+": "-", | ||
"/": "_", | ||
"=": "", | ||
}; | ||
const SAFE_DEC = { | ||
'-': '+', | ||
_: '/' | ||
} | ||
"-": "+", | ||
_: "/", | ||
}; | ||
const { | ||
box, | ||
secretbox, | ||
sign, | ||
randomBytes | ||
} = require('tweetnacl') | ||
const { box, secretbox, sign, randomBytes } = require("tweetnacl"); | ||
class Crypto { | ||
static toBase64(data) { | ||
return Buffer.from(data).toString('base64') | ||
return Buffer.from(data).toString("base64"); | ||
} | ||
static fromBase64(data) { | ||
return Buffer.from(data, 'base64').toString('utf-8') | ||
return Buffer.from(data, "base64").toString("utf-8"); | ||
} | ||
@@ -43,9 +35,9 @@ | ||
if (!Buffer.isBuffer(data)) { | ||
data = Buffer.from(data) | ||
data = Buffer.from(data); | ||
} | ||
return Crypto.bs58.encode(data) | ||
return Crypto.bs58.encode(data); | ||
} | ||
static fromBase58(data) { | ||
return Crypto.bs58.decode(data) | ||
return Crypto.bs58.decode(data); | ||
} | ||
@@ -55,29 +47,33 @@ | ||
if (!Buffer.isBuffer(data)) { | ||
data = Buffer.from(data) | ||
data = Buffer.from(data); | ||
} | ||
return Crypto.bs32.encode(data) | ||
return Crypto.bs32.encode(data); | ||
} | ||
static fromBase32(data) { | ||
return Crypto.bs32.decode(data) | ||
return Crypto.bs32.decode(data); | ||
} | ||
static getRandomBase58String(size) { | ||
let i = Math.round(size / 2) | ||
let j = i + size | ||
return Crypto.bs58.encode(Buffer.from(randomBytes(2 * size))).substring(i, j) | ||
let i = Math.round(size / 2); | ||
let j = i + size; | ||
return Crypto.bs58 | ||
.encode(Buffer.from(randomBytes(2 * size))) | ||
.substring(i, j); | ||
} | ||
static getRandomBase32String(size) { | ||
let i = Math.round(size / 2) | ||
let j = i + size | ||
return Crypto.bs32.encode(Buffer.from(randomBytes(2 * size))).substring(i, j) | ||
let i = Math.round(size / 2); | ||
let j = i + size; | ||
return Crypto.bs32 | ||
.encode(Buffer.from(randomBytes(2 * size))) | ||
.substring(i, j); | ||
} | ||
static getRandomId(allIds) { | ||
let id | ||
for (; ;) { | ||
id = Crypto.getRandomBase58String(4) | ||
let id; | ||
for (;;) { | ||
id = Crypto.getRandomBase58String(4); | ||
if (!/^[a-zA-Z]+/.test(id)) { | ||
continue | ||
continue; | ||
} | ||
@@ -87,7 +83,7 @@ if (allIds) { | ||
if (allIds[id]) { | ||
continue | ||
continue; | ||
} | ||
// allIds[id] = true | ||
} | ||
return id | ||
return id; | ||
} | ||
@@ -97,13 +93,13 @@ } | ||
static SHA3(data) { | ||
const hash = new Keccak(256) | ||
hash.update(data) | ||
return hash.digest() | ||
const hash = new Keccak(256); | ||
hash.update(data); | ||
return hash.digest(); | ||
} | ||
static getRandomString(length = 12, encode = 'hex') { | ||
return crypto.randomBytes(length).toString(encode) | ||
static getRandomString(length = 12, encode = "hex") { | ||
return crypto.randomBytes(length).toString(encode); | ||
} | ||
static deriveKey(key, salt, iterations, size = 32, digest = 'sha512') { | ||
return crypto.pbkdf2Sync(key, salt, iterations, size, digest) | ||
static deriveKey(key, salt, iterations, size = 32, digest = "sha512") { | ||
return crypto.pbkdf2Sync(key, salt, iterations, size, digest); | ||
} | ||
@@ -113,9 +109,9 @@ | ||
if (!Buffer.isBuffer(data)) { | ||
data = Buffer.from(data) | ||
data = Buffer.from(data); | ||
} | ||
return Crypto.bs58.encode(Crypto.SHA3(data)).substring(0, size) | ||
return Crypto.bs58.encode(Crypto.SHA3(data)).substring(0, size); | ||
} | ||
static b64Hash(data, size) { | ||
return Crypto.bs64.encode(Crypto.SHA3(data)).substring(0, size) | ||
return Crypto.bs64.encode(Crypto.SHA3(data)).substring(0, size); | ||
} | ||
@@ -125,13 +121,13 @@ | ||
if (!Buffer.isBuffer(data)) { | ||
data = Buffer.from(data) | ||
data = Buffer.from(data); | ||
} | ||
return Crypto.bs32.encode(Crypto.SHA3(data)).substring(0, size) | ||
return Crypto.bs32.encode(Crypto.SHA3(data)).substring(0, size); | ||
} | ||
static isValidB58Hash(hash) { | ||
return Crypto.bs58.decode(hash).length === 32 | ||
return Crypto.bs58.decode(hash).length === 32; | ||
} | ||
static isValidB32Hash(hash) { | ||
return Crypto.bs32.decode(hash).length === 32 | ||
return Crypto.bs32.decode(hash).length === 32; | ||
} | ||
@@ -141,106 +137,124 @@ | ||
if (hexStr.length % 2) { | ||
hexStr = '0' + hexStr | ||
hexStr = "0" + hexStr; | ||
} | ||
return new Uint8Array(hexStr.match(/.{1,2}/g).map(byte => parseInt(byte, 16))) | ||
return new Uint8Array( | ||
hexStr.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)) | ||
); | ||
} | ||
static bufferToUint8Array(buf) { | ||
return new Uint8Array(buf) | ||
return new Uint8Array(buf); | ||
} | ||
static uint8ArrayToHex(uint8) { | ||
return Buffer.from(uint8).toString('hex') | ||
return Buffer.from(uint8).toString("hex"); | ||
} | ||
static newTimeBasedNonce(size, timestamp = Date.now()) { | ||
let nonce = randomBytes(size) | ||
timestamp = timestamp.toString(16) | ||
let ts = Crypto.hexToUint8Array(timestamp) | ||
let nonce = randomBytes(size); | ||
timestamp = timestamp.toString(16); | ||
let ts = Crypto.hexToUint8Array(timestamp); | ||
for (let i = 0; i < 6; i++) { | ||
nonce[i] = ts[i] | ||
nonce[i] = ts[i]; | ||
} | ||
return nonce | ||
return nonce; | ||
} | ||
static getTimestampFromNonce(nonce) { | ||
nonce = nonce.slice(0, 6) | ||
let ts = Crypto.uint8ArrayToHex(nonce) | ||
return parseInt(ts, 16) | ||
nonce = nonce.slice(0, 6); | ||
let ts = Crypto.uint8ArrayToHex(nonce); | ||
return parseInt(ts, 16); | ||
} | ||
static generateKey(noEncode, codec = 'bs64') { | ||
let key = randomBytes(secretbox.keyLength) | ||
static generateKey(noEncode, codec = "bs64") { | ||
let key = randomBytes(secretbox.keyLength); | ||
/* istanbul ignore if */ | ||
if (codec === 'bs58') { | ||
key = Buffer.from(key) | ||
if (codec === "bs58") { | ||
key = Buffer.from(key); | ||
} | ||
return noEncode ? key : Crypto[codec].encode(key) | ||
return noEncode ? key : Crypto[codec].encode(key); | ||
} | ||
static isBase58String(str) { | ||
let re = RegExp(`[^${Crypto.base58Alphabet}]+`) | ||
return !re.test(str) | ||
let re = RegExp(`[^${Crypto.base58Alphabet}]+`); | ||
return !re.test(str); | ||
} | ||
static isBase32String(str) { | ||
let re = RegExp(`[^${Crypto.zBase32Alphabet}]+`) | ||
return !re.test(str) | ||
let re = RegExp(`[^${Crypto.zBase32Alphabet}]+`); | ||
return !re.test(str); | ||
} | ||
static isUint8Array(key) { | ||
return typeof key === 'object' && key.constructor === Uint8Array | ||
return typeof key === "object" && key.constructor === Uint8Array; | ||
} | ||
static encrypt(message, key, nonce = Crypto.randomBytes(secretbox.nonceLength), getNonce, returnUint8Array, codec = 'bs64') { | ||
let messageUint8 = Buffer.isBuffer(message) ? new Uint8Array(message) : typeof message === 'string' ? Crypto.utf8ToArray(message) : message | ||
const keyUint8Array = typeof key === 'string' ? Crypto[codec].decode(key) : key | ||
const box = secretbox(messageUint8, nonce, keyUint8Array) | ||
let fullMessage = new Uint8Array(nonce.length + box.length) | ||
fullMessage.set(nonce) | ||
fullMessage.set(box, nonce.length) | ||
static encrypt( | ||
message, | ||
key, | ||
nonce = Crypto.randomBytes(secretbox.nonceLength), | ||
getNonce, | ||
returnUint8Array, | ||
codec = "bs64" | ||
) { | ||
let messageUint8 = Buffer.isBuffer(message) | ||
? new Uint8Array(message) | ||
: typeof message === "string" | ||
? Crypto.utf8ToArray(message) | ||
: message; | ||
const keyUint8Array = | ||
typeof key === "string" ? Crypto[codec].decode(key) : key; | ||
const box = secretbox(messageUint8, nonce, keyUint8Array); | ||
let fullMessage = new Uint8Array(nonce.length + box.length); | ||
fullMessage.set(nonce); | ||
fullMessage.set(box, nonce.length); | ||
/* istanbul ignore if */ | ||
if (codec === 'bs58') { | ||
fullMessage = Buffer.from(fullMessage) | ||
if (codec === "bs58") { | ||
fullMessage = Buffer.from(fullMessage); | ||
} | ||
const encoded = returnUint8Array ? fullMessage : Crypto[codec].encode(fullMessage) | ||
const encoded = returnUint8Array | ||
? fullMessage | ||
: Crypto[codec].encode(fullMessage); | ||
if (getNonce) { | ||
return [nonce, encoded] | ||
return [nonce, encoded]; | ||
} else { | ||
return encoded | ||
return encoded; | ||
} | ||
} | ||
static decrypt(messageWithNonce, key, returnUint8Array, codec = 'bs64') { | ||
const messageWithNonceAsUint8Array = typeof messageWithNonce === 'string' ? Crypto[codec].decode(messageWithNonce) : messageWithNonce | ||
const keyUint8Array = typeof key === 'string' ? Crypto[codec].decode(key) : key | ||
const nonce = messageWithNonceAsUint8Array.slice( | ||
0, | ||
secretbox.nonceLength | ||
) | ||
static decrypt(messageWithNonce, key, returnUint8Array, codec = "bs64") { | ||
const messageWithNonceAsUint8Array = | ||
typeof messageWithNonce === "string" | ||
? Crypto[codec].decode(messageWithNonce) | ||
: messageWithNonce; | ||
const keyUint8Array = | ||
typeof key === "string" ? Crypto[codec].decode(key) : key; | ||
const nonce = messageWithNonceAsUint8Array.slice(0, secretbox.nonceLength); | ||
const message = messageWithNonceAsUint8Array.slice( | ||
secretbox.nonceLength, | ||
messageWithNonceAsUint8Array.length | ||
) | ||
const decrypted = secretbox.open(message, nonce, keyUint8Array) | ||
secretbox.nonceLength, | ||
messageWithNonceAsUint8Array.length | ||
); | ||
const decrypted = secretbox.open(message, nonce, keyUint8Array); | ||
/* istanbul ignore if */ | ||
if (!decrypted) { | ||
throw new Error('Could not decrypt message') | ||
throw new Error("Could not decrypt message"); | ||
} | ||
return returnUint8Array ? decrypted : Crypto.arrayToUtf8(decrypted) | ||
return returnUint8Array ? decrypted : Crypto.arrayToUtf8(decrypted); | ||
} | ||
static getNonceFromMessage(messageWithNonce, codec = 'bs64') { | ||
return Crypto[codec].decode(messageWithNonce).slice(0, secretbox.nonceLength) | ||
static getNonceFromMessage(messageWithNonce, codec = "bs64") { | ||
return Crypto[codec] | ||
.decode(messageWithNonce) | ||
.slice(0, secretbox.nonceLength); | ||
} | ||
static generateBoxKeyPair(hexEncode) { | ||
const pair = box.keyPair() | ||
const pair = box.keyPair(); | ||
if (hexEncode) { | ||
return { | ||
publicKey: Crypto.uint8ArrayToHex(pair.publicKey), | ||
secretKey: Crypto.uint8ArrayToHex(pair.secretKey) | ||
} | ||
secretKey: Crypto.uint8ArrayToHex(pair.secretKey), | ||
}; | ||
} else { | ||
return pair | ||
return pair; | ||
} | ||
@@ -250,6 +264,6 @@ } | ||
static seedFromPassphrase(passphrase) { | ||
if (typeof passphrase === 'string' && passphrase.length > 0) { | ||
return Uint8Array.from(Crypto.SHA3(passphrase)) | ||
if (typeof passphrase === "string" && passphrase.length > 0) { | ||
return Uint8Array.from(Crypto.SHA3(passphrase)); | ||
} else { | ||
throw new Error('Not a valid string') | ||
throw new Error("Not a valid string"); | ||
} | ||
@@ -259,9 +273,9 @@ } | ||
static generateSignatureKeyPair(seed) { | ||
let pair | ||
let pair; | ||
if (seed) { | ||
pair = sign.keyPair.fromSeed(seed) | ||
pair = sign.keyPair.fromSeed(seed); | ||
} else { | ||
pair = sign.keyPair() | ||
pair = sign.keyPair(); | ||
} | ||
return pair | ||
return pair; | ||
} | ||
@@ -271,5 +285,5 @@ | ||
if (key instanceof Uint8Array) { | ||
return key.length === 32 | ||
return key.length === 32; | ||
} else { | ||
return false | ||
return false; | ||
} | ||
@@ -280,5 +294,5 @@ } | ||
if (key instanceof Uint8Array) { | ||
return key.length === 64 | ||
return key.length === 64; | ||
} else { | ||
return false | ||
return false; | ||
} | ||
@@ -288,66 +302,85 @@ } | ||
static getSharedSecret(theirPublicKey, mySecretKey) { | ||
return box.before(theirPublicKey, mySecretKey) | ||
return box.before(theirPublicKey, mySecretKey); | ||
} | ||
static boxEncrypt(secretOrSharedKey, message, key, nonce = randomBytes(box.nonceLength), getNonce, codec = 'bs64') { | ||
const messageUint8 = Crypto.utf8ToArray(message) | ||
const keyUint8Array = typeof key === 'string' ? Crypto.hexToUint8Array(key) : key | ||
const secretKeyUint8Array = typeof secretOrSharedKey === 'string' ? Crypto.hexToUint8Array(secretOrSharedKey) : secretOrSharedKey | ||
static boxEncrypt( | ||
secretOrSharedKey, | ||
message, | ||
key, | ||
nonce = randomBytes(box.nonceLength), | ||
getNonce, | ||
codec = "bs64" | ||
) { | ||
const messageUint8 = Crypto.utf8ToArray(message); | ||
const keyUint8Array = | ||
typeof key === "string" ? Crypto.hexToUint8Array(key) : key; | ||
const secretKeyUint8Array = | ||
typeof secretOrSharedKey === "string" | ||
? Crypto.hexToUint8Array(secretOrSharedKey) | ||
: secretOrSharedKey; | ||
const encrypted = keyUint8Array | ||
? box(messageUint8, nonce, keyUint8Array, secretKeyUint8Array) | ||
: box.after(messageUint8, nonce, secretKeyUint8Array) | ||
let fullMessage = new Uint8Array(nonce.length + encrypted.length) | ||
fullMessage.set(nonce) | ||
fullMessage.set(encrypted, nonce.length) | ||
? box(messageUint8, nonce, keyUint8Array, secretKeyUint8Array) | ||
: box.after(messageUint8, nonce, secretKeyUint8Array); | ||
let fullMessage = new Uint8Array(nonce.length + encrypted.length); | ||
fullMessage.set(nonce); | ||
fullMessage.set(encrypted, nonce.length); | ||
/* istanbul ignore if */ | ||
if (codec === 'bs58') { | ||
fullMessage = Buffer.from(fullMessage) | ||
if (codec === "bs58") { | ||
fullMessage = Buffer.from(fullMessage); | ||
} | ||
const encoded = Crypto[codec].encode(fullMessage) | ||
const encoded = Crypto[codec].encode(fullMessage); | ||
if (getNonce) { | ||
return [nonce, encoded] | ||
return [nonce, encoded]; | ||
} else { | ||
return encoded | ||
return encoded; | ||
} | ||
} | ||
static boxDecrypt(secretOrSharedKey, messageWithNonce, key, codec = 'bs64') { | ||
const messageWithNonceAsUint8Array = Crypto[codec].decode(messageWithNonce) | ||
const nonce = messageWithNonceAsUint8Array.slice(0, box.nonceLength) | ||
static boxDecrypt(secretOrSharedKey, messageWithNonce, key, codec = "bs64") { | ||
const messageWithNonceAsUint8Array = Crypto[codec].decode(messageWithNonce); | ||
const nonce = messageWithNonceAsUint8Array.slice(0, box.nonceLength); | ||
const message = messageWithNonceAsUint8Array.slice( | ||
box.nonceLength, | ||
messageWithNonce.length | ||
) | ||
const keyUint8Array = typeof key === 'string' ? Crypto.hexToUint8Array(key) : key | ||
const secretKeyUint8Array = typeof secretOrSharedKey === 'string' ? Crypto.hexToUint8Array(secretOrSharedKey) : secretOrSharedKey | ||
box.nonceLength, | ||
messageWithNonce.length | ||
); | ||
const keyUint8Array = | ||
typeof key === "string" ? Crypto.hexToUint8Array(key) : key; | ||
const secretKeyUint8Array = | ||
typeof secretOrSharedKey === "string" | ||
? Crypto.hexToUint8Array(secretOrSharedKey) | ||
: secretOrSharedKey; | ||
const decrypted = keyUint8Array | ||
? box.open(message, nonce, keyUint8Array, secretKeyUint8Array) | ||
: box.open.after(message, nonce, secretKeyUint8Array) | ||
? box.open(message, nonce, keyUint8Array, secretKeyUint8Array) | ||
: box.open.after(message, nonce, secretKeyUint8Array); | ||
/* istanbul ignore if */ | ||
if (!decrypted) { | ||
throw new Error('Could not decrypt message') | ||
throw new Error("Could not decrypt message"); | ||
} | ||
return Crypto.arrayToUtf8(decrypted) | ||
return Crypto.arrayToUtf8(decrypted); | ||
} | ||
static getSignature(message, secretKey, codec = 'bs64') { | ||
let signature = sign.detached(Crypto.utf8ToArray(message), secretKey) | ||
static getSignature(message, secretKey, codec = "bs64") { | ||
let signature = sign.detached(Crypto.utf8ToArray(message), secretKey); | ||
/* istanbul ignore if */ | ||
if (codec === 'bs58') { | ||
signature = Buffer.from(signature) | ||
if (codec === "bs58") { | ||
signature = Buffer.from(signature); | ||
} | ||
return Crypto[codec].encode(signature) | ||
return Crypto[codec].encode(signature); | ||
} | ||
static verifySignature(message, signature, publicKey, codec = 'bs64') { | ||
let verified = sign.detached.verify(Crypto.utf8ToArray(message), Crypto[codec].decode(signature), publicKey) | ||
return verified | ||
static verifySignature(message, signature, publicKey, codec = "bs64") { | ||
let verified = sign.detached.verify( | ||
Crypto.utf8ToArray(message), | ||
Crypto[codec].decode(signature), | ||
publicKey | ||
); | ||
return verified; | ||
} | ||
static utf8ToArray(bytes) { | ||
return utf8Encoder.encode(bytes) | ||
return utf8Encoder.encode(bytes); | ||
} | ||
static arrayToUtf8(bytes) { | ||
return utf8Decoder.decode(bytes) | ||
return utf8Decoder.decode(bytes); | ||
} | ||
@@ -357,82 +390,88 @@ | ||
if (!Crypto.isUint8Array(secretBytes)) { | ||
secretBytes = Crypto.utf8ToArray(secretBytes) | ||
secretBytes = Crypto.utf8ToArray(secretBytes); | ||
} | ||
return shamir.split(Crypto.randomBytes, parts, quorum, secretBytes) | ||
return shamir.split(Crypto.randomBytes, parts, quorum, secretBytes); | ||
} | ||
static joinSecret(parts, asUint8Array) { | ||
const recovered = shamir.join(parts) | ||
return asUint8Array ? recovered : Buffer.from(recovered).toString('utf8') | ||
const recovered = shamir.join(parts); | ||
return asUint8Array ? recovered : Buffer.from(recovered).toString("utf8"); | ||
} | ||
static getSignPublicKeyFromSecretPublicKey(publicKey) { | ||
return Crypto.bs64.decode(publicKey.split('$')[1]) | ||
return Crypto.bs64.decode(publicKey.split("$")[1]); | ||
} | ||
static getBoxPublicKeyFromSecretPublicKey (publicKey) { | ||
return Crypto.bs64.decode(publicKey.split('$')[0]) | ||
static getBoxPublicKeyFromSecretPublicKey(publicKey) { | ||
return Crypto.bs64.decode(publicKey.split("$")[0]); | ||
} | ||
static isValidSecrezPublicKey (pk) { | ||
if (typeof pk === 'string') { | ||
static isValidSecrezPublicKey(pk) { | ||
if (typeof pk === "string") { | ||
try { | ||
const [boxPublicKey, signPublicKey] = pk.split('$').map(e => { | ||
e = Crypto.bs64.decode(e) | ||
const [boxPublicKey, signPublicKey] = pk.split("$").map((e) => { | ||
e = Crypto.bs64.decode(e); | ||
if (Crypto.isValidPublicKey(e)) { | ||
return e | ||
return e; | ||
} | ||
}) | ||
}); | ||
if (boxPublicKey && signPublicKey) { | ||
return true | ||
return true; | ||
} | ||
} catch (e) { | ||
} | ||
} catch (e) {} | ||
} | ||
return false | ||
return false; | ||
} | ||
static getTimestampWithMicroseconds () { | ||
let now = Date.now() | ||
let tmp = [Math.floor(now / 1000), μs.parse(μs.now()).microseconds.toString()] | ||
tmp[1] = parseInt(now.toString().substr(-3) + '0'.repeat(3 - tmp[1].length) + tmp[1]) | ||
return tmp | ||
static getTimestampWithMicroseconds() { | ||
let now = Date.now(); | ||
let tmp = [ | ||
Math.floor(now / 1000), | ||
μs.parse(μs.now()).microseconds.toString(), | ||
]; | ||
tmp[1] = parseInt( | ||
now.toString().substr(-3) + "0".repeat(3 - tmp[1].length) + tmp[1] | ||
); | ||
return tmp; | ||
} | ||
static fromTsToDate(ts) { | ||
let [seconds, microseconds] = ts.split('.') | ||
let milliseconds = microseconds.substring(0, 3) | ||
let timestamp = parseInt(seconds) * 1000 + parseInt(milliseconds) | ||
return [(new Date(timestamp)).toISOString(), parseInt(microseconds.substring(3))] | ||
let [seconds, microseconds] = ts.split("."); | ||
let milliseconds = microseconds.substring(0, 3); | ||
let timestamp = parseInt(seconds) * 1000 + parseInt(milliseconds); | ||
return [ | ||
new Date(timestamp).toISOString(), | ||
parseInt(microseconds.substring(3)), | ||
]; | ||
} | ||
static fromBase64ToFsSafeBase64 (base64 ) { | ||
return base64.replace(/[+/=]/g, (m) => SAFE_ENC[m]) | ||
static fromBase64ToFsSafeBase64(base64) { | ||
return base64.replace(/[+/=]/g, (m) => SAFE_ENC[m]); | ||
} | ||
static fromFsSafeBase64ToBase64 (safeBase64 ) { | ||
for (let i = 1; i < safeBase64.length % 4; i++) safeBase64 += '=' | ||
return safeBase64.replace(/[-_]/g, (m) => SAFE_DEC[m]) | ||
static fromFsSafeBase64ToBase64(safeBase64) { | ||
for (let i = 1; i < safeBase64.length % 4; i++) safeBase64 += "="; | ||
return safeBase64.replace(/[-_]/g, (m) => SAFE_DEC[m]); | ||
} | ||
} | ||
Crypto.base58Alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | ||
Crypto.bs58 = basex(Crypto.base58Alphabet) | ||
Crypto.base58Alphabet = | ||
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; | ||
Crypto.bs58 = basex(Crypto.base58Alphabet); | ||
Crypto.zBase32Alphabet = 'ybndrfg8ejkmcpqxot1uwisza345h769' | ||
Crypto.bs32 = basex(Crypto.zBase32Alphabet) | ||
Crypto.zBase32Alphabet = "ybndrfg8ejkmcpqxot1uwisza345h769"; | ||
Crypto.bs32 = basex(Crypto.zBase32Alphabet); | ||
Crypto.bs64 = { | ||
encode: (data) => { | ||
return bytesToBase64(data); | ||
}, | ||
encode: data => { | ||
return bytesToBase64(data) | ||
decode: (data) => { | ||
return base64ToBytes(data); | ||
}, | ||
}; | ||
decode: data => { | ||
return base64ToBytes(data) | ||
} | ||
} | ||
Crypto.randomBytes = randomBytes; | ||
Crypto.randomBytes = randomBytes | ||
module.exports = Crypto | ||
module.exports = Crypto; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18766
5
405
53
5
1