Compare commits

...

4 Commits

Author SHA1 Message Date
overtorment
90dc31dbac doc
Some checks failed
CI / Test (20.x) (push) Has been cancelled
CI / Test (22.x) (push) Has been cancelled
CI / Lint (push) Has been cancelled
2025-07-07 17:56:54 +01:00
overtorment
412941f009 doc 2025-07-07 17:37:57 +01:00
overtorment
adf136e51d doc 2025-07-07 17:28:27 +01:00
Overtorment
1d8c52f76c
Noblify create sign (#4)
* noblify create sign (rsa)

* lint

* noblify create sign (ec)

* noblify create sign (ec)

* lint

* lint

* noblify publicEncrypt

* noblify publicEncrypt

* noblify publicEncrypt

* noblify publicEncrypt
2025-07-07 17:22:57 +01:00
23 changed files with 888 additions and 326 deletions

View File

@ -1,23 +1,43 @@
{
"root": true,
"extends": "@ljharb",
"extends": [
"eslint:recommended"
],
"env": {
"browser": true,
"node": true,
"es2020": true
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"rules": {
"func-style": "off",
"multiline-comment-style": "off",
"sort-keys": "off",
"no-magic-numbers": "off",
"max-params": "off"
"max-params": "off",
"indent": ["error", "tab"],
"quotes": ["error", "single"],
"semi": ["error", "always"],
"no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"no-console": "warn",
"prefer-const": "error",
"no-var": "error"
},
"ignorePatterns": [
"example/bundle.js",
"coverage/**",
],
"overrides": [
{
"files": "example/**",
"files": "test/**",
"rules": {
"no-console": "off",
},

View File

@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
node-version: [18.x, 20.x, 22.x]
node-version: [20.x, 22.x]
steps:
- name: Checkout code

View File

@ -1,18 +0,0 @@
sudo: false
language: node_js
matrix:
include:
env: TEST_SUITE=browser BROWSER_NAME=ie BROWSER_VERSION="10..latest"
- node_js: '4'
env: TEST_SUITE=browser BROWSER_NAME=chrome BROWSER_VERSION="-2..beta"
- node_js: '4'
env: TEST_SUITE=browser BROWSER_NAME=firefox BROWSER_VERSION="-2..latest"
- node_js: '4'
env: TEST_SUITE=browser BROWSER_NAME=safari BROWSER_VERSION="7..latest"
- node_js: '4'
env: TEST_SUITE=browser BROWSER_NAME=android BROWSER_VERSION="5.0..latest"
script: "npm run-script $TEST_SUITE"
env:
global:
- secure: YHNUDQmx/WiW3gmDcRCfb6KLDeio7Mr5tqPY2kHPdZlBSytsQjNk75ytM4U6Cu8Uk8iEIoj/aFlxiVMpJNA8J4QSUyW/YkbVaIz0+1oywoV0Ht8aRBfZ1jvXfX6789+1Q9c4xaMkYYbJpXSh9JcirsiwmqWd4+IDd7hcESodsDQ=
- secure: Nhj5yejKZxUbtHGZta+GjYWqXGaOZB7ainTkOuGcpXM+OwwjeDpYlTBrwS90Q7hqens7KXVzQM09aDbadpsDCsOo1nyaEigMtomAorZ1UC1CpEoVz1ZuikF9bEhb+/7M9pzuL1fX+Ke9Dx4mPPeb8sf/2SrAu1RqXkSwZV/duAc=

View File

@ -1,10 +1,18 @@
# noble-crypto
`crypto-browserify` fork with all dependencies swapped for more modern `@noble` suite
## Incompatibilities
* `randomBytes` is removed and will throw an exception. Bring your own CSPRNG for your runtime
* `randomfill`
* `createSign()` & `createVerify()` are carved out (could not get noble to work with it)
* `randomfill` --//--
* for `createECDH`, curves `secp224r1` & `prime192v1` are carved out (not supported in noble)
## Todo
* throw away `safe-buffer` in favor of Uint8Array
* figure out what to do with `parse-asn1` dep
# crypto-browserify <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
A port of node's `crypto` module to the browser.

View File

@ -11,7 +11,7 @@ exports.createHash = exports.Hash = require('./noble-hash-wrapper');
// eslint-disable-next-line no-multi-assign
exports.createHmac = exports.Hmac = require('./noble-hmac-wrapper');
var hashes = [
const hashes = [
'sha1',
'sha224',
'sha256',
@ -25,11 +25,11 @@ exports.getHashes = function () {
return hashes;
};
var p = require('./noble-pbkdf2-wrapper');
const p = require('./noble-pbkdf2-wrapper');
exports.pbkdf2 = p.pbkdf2;
exports.pbkdf2Sync = p.pbkdf2Sync;
var aes = require('./noble-cipher-wrapper');
const aes = require('./noble-cipher-wrapper');
exports.Cipher = aes.Cipher;
exports.createCipher = aes.createCipher;
@ -42,7 +42,7 @@ exports.createDecipheriv = aes.createDecipheriv;
exports.getCiphers = aes.getCiphers;
exports.listCiphers = aes.listCiphers;
var dh = require('./micro-dh-wrapper');
const dh = require('./micro-dh-wrapper');
exports.DiffieHellmanGroup = dh.DiffieHellmanGroup;
exports.createDiffieHellmanGroup = dh.createDiffieHellmanGroup;
@ -50,30 +50,21 @@ exports.getDiffieHellman = dh.getDiffieHellman;
exports.createDiffieHellman = dh.createDiffieHellman;
exports.DiffieHellman = dh.DiffieHellman;
exports.createSign = function () {
throw new Error('Not implemented');
};
const signVerify = require('./noble-sign-wrapper');
exports.Sign = function () {
throw new Error('Not implemented');
};
exports.createVerify = function () {
throw new Error('Not implemented');
};
exports.Verify = function () {
throw new Error('Not implemented');
};
exports.createSign = signVerify.createSign;
exports.Sign = signVerify.Sign;
exports.createVerify = signVerify.createVerify;
exports.Verify = signVerify.Verify;
exports.createECDH = require('./noble-ecdh-wrapper');
var publicEncrypt = require('public-encrypt');
const rsa = require('./noble-rsa-wrapper');
exports.publicEncrypt = publicEncrypt.publicEncrypt;
exports.privateEncrypt = publicEncrypt.privateEncrypt;
exports.publicDecrypt = publicEncrypt.publicDecrypt;
exports.privateDecrypt = publicEncrypt.privateDecrypt;
exports.publicEncrypt = rsa.publicEncrypt;
exports.privateEncrypt = rsa.privateEncrypt;
exports.publicDecrypt = rsa.publicDecrypt;
exports.privateDecrypt = rsa.privateDecrypt;
// the least I can do is make error messages for the rest of the node.js/crypto api.
// [

View File

@ -1,10 +1,8 @@
'use strict';
var DHGroupsModule = require('micro-rsa-dsa-dh/dh.js');
var DHGroups = DHGroupsModule.DHGroups;
var Buffer = require('safe-buffer').Buffer;
/* global window */
const DHGroupsModule = require('micro-rsa-dsa-dh/dh.js');
const DHGroups = DHGroupsModule.DHGroups;
const Buffer = require('safe-buffer').Buffer;
// Cross-platform random bytes function
function getRandomBytes(buffer) {
@ -21,14 +19,14 @@ function getRandomBytes(buffer) {
// Convert bigint to Buffer with proper padding
function bigintToBuffer(bigint, length) {
var hex = bigint.toString(16);
const hex = bigint.toString(16);
// Ensure even length by padding with leading zero if needed
var paddedHex = hex.length % 2 === 0 ? hex : '0' + hex;
var buffer = Buffer.from(paddedHex, 'hex');
const paddedHex = hex.length % 2 === 0 ? hex : '0' + hex;
const buffer = Buffer.from(paddedHex, 'hex');
if (length && buffer.length < length) {
// Pad with leading zeros to reach desired length
var pad = Buffer.alloc(length - buffer.length, 0);
const pad = Buffer.alloc(length - buffer.length, 0);
return Buffer.concat([pad, buffer]);
}
@ -37,9 +35,9 @@ function bigintToBuffer(bigint, length) {
// Helper function for BigInt power operation
function bigintPow(base, exponent) {
var result = BigInt(1);
var localBase = BigInt(base);
var localExponent = BigInt(exponent);
let result = BigInt(1);
let localBase = BigInt(base);
let localExponent = BigInt(exponent);
while (localExponent > 0) {
if (localExponent % BigInt(2) === BigInt(1)) {
result = result * localBase;
@ -57,9 +55,9 @@ function bufferToBigint(buffer) {
// Modular exponentiation
function modPow(base, exp, mod) {
var result = BigInt(1);
var localBase = base % mod;
var localExp = exp;
let result = BigInt(1);
let localBase = base % mod;
let localExp = exp;
while (localExp > BigInt(0)) {
if (localExp % BigInt(2) === BigInt(1)) {
result = (result * localBase) % mod;
@ -72,12 +70,12 @@ function modPow(base, exp, mod) {
// Miller-Rabin primality test helper
function millerRabinTest(n, a, d, r) {
var x = modPow(a, d, n);
let x = modPow(a, d, n);
if (x === BigInt(1) || x === n - BigInt(1)) {
return true;
}
for (var i = 0; i < r - 1; i++) {
for (let i = 0; i < r - 1; i++) {
x = modPow(x, BigInt(2), n);
if (x === n - BigInt(1)) {
return true;
@ -100,15 +98,15 @@ function isProbablePrime(n) {
}
// Miller-Rabin primality test (simplified)
var d = n - BigInt(1);
var r = 0;
let d = n - BigInt(1);
let r = 0;
while (d % BigInt(2) === BigInt(0)) {
d /= BigInt(2);
r = r + 1;
}
// Test with a few witnesses
var witnesses = [
const witnesses = [
BigInt(2),
BigInt(3),
BigInt(5),
@ -122,8 +120,8 @@ function isProbablePrime(n) {
BigInt(31),
BigInt(37)
];
for (var i = 0; i < witnesses.length; i++) {
var a = witnesses[i];
for (let i = 0; i < witnesses.length; i++) {
const a = witnesses[i];
if (a < n) {
if (!millerRabinTest(n, a, d, r)) {
return false;
@ -138,13 +136,13 @@ function isProbablePrime(n) {
function generatePrime(bitLength) {
// Simple prime generation (not cryptographically secure, but sufficient for this demo)
// In production, you'd use a proper prime generation algorithm
var min = bigintPow(BigInt(2), BigInt(bitLength - 1));
var max = bigintPow(BigInt(2), BigInt(bitLength)) - BigInt(1);
const min = bigintPow(BigInt(2), BigInt(bitLength - 1));
const max = bigintPow(BigInt(2), BigInt(bitLength)) - BigInt(1);
// Generate random number in range
var candidate;
let candidate;
do {
var randomBytes = Buffer.allocUnsafe(Math.ceil(bitLength / 8));
const randomBytes = Buffer.allocUnsafe(Math.ceil(bitLength / 8));
getRandomBytes(randomBytes);
candidate = bufferToBigint(randomBytes);
candidate = (candidate % (max - min + BigInt(1))) + min;
@ -163,9 +161,9 @@ function CustomDH(prime, generator) {
CustomDH.prototype.randomPrivateKey = function () {
// Generate random private key in range [1, prime-1]
var keyBytes = Buffer.allocUnsafe(Math.ceil(this.prime.toString(16).length / 2));
const keyBytes = Buffer.allocUnsafe(Math.ceil(this.prime.toString(16).length / 2));
getRandomBytes(keyBytes);
var key = bufferToBigint(keyBytes);
let key = bufferToBigint(keyBytes);
key = (key % (this.prime - BigInt(1))) + BigInt(1);
return key;
};
@ -175,7 +173,7 @@ CustomDH.prototype.getPublicKey = function (privateKey) {
};
CustomDH.prototype.getSharedSecret = function (privateKey, otherPublicKey) {
var otherPubKeyBigint = typeof otherPublicKey === 'bigint' ? otherPublicKey : bufferToBigint(otherPublicKey);
const otherPubKeyBigint = typeof otherPublicKey === 'bigint' ? otherPublicKey : bufferToBigint(otherPublicKey);
return modPow(otherPubKeyBigint, privateKey, this.prime);
};
@ -190,16 +188,16 @@ function DiffieHellman(prime, generator) {
this.primeLength = Math.ceil(DHGroups[prime].p.toString(16).length / 2);
} else if (typeof prime === 'number') {
// Key length specified - generate prime of that length
var generatedPrime = generatePrime(prime);
var generatedGenerator = BigInt(2); // Use 2 as generator (safe for most primes)
const generatedPrime = generatePrime(prime);
const generatedGenerator = BigInt(2); // Use 2 as generator (safe for most primes)
this.customDH = new CustomDH(generatedPrime, generatedGenerator);
this.customPrime = generatedPrime;
this.customGenerator = generatedGenerator;
this.primeLength = Math.ceil(generatedPrime.toString(16).length / 2);
} else if (Buffer.isBuffer(prime)) {
// Custom prime provided
var primeBigint = bufferToBigint(prime);
var genBigint = BigInt(2); // Default generator
const primeBigint = bufferToBigint(prime);
let genBigint = BigInt(2); // Default generator
if (Buffer.isBuffer(generator)) {
genBigint = bufferToBigint(generator);
} else if (typeof generator === 'number') {
@ -234,7 +232,7 @@ DiffieHellman.prototype.computeSecret = function (otherPublicKey, inputEncoding,
}
// Using custom DH implementation for all cases
var otherKeyInput;
let otherKeyInput;
if (Buffer.isBuffer(otherPublicKey)) {
otherKeyInput = otherPublicKey;
} else if (typeof otherPublicKey === 'string') {
@ -245,8 +243,8 @@ DiffieHellman.prototype.computeSecret = function (otherPublicKey, inputEncoding,
throw new Error('Invalid public key type for computeSecret');
}
var sharedSecret = this.customDH.getSharedSecret(this.privateKey, otherKeyInput);
var result = bigintToBuffer(sharedSecret, this.primeLength);
const sharedSecret = this.customDH.getSharedSecret(this.privateKey, otherKeyInput);
const result = bigintToBuffer(sharedSecret, this.primeLength);
if (outputEncoding) {
return result.toString(outputEncoding);
@ -259,7 +257,7 @@ DiffieHellman.prototype.getPrime = function (encoding) {
throw new Error('No prime available');
}
var result = bigintToBuffer(this.customPrime);
const result = bigintToBuffer(this.customPrime);
if (encoding) {
return result.toString(encoding);
}
@ -271,7 +269,7 @@ DiffieHellman.prototype.getGenerator = function (encoding) {
throw new Error('No generator available');
}
var result = bigintToBuffer(this.customGenerator);
const result = bigintToBuffer(this.customGenerator);
if (encoding) {
return result.toString(encoding);
}
@ -283,7 +281,7 @@ DiffieHellman.prototype.getPublicKey = function (encoding) {
throw new Error('Keys not generated. Call generateKeys() first.');
}
var result = bigintToBuffer(this.publicKey, this.primeLength);
const result = bigintToBuffer(this.publicKey, this.primeLength);
if (encoding) {
return result.toString(encoding);
@ -296,7 +294,7 @@ DiffieHellman.prototype.getPrivateKey = function (encoding) {
throw new Error('Keys not generated. Call generateKeys() first.');
}
var result = bigintToBuffer(this.privateKey);
const result = bigintToBuffer(this.privateKey);
if (encoding) {
return result.toString(encoding);
}

View File

@ -1,13 +1,11 @@
'use strict';
/* global Uint8Array */
var aes = require('@noble/ciphers/aes');
var utils = require('@noble/ciphers/utils');
var sha256 = require('@noble/hashes/sha256');
const aes = require('@noble/ciphers/aes');
const utils = require('@noble/ciphers/utils');
const sha256 = require('@noble/hashes/sha256');
// Map cipher names to noble AES functions
var cipherMap = {
const cipherMap = {
// AES modes
'aes-128-ecb': { fn: aes.ecb, keySize: 16 },
'aes-192-ecb': { fn: aes.ecb, keySize: 24 },
@ -63,18 +61,18 @@ function toBuffer(data) {
// Helper function to derive key from password
function deriveKey(password, salt, keySize) {
var passwordBytes = toUint8Array(password);
var saltBytes = toUint8Array(salt);
const passwordBytes = toUint8Array(password);
const saltBytes = toUint8Array(salt);
// Simple key derivation - in real usage you'd want PBKDF2
var hash = sha256.sha256(utils.concatBytes(passwordBytes, saltBytes));
const hash = sha256.sha256(utils.concatBytes(passwordBytes, saltBytes));
return hash.slice(0, keySize);
}
// Helper function to generate IV
function generateIV(ivSize) {
var iv = new Uint8Array(ivSize);
for (var i = 0; i < ivSize; i++) {
const iv = new Uint8Array(ivSize);
for (let i = 0; i < ivSize; i++) {
iv[i] = Math.floor(Math.random() * 256);
}
return iv;
@ -119,20 +117,20 @@ function Cipher(algorithm, password) {
}
Cipher.prototype.update = function (data) {
var dataBytes = toUint8Array(data);
const dataBytes = toUint8Array(data);
this.encrypted.push(dataBytes);
return Buffer.alloc(0); // No output until final
};
Cipher.prototype['final'] = function () {
var input = Buffer.concat(this.encrypted.map(toBuffer));
var result;
var output;
const input = Buffer.concat(this.encrypted.map(toBuffer));
let result;
let output;
if (this.isGcm) {
var encResult = this.cipher.encrypt(input);
var tagLen = 16;
var ciphertext = encResult.slice(0, -tagLen);
var tag = encResult.slice(-tagLen);
const encResult = this.cipher.encrypt(input);
const tagLen = 16;
const ciphertext = encResult.slice(0, -tagLen);
const tag = encResult.slice(-tagLen);
this.authTag = Buffer.from(tag);
output = Buffer.concat([
Buffer.from(this.salt), Buffer.from(this.iv), Buffer.from(ciphertext)
@ -182,8 +180,8 @@ function Cipheriv(algorithm, key, iv) {
}
Cipheriv.prototype.update = function (data) {
var dataBytes = toUint8Array(data);
var result;
const dataBytes = toUint8Array(data);
let result;
if (this.isEcb) {
result = this.cipher.encrypt(dataBytes);
@ -196,7 +194,7 @@ Cipheriv.prototype.update = function (data) {
};
Cipheriv.prototype['final'] = function () {
var result = Buffer.concat(this.encrypted.map(toBuffer));
const result = Buffer.concat(this.encrypted.map(toBuffer));
if (this.isGcm) {
this.authTag = toBuffer(this.cipher.tag);
@ -231,40 +229,40 @@ function Decipher(algorithm, password) {
}
Decipher.prototype.update = function (data) {
var dataBytes = toUint8Array(data);
const dataBytes = toUint8Array(data);
this.decrypted.push(dataBytes);
return Buffer.alloc(0); // No output until final
};
Decipher.prototype['final'] = function () {
var input = Buffer.concat(this.decrypted.map(toBuffer));
const input = Buffer.concat(this.decrypted.map(toBuffer));
// For first call, extract salt and IV, derive key, and initialize cipher
if (!this.cipher) {
this.salt = input.slice(0, 16);
if (this.isGcm) {
this.iv = input.slice(16, 28);
this.key = deriveKey(this.password, this.salt, this.keySize);
var gcmCipherData = input.slice(28);
const gcmCipherData = input.slice(28);
if (!this.authTag) {
throw new Error('GCM: authTag must be set before final');
}
this.cipher = this.cipherInfo.fn(this.key, this.iv);
var gcmCiphertextWithTag = Buffer.concat([gcmCipherData, this.authTag]);
var gcmResult = this.cipher.decrypt(gcmCiphertextWithTag);
const gcmCiphertextWithTag = Buffer.concat([gcmCipherData, this.authTag]);
const gcmResult = this.cipher.decrypt(gcmCiphertextWithTag);
return toBuffer(gcmResult);
}
this.iv = this.isEcb ? null : input.slice(16, 32);
this.key = deriveKey(this.password, this.salt, this.keySize);
var blockCipherData = input.slice(this.isEcb ? 16 : 32);
const blockCipherData = input.slice(this.isEcb ? 16 : 32);
if (this.isEcb) {
this.cipher = this.cipherInfo.fn(this.key);
} else {
this.cipher = this.cipherInfo.fn(this.key, this.iv);
}
var blockResult = this.cipher.decrypt(blockCipherData);
const blockResult = this.cipher.decrypt(blockCipherData);
return toBuffer(blockResult);
}
var result = this.cipher.decrypt(input);
const result = this.cipher.decrypt(input);
return toBuffer(result);
};
@ -301,14 +299,14 @@ function Decipheriv(algorithm, key, iv) {
}
Decipheriv.prototype.update = function (data) {
var dataBytes = toUint8Array(data);
var result = this.cipher.decrypt(dataBytes);
const dataBytes = toUint8Array(data);
const result = this.cipher.decrypt(dataBytes);
this.decrypted.push(result);
return toBuffer(result);
};
Decipheriv.prototype['final'] = function () {
var result = Buffer.concat(this.decrypted.map(toBuffer));
const result = Buffer.concat(this.decrypted.map(toBuffer));
return result;
};

View File

@ -1,11 +1,11 @@
'use strict';
var secp256k1 = require('@noble/curves/secp256k1');
var nist = require('@noble/curves/nist');
var Buffer = require('safe-buffer').Buffer;
const secp256k1 = require('@noble/curves/secp256k1');
const nist = require('@noble/curves/nist');
const Buffer = require('safe-buffer').Buffer;
// Curve name mapping
var curveMap = {
const curveMap = {
secp256k1: secp256k1.secp256k1,
secp224r1: null, // Not available in noble
prime256v1: nist.p256,
@ -35,7 +35,7 @@ ECDH.prototype.getPrivateKey = function (encoding) {
throw new Error('Private key not set');
}
var key = Buffer.from(this.privateKey);
const key = Buffer.from(this.privateKey);
return encoding === 'hex' ? key.toString('hex') : key;
};
@ -49,7 +49,7 @@ ECDH.prototype.getPublicKey = function (encoding, format) {
throw new Error('Unsupported format: ' + format);
}
var key;
let key;
if (format === 'compressed') {
key = Buffer.from(this.curve.getPublicKey(this.privateKey, true));
} else {
@ -61,7 +61,7 @@ ECDH.prototype.getPublicKey = function (encoding, format) {
};
ECDH.prototype.setPrivateKey = function (privateKey) {
var key;
let key;
if (typeof privateKey === 'string') {
key = Buffer.from(privateKey, 'hex');
} else {
@ -69,7 +69,7 @@ ECDH.prototype.setPrivateKey = function (privateKey) {
}
// Validate private key
var expectedLength = this.curve.CURVE.n.toString(16).length / 2;
const expectedLength = this.curve.CURVE.n.toString(16).length / 2;
if (key.length !== expectedLength) {
throw new Error('Invalid private key length: expected ' + expectedLength + ', got ' + key.length);
}
@ -80,7 +80,7 @@ ECDH.prototype.setPrivateKey = function (privateKey) {
};
ECDH.prototype.setPublicKey = function (publicKey) {
var key;
let key;
if (typeof publicKey === 'string') {
key = Buffer.from(publicKey, 'hex');
} else {
@ -103,7 +103,7 @@ ECDH.prototype.computeSecret = function (otherPublicKey) {
throw new Error('Private key not set');
}
var otherKey;
let otherKey;
if (typeof otherPublicKey === 'string') {
otherKey = Buffer.from(otherPublicKey, 'hex');
} else {
@ -111,7 +111,7 @@ ECDH.prototype.computeSecret = function (otherPublicKey) {
}
try {
var sharedSecret = this.curve.getSharedSecret(this.privateKey, otherKey);
const sharedSecret = this.curve.getSharedSecret(this.privateKey, otherKey);
// Remove the prefix (first byte) from the shared secret
return Buffer.from(sharedSecret.slice(1));
} catch (e) {

View File

@ -1,12 +1,10 @@
'use strict';
/* global Uint8Array */
var sha2 = require('@noble/hashes/sha2');
var legacy = require('@noble/hashes/legacy');
const sha2 = require('@noble/hashes/sha2');
const legacy = require('@noble/hashes/legacy');
// Map algorithm names to noble hash functions
var hashFunctions = {
const hashFunctions = {
sha1: legacy.sha1,
sha224: sha2.sha224,
sha256: sha2.sha256,
@ -32,10 +30,10 @@ function toUint8Array(data) {
// Helper function to convert Uint8Array to hex string
function toHex(bytes) {
var result = '';
var hexBase = 16;
var padLength = 2;
for (var i = 0; i < bytes.length; i++) {
let result = '';
const hexBase = 16;
const padLength = 2;
for (let i = 0; i < bytes.length; i++) {
result += bytes[i].toString(hexBase).padStart(padLength, '0');
}
return result;
@ -61,7 +59,7 @@ Hash.prototype.update = function (data, encoding) {
throw new Error('Digest already called');
}
var uint8Data;
let uint8Data;
if (encoding) {
// Handle encoding parameter
if (encoding === 'hex') {
@ -85,20 +83,20 @@ Hash.prototype.digest = function (encoding) {
}
// Concatenate all data
var totalLength = 0;
for (var i = 0; i < this.data.length; i++) {
let totalLength = 0;
for (let i = 0; i < this.data.length; i++) {
totalLength += this.data[i].length;
}
var concatenated = new Uint8Array(totalLength);
var offset = 0;
for (var j = 0; j < this.data.length; j++) {
var chunk = this.data[j];
const concatenated = new Uint8Array(totalLength);
let offset = 0;
for (let j = 0; j < this.data.length; j++) {
const chunk = this.data[j];
concatenated.set(chunk, offset);
offset += chunk.length;
}
// Calculate hash
var result = this.hashFunction(concatenated);
const result = this.hashFunction(concatenated);
this.finalized = true;
// Return based on encoding
@ -118,9 +116,9 @@ Hash.prototype.copy = function () {
if (this.finalized) {
throw new Error('Cannot copy after digest');
}
var newHash = new Hash(this.algorithm);
const newHash = new Hash(this.algorithm);
newHash.data = [];
for (var i = 0; i < this.data.length; i++) {
for (let i = 0; i < this.data.length; i++) {
newHash.data.push(new Uint8Array(this.data[i]));
}
return newHash;

View File

@ -1,13 +1,11 @@
'use strict';
/* global Uint8Array */
var hmac = require('@noble/hashes/hmac').hmac;
var sha2 = require('@noble/hashes/sha2');
var legacy = require('@noble/hashes/legacy');
const hmac = require('@noble/hashes/hmac').hmac;
const sha2 = require('@noble/hashes/sha2');
const legacy = require('@noble/hashes/legacy');
// Map algorithm names to noble hash functions
var hashFunctions = {
const hashFunctions = {
sha1: legacy.sha1,
sha224: sha2.sha224,
sha256: sha2.sha256,
@ -33,10 +31,10 @@ function toUint8Array(data) {
// Helper function to convert Uint8Array to hex string
function toHex(bytes) {
var result = '';
var hexBase = 16;
var padLength = 2;
for (var i = 0; i < bytes.length; i++) {
let result = '';
const hexBase = 16;
const padLength = 2;
for (let i = 0; i < bytes.length; i++) {
result += bytes[i].toString(hexBase).padStart(padLength, '0');
}
return result;
@ -55,7 +53,7 @@ function Hmac(algorithm, key, encoding) {
}
// Node's createHmac allows key to be a string with encoding
var keyBuf;
let keyBuf;
if (typeof key === 'string') {
// If encoding is not provided, default to 'utf8' (Node's default)
keyBuf = Buffer.from(key, encoding || 'utf8');
@ -77,7 +75,7 @@ Hmac.prototype.update = function (data, encoding) {
throw new Error('Digest already called');
}
var uint8Data;
let uint8Data;
if (encoding) {
// Handle encoding parameter
if (encoding === 'hex') {
@ -101,7 +99,7 @@ Hmac.prototype.digest = function (encoding) {
}
// Calculate HMAC
var result = this.hmacInstance.digest();
const result = this.hmacInstance.digest();
this.finalized = true;
// Return based on encoding
@ -121,7 +119,7 @@ Hmac.prototype.copy = function () {
if (this.finalized) {
throw new Error('Cannot copy after digest');
}
var newHmac = new Hmac(this.algorithm, Buffer.from(this.hmacInstance.cloneInto().iHash.digest()));
const newHmac = new Hmac(this.algorithm, Buffer.from(this.hmacInstance.cloneInto().iHash.digest()));
newHmac.hmacInstance = this.hmacInstance.clone();
return newHmac;
};

View File

@ -1,14 +1,12 @@
'use strict';
/* global Uint8Array */
var noblePbkdf2 = require('@noble/hashes/pbkdf2');
var sha1 = require('@noble/hashes/sha1');
var sha2 = require('@noble/hashes/sha2');
var legacy = require('@noble/hashes/legacy');
const noblePbkdf2 = require('@noble/hashes/pbkdf2');
const sha1 = require('@noble/hashes/sha1');
const sha2 = require('@noble/hashes/sha2');
const legacy = require('@noble/hashes/legacy');
// Map digest names to noble hash functions
var hashFunctions = {
const hashFunctions = {
sha1: sha1.sha1,
sha224: sha2.sha224,
sha256: sha2.sha256,
@ -34,20 +32,20 @@ function toUint8Array(data) {
function pbkdf2Sync(password, salt, iterations, keylen, digest) {
// Default to sha1 if no digest specified
var hashName = digest || 'sha1';
const hashName = digest || 'sha1';
// Get the hash function
var hashFunction = hashFunctions[hashName];
const hashFunction = hashFunctions[hashName];
if (!hashFunction) {
throw new Error('Unsupported digest algorithm: ' + hashName);
}
// Convert inputs to Uint8Array
var passwordBytes = toUint8Array(password);
var saltBytes = toUint8Array(salt);
const passwordBytes = toUint8Array(password);
const saltBytes = toUint8Array(salt);
// Call noble PBKDF2
var result = noblePbkdf2.pbkdf2(hashFunction, passwordBytes, saltBytes, {
const result = noblePbkdf2.pbkdf2(hashFunction, passwordBytes, saltBytes, {
c: iterations,
dkLen: keylen
});
@ -58,17 +56,17 @@ function pbkdf2Sync(password, salt, iterations, keylen, digest) {
function pbkdf2(password, salt, iterations, keylen, digest, callback) {
// Default to sha1 if no digest specified
var hashName = digest || 'sha1';
const hashName = digest || 'sha1';
// Get the hash function
var hashFunction = hashFunctions[hashName];
const hashFunction = hashFunctions[hashName];
if (!hashFunction) {
callback(new Error('Unsupported digest algorithm: ' + hashName));
return;
}
// Convert inputs to Uint8Array
var passwordBytes, saltBytes;
let passwordBytes, saltBytes;
try {
passwordBytes = toUint8Array(password);
saltBytes = toUint8Array(salt);

203
noble-rsa-wrapper.js Normal file
View File

@ -0,0 +1,203 @@
'use strict';
const { OAEP, mgf1 } = require('micro-rsa-dsa-dh/rsa.js');
const { sha1 } = require('@noble/hashes/sha1');
const parseAsn1 = require('parse-asn1');
// Helper function to extract RSA components from PEM using parse-asn1
function parseKeyComponents(keyData) {
if (Buffer.isBuffer(keyData)) {
keyData = keyData.toString('utf8');
}
try {
// Use parse-asn1 to parse the key
const parsed = parseAsn1(keyData);
// Convert BN objects to BigInt
const n = bnToBigInt(parsed.modulus);
const e = bnToBigInt(parsed.publicExponent);
// For private keys, also get the private exponent
let d = null;
if (parsed.privateExponent) {
d = bnToBigInt(parsed.privateExponent);
}
return { n, e, d };
} catch (error) {
throw new Error(`Failed to parse key: ${error.message}`);
}
}
// Helper function to convert BN objects to BigInt
function bnToBigInt(bn) {
if (!bn) return null;
// Convert BN to hex string, then to BigInt
const hex = bn.toString(16);
return BigInt('0x' + hex);
}
// Helper function to prepare key object
function prepareKey(key) {
if (typeof key === 'object' && !Buffer.isBuffer(key)) {
// If key is an options object
const keyData = key.key || key;
const parsed = parseKeyComponents(keyData);
return { ...parsed, options: key };
} else {
// If key is just the key material
return parseKeyComponents(key);
}
}
function publicEncrypt(key, buffer) {
try {
const parsedKey = prepareKey(key);
if (!parsedKey.n || !parsedKey.e) {
throw new Error('Invalid public key');
}
const publicKey = { n: parsedKey.n, e: parsedKey.e };
// Use OAEP padding by default (for compatibility with public-encrypt module)
// Node.js uses SHA-1 by default for OAEP
const oaep = OAEP(sha1, mgf1(sha1));
const encrypted = oaep.encrypt(publicKey, new Uint8Array(buffer));
return Buffer.from(encrypted);
} catch (error) {
throw new Error(`Public encryption failed: ${error.message}`);
}
}
function privateDecrypt(key, buffer) {
try {
const parsedKey = prepareKey(key);
if (!parsedKey.n || !parsedKey.d) {
throw new Error('Invalid private key');
}
const privateKey = { n: parsedKey.n, d: parsedKey.d };
// Calculate expected ciphertext length
const k = Math.ceil(parsedKey.n.toString(16).length / 2);
// Handle case where ciphertext might be 1 byte shorter due to leading zero
let ciphertextBuffer = buffer;
if (buffer.length === k - 1) {
// Pad with leading zero to match expected length
ciphertextBuffer = Buffer.concat([Buffer.alloc(1, 0), buffer]);
}
// Use OAEP padding by default (for compatibility with public-encrypt module)
// Node.js uses SHA-1 by default for OAEP
const oaep = OAEP(sha1, mgf1(sha1));
const decrypted = oaep.decrypt(privateKey, new Uint8Array(ciphertextBuffer));
return Buffer.from(decrypted);
} catch (error) {
throw new Error(`Private decryption failed: ${error.message}`);
}
}
function privateEncrypt(key, buffer) {
try {
const parsedKey = prepareKey(key);
if (!parsedKey.n || !parsedKey.d) {
throw new Error('Invalid private key');
}
const privateKey = { n: parsedKey.n, d: parsedKey.d };
// We'll implement this using the low-level primitives
const { _TEST } = require('micro-rsa-dsa-dh/rsa.js');
const { OS2IP, I2OSP } = require('micro-rsa-dsa-dh/utils.js');
// Pad the message using PKCS1 v1.5 padding for signatures
const k = Math.ceil(parsedKey.n.toString(16).length / 2);
const mLen = buffer.length;
if (mLen > k - 11) {
throw new Error('message too long');
}
// Create PKCS1 v1.5 padding for signature (type 1)
const psLen = k - mLen - 3;
const PS = new Uint8Array(psLen).fill(0xff);
const EM = new Uint8Array([0x00, 0x01, ...PS, 0x00, ...buffer]);
const m = OS2IP(EM);
const s = _TEST.RSASP1(privateKey, m);
const encrypted = I2OSP(s, k);
return Buffer.from(encrypted);
} catch (error) {
throw new Error(`Private encryption failed: ${error.message}`);
}
}
function publicDecrypt(key, buffer) {
try {
const parsedKey = prepareKey(key);
if (!parsedKey.n || !parsedKey.e) {
throw new Error('Invalid public key');
}
const publicKey = { n: parsedKey.n, e: parsedKey.e };
// For public decrypt (signature verification), we need to use the public key
const { _TEST } = require('micro-rsa-dsa-dh/rsa.js');
const { OS2IP, I2OSP } = require('micro-rsa-dsa-dh/utils.js');
const k = Math.ceil(parsedKey.n.toString(16).length / 2);
if (buffer.length !== k) {
throw new Error('incorrect signature length');
}
const s = OS2IP(new Uint8Array(buffer));
const m = _TEST.RSAEP(publicKey, s);
if (m === false) {
throw new Error('signature verification failed');
}
const EM = I2OSP(m, k);
// Verify PKCS1 v1.5 padding for signature (type 1)
if (EM[0] !== 0x00 || EM[1] !== 0x01) {
throw new Error('invalid signature padding');
}
// Find the 0x00 separator
let sepIdx = -1;
for (let i = 2; i < EM.length; i++) {
if (EM[i] === 0x00) {
sepIdx = i;
break;
} else if (EM[i] !== 0xff) {
throw new Error('invalid signature padding');
}
}
if (sepIdx === -1 || sepIdx < 10) {
throw new Error('invalid signature padding');
}
return Buffer.from(EM.slice(sepIdx + 1));
} catch (error) {
throw new Error(`Public decryption failed: ${error.message}`);
}
}
module.exports = {
publicEncrypt,
privateDecrypt,
privateEncrypt,
publicDecrypt
};

373
noble-sign-wrapper.js Normal file
View File

@ -0,0 +1,373 @@
'use strict';
const rsa = require('micro-rsa-dsa-dh/rsa.js');
const parseASN1 = require('parse-asn1');
const { p256 } = require('@noble/curves/p256');
const { secp256k1 } = require('@noble/curves/secp256k1');
const { sha1 } = require('@noble/hashes/sha1');
const { sha256 } = require('@noble/hashes/sha2');
// Helper function to convert Buffer/string to Uint8Array
function toUint8Array(data) {
if (data instanceof Uint8Array) {
return data;
}
if (data instanceof Buffer) {
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
}
if (typeof data === 'string') {
return new TextEncoder().encode(data);
}
throw new Error('Unsupported data type');
}
// Use parse-asn1 to parse keys reliably
function parseKey(keyData) {
try {
const parsed = parseASN1(keyData);
// Check if it's an RSA key (has modulus)
if (parsed.modulus) {
// RSA key
const n = parsed.modulus;
const e = parsed.publicExponent;
const d = parsed.privateExponent;
if (!n) {
throw new Error('Missing modulus');
}
if (!e) {
throw new Error('Missing exponent');
}
// Convert to BigInt format expected by micro-rsa-dsa-dh
const nBig = BigInt('0x' + n.toString('hex'));
const eBig = BigInt('0x' + e.toString('hex'));
if (d) {
// Private key
const dBig = BigInt('0x' + d.toString('hex'));
return {
type: 'rsa',
n: nBig, e: eBig, d: dBig
};
}
// Public key
return {
type: 'rsa',
n: nBig, e: eBig
};
} else if (parsed.privateKey && parsed.curve) {
// EC private key - convert OID to curve name
let curveName = 'prime256v1'; // default
if (Array.isArray(parsed.curve)) {
// OID [1, 3, 132, 0, 10] = secp256k1
const oidStr = parsed.curve.join('.');
if (oidStr === '1.3.132.0.10') {
curveName = 'secp256k1';
} else if (oidStr === '1.2.840.10045.3.1.7') {
curveName = 'prime256v1';
}
}
return {
type: 'ec',
privateKey: parsed.privateKey,
curve: curveName
};
} else if (parsed.type === 'ec' && parsed.data && parsed.data.subjectPublicKey) {
// EC public key with full structure
let curveName = 'prime256v1'; // default
if (parsed.data.algorithm && parsed.data.algorithm.curve && Array.isArray(parsed.data.algorithm.curve)) {
const oidStr = parsed.data.algorithm.curve.join('.');
if (oidStr === '1.3.132.0.10') {
curveName = 'secp256k1';
} else if (oidStr === '1.2.840.10045.3.1.7') {
curveName = 'prime256v1';
}
}
return {
type: 'ec',
publicKey: parsed.data.subjectPublicKey.data,
curve: curveName
};
} else {
throw new Error('Unknown key type');
}
} catch (error) {
throw new Error('Failed to parse key: ' + error.message);
}
}
// Map algorithm names to signers and hash functions
const algorithms = {
// RSA algorithms
'RSA-SHA1': { type: 'rsa', signer: rsa.PKCS1_SHA1 },
'RSA-SHA256': { type: 'rsa', signer: rsa.PKCS1_SHA256 },
'RSA-SHA384': { type: 'rsa', signer: rsa.PKCS1_SHA384 },
'RSA-SHA512': { type: 'rsa', signer: rsa.PKCS1_SHA512 },
sha1WithRSAEncryption: { type: 'rsa', signer: rsa.PKCS1_SHA1 },
sha256WithRSAEncryption: { type: 'rsa', signer: rsa.PKCS1_SHA256 },
// EC algorithms (ECDSA)
'sha1': { type: 'ec', hash: sha1 },
'sha256': { type: 'ec', hash: sha256 },
'ECDSA-SHA1': { type: 'ec', hash: sha1 },
'ECDSA-SHA256': { type: 'ec', hash: sha256 }
};
function Sign(algorithm) {
this.algorithm = algorithm;
this.algorithmInfo = algorithms[algorithm];
if (!this.algorithmInfo) {
throw new Error('Unsupported algorithm: ' + algorithm);
}
this.data = [];
this.finished = false;
}
Sign.prototype.update = function (data, encoding) {
if (this.finished) {
throw new Error('Cannot update after sign has been called');
}
let uint8Data;
if (encoding) {
if (encoding === 'hex') {
uint8Data = new Uint8Array(Buffer.from(data, 'hex'));
} else if (encoding === 'base64') {
uint8Data = new Uint8Array(Buffer.from(data, 'base64'));
} else {
throw new Error('Unsupported encoding: ' + encoding);
}
} else {
uint8Data = toUint8Array(data);
}
this.data.push(uint8Data);
return this;
};
Sign.prototype.sign = function (privateKey, encoding) {
if (this.finished) {
throw new Error('Sign already called');
}
// Concatenate all data
let totalLength = 0;
for (let i = 0; i < this.data.length; i++) {
totalLength += this.data[i].length;
}
const concatenated = new Uint8Array(totalLength);
let offset = 0;
for (let j = 0; j < this.data.length; j++) {
const chunk = this.data[j];
concatenated.set(chunk, offset);
offset += chunk.length;
}
// Parse private key
let keyData;
if (typeof privateKey === 'string' || privateKey instanceof Buffer) {
keyData = parseKey(privateKey);
} else {
throw new Error('Unsupported private key format');
}
let signature;
if (this.algorithmInfo.type === 'rsa') {
// RSA signing
if (keyData.type !== 'rsa') {
throw new Error('RSA algorithm requires RSA key');
}
signature = this.algorithmInfo.signer.sign(keyData, concatenated);
} else if (this.algorithmInfo.type === 'ec') {
// EC signing
if (keyData.type !== 'ec') {
throw new Error('EC algorithm requires EC key');
}
// Hash the data
const hash = this.algorithmInfo.hash(concatenated);
// Choose the curve - for now default to p256
let curve = p256;
if (keyData.curve === 'secp256k1') {
curve = secp256k1;
}
// Convert private key to hex string if it's a Buffer
let privKeyHex = keyData.privateKey;
if (Buffer.isBuffer(privKeyHex)) {
privKeyHex = privKeyHex.toString('hex');
}
// Sign with the curve
signature = curve.sign(hash, privKeyHex).toDERRawBytes();
} else {
throw new Error('Unknown algorithm type');
}
this.finished = true;
// Return signature in requested format
if (encoding === 'hex') {
return Buffer.from(signature).toString('hex');
} else if (encoding === 'base64') {
return Buffer.from(signature).toString('base64');
}
return Buffer.from(signature);
};
function Verify(algorithm) {
this.algorithm = algorithm;
this.algorithmInfo = algorithms[algorithm];
if (!this.algorithmInfo) {
throw new Error('Unsupported algorithm: ' + algorithm);
}
this.data = [];
this.finished = false;
}
Verify.prototype.update = function (data, encoding) {
if (this.finished) {
throw new Error('Cannot update after verify has been called');
}
let uint8Data;
if (encoding) {
if (encoding === 'hex') {
uint8Data = new Uint8Array(Buffer.from(data, 'hex'));
} else if (encoding === 'base64') {
uint8Data = new Uint8Array(Buffer.from(data, 'base64'));
} else {
throw new Error('Unsupported encoding: ' + encoding);
}
} else {
uint8Data = toUint8Array(data);
}
this.data.push(uint8Data);
return this;
};
Verify.prototype.verify = function (publicKey, signature, encoding) {
if (this.finished) {
throw new Error('Verify already called');
}
// Concatenate all data
let totalLength = 0;
for (let i = 0; i < this.data.length; i++) {
totalLength += this.data[i].length;
}
const concatenated = new Uint8Array(totalLength);
let offset = 0;
for (let j = 0; j < this.data.length; j++) {
const chunk = this.data[j];
concatenated.set(chunk, offset);
offset += chunk.length;
}
// Parse public key
let keyData;
if (typeof publicKey === 'string' || publicKey instanceof Buffer) {
keyData = parseKey(publicKey);
} else {
throw new Error('Unsupported public key format');
}
// Convert signature to Uint8Array
let sigData;
if (encoding === 'hex') {
sigData = new Uint8Array(Buffer.from(signature, 'hex'));
} else if (encoding === 'base64') {
sigData = new Uint8Array(Buffer.from(signature, 'base64'));
} else {
sigData = toUint8Array(signature);
}
let result;
if (this.algorithmInfo.type === 'rsa') {
// RSA verification
if (keyData.type !== 'rsa') {
throw new Error('RSA algorithm requires RSA key');
}
result = this.algorithmInfo.signer.verify(keyData, concatenated, sigData);
} else if (this.algorithmInfo.type === 'ec') {
// EC verification
if (keyData.type !== 'ec') {
throw new Error('EC algorithm requires EC key');
}
// Hash the data
const hash = this.algorithmInfo.hash(concatenated);
// Choose the curve - for now default to p256
let curve = p256;
if (keyData.curve === 'secp256k1') {
curve = secp256k1;
}
// Convert public key to hex string if it's a Buffer
let pubKeyHex = keyData.publicKey;
if (Buffer.isBuffer(pubKeyHex)) {
pubKeyHex = pubKeyHex.toString('hex');
} else if (pubKeyHex instanceof Uint8Array) {
pubKeyHex = Buffer.from(pubKeyHex).toString('hex');
}
// Verify with the curve
try {
const sig = curve.Signature.fromDER(sigData);
// Try verification with original signature
let verified = curve.verify(sig, hash, pubKeyHex);
// If verification fails, try signature malleability fix
// In ECDSA, both (r, s) and (r, n - s) are valid signatures
if (!verified) {
try {
// Create alternate signature with s' = n - s
const n = curve.CURVE.n;
const altS = n - sig.s;
const altSig = new curve.Signature(sig.r, altS);
verified = curve.verify(altSig, hash, pubKeyHex);
} catch (e) {
// If that also fails, the signature is genuinely invalid
verified = false;
}
}
result = verified;
} catch (error) {
result = false;
}
} else {
throw new Error('Unknown algorithm type');
}
this.finished = true;
return result;
};
function createSign(algorithm) {
return new Sign(algorithm);
}
function createVerify(algorithm) {
return new Verify(algorithm);
}
module.exports = {
createSign: createSign,
Sign: Sign,
createVerify: createVerify,
Verify: Verify
};

View File

@ -1,21 +1,17 @@
{
"author": "Dominic Tarr <dominic.tarr@gmail.com> (dominictarr.com)",
"name": "crypto-browserify",
"name": "noble-crypto",
"description": "implementation of crypto for the browser",
"version": "3.12.1",
"homepage": "https://github.com/browserify/crypto-browserify",
"version": "4.0.0",
"homepage": "https://github.com/BlueWallet/noble-crypto",
"sideEffects": false,
"repository": {
"type": "git",
"url": "git://github.com/browserify/crypto-browserify.git"
"url": "git://github.com/BlueWallet/noble-crypto.git"
},
"scripts": {
"lint": "eslint --ext=js,mjs .",
"test": "nyc tape 'test/**/*.js'"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
},
"engines": {
"node": ">= 0.10"
},
@ -24,16 +20,15 @@
"@noble/curves": "^1.9.2",
"@noble/hashes": "^1.8.0",
"micro-rsa-dsa-dh": "^0.1.0",
"public-encrypt": "^4.0.3"
"parse-asn1": "^5.1.7",
"safe-buffer": "^5.2.1"
},
"devDependencies": {
"@ljharb/eslint-config": "^21.1.1",
"eslint": "=8.8.0",
"hash-test-vectors": "^1.3.2",
"nyc": "^10.3.2",
"object.entries": "^1.1.8",
"pseudorandombytes": "^2.0.0",
"safe-buffer": "^5.2.1",
"public-encrypt": "^4.0.3",
"semver": "^6.3.1",
"tape": "^5.9.0"
},

View File

@ -1,21 +1,21 @@
'use strict';
var test = require('tape');
var bcCrypto = require('../noble-cipher-wrapper');
var bcCyphers = bcCrypto.getCiphers();
var randomBytes = require('pseudorandombytes');
const test = require('tape');
const bcCrypto = require('../noble-cipher-wrapper');
const bcCyphers = bcCrypto.getCiphers();
const randomBytes = require('pseudorandombytes');
for (var i = 0; i < 4; i += 1) {
for (let i = 0; i < 4; i += 1) {
bcCrypto.listCiphers().forEach(function (cipher) {
test('run: ' + i, function (t) {
/* eslint no-loop-func: 0 */
t.test('ciphers: ' + cipher, function (st) {
st.plan(1);
var data = randomBytes(562);
var password = randomBytes(20);
var crypter = bcCrypto.createCipher(cipher, password);
var decrypter = bcCrypto.createDecipher(cipher, password);
var out = [];
const data = randomBytes(562);
const password = randomBytes(20);
const crypter = bcCrypto.createCipher(cipher, password);
const decrypter = bcCrypto.createDecipher(cipher, password);
const out = [];
out.push(decrypter.update(crypter.update(data)));
out.push(decrypter.update(crypter['final']()));
if (cipher.indexOf('gcm') > -1) {
@ -37,13 +37,13 @@ test('getCiphers', function (t) {
// eslint-disable-next-line global-require
test('through crypto browserify works', { skip: !require('crypto').createCipher && 'node 22+ removes createCipher' }, function (t) {
t.plan(2);
var crypto = require('../'); // eslint-disable-line global-require
var cipher = 'aes-128-ctr';
var data = randomBytes(562);
var password = randomBytes(20);
var crypter = crypto.createCipher(cipher, password);
var decrypter = crypto.createDecipher(cipher, password);
var out = [];
const crypto = require('../'); // eslint-disable-line global-require
const cipher = 'aes-128-ctr';
const data = randomBytes(562);
const password = randomBytes(20);
const crypter = crypto.createCipher(cipher, password);
const decrypter = crypto.createDecipher(cipher, password);
const out = [];
out.push(decrypter.update(crypter.update(data)));
out.push(decrypter.update(crypter['final']()));
out.push(decrypter['final']());

View File

@ -1,14 +1,14 @@
'use strict';
var test = require('tape');
var satisfies = require('semver').satisfies;
const test = require('tape');
const satisfies = require('semver').satisfies;
var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160'];
var encodings = ['hex', 'base64']; // FIXME: test binary
var vectors = require('hash-test-vectors');
const algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160'];
const encodings = ['hex', 'base64']; // FIXME: test binary
const vectors = require('hash-test-vectors');
function runTest(name, createHash, algorithm) {
var isUnsupported = satisfies(process.version, '^17') && (
const isUnsupported = satisfies(process.version, '^17') && (
algorithm === 'rmd160'
|| algorithm === 'hmac(rmd160)'
);
@ -17,20 +17,20 @@ function runTest(name, createHash, algorithm) {
{ skip: isUnsupported && 'this node version does not support ' + algorithm },
function (t) {
vectors.forEach(function (obj, i) {
var input = new Buffer(obj.input, 'base64');
var node = obj[algorithm];
var js = createHash(algorithm).update(input).digest('hex');
let input = new Buffer(obj.input, 'base64');
let node = obj[algorithm];
let js = createHash(algorithm).update(input).digest('hex');
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node);
encodings.forEach(function (encoding) {
var eInput = new Buffer(obj.input, 'base64').toString(encoding);
var eNode = obj[algorithm];
var eJS = createHash(algorithm).update(eInput, encoding).digest('hex');
const eInput = new Buffer(obj.input, 'base64').toString(encoding);
const eNode = obj[algorithm];
const eJS = createHash(algorithm).update(eInput, encoding).digest('hex');
t.equal(eJS, eNode, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + eNode);
});
input = new Buffer(obj.input, 'base64');
node = obj[algorithm];
var hash = createHash(algorithm);
const hash = createHash(algorithm);
hash.end(input);
js = hash.read().toString('hex');
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node);

View File

@ -1,14 +1,14 @@
'use strict';
var test = require('tape');
var satisfies = require('semver').satisfies;
const test = require('tape');
const satisfies = require('semver').satisfies;
var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160'];
var vectors = require('hash-test-vectors/hmac');
const algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160'];
const vectors = require('hash-test-vectors/hmac');
function testLib(name, createHmac) {
algorithms.forEach(function (alg) {
var isUnsupported = satisfies(process.version, '^17') && (
const isUnsupported = satisfies(process.version, '^17') && (
alg === 'rmd160'
|| alg === 'hmac(rmd160)'
);
@ -17,7 +17,7 @@ function testLib(name, createHmac) {
{ skip: isUnsupported && 'this node version does not support ' + alg },
function (t) {
vectors.forEach(function (input) {
var output = createHmac(alg, new Buffer(input.key, 'hex'))
let output = createHmac(alg, new Buffer(input.key, 'hex'))
.update(input.data, 'hex').digest();
output = input.truncate ? output.slice(0, input.truncate) : output;
@ -34,10 +34,10 @@ function testLib(name, createHmac) {
{ skip: isUnsupported && 'this node version does not support ' + alg },
function (t) {
vectors.forEach(function (input) {
var hmac = createHmac(alg, new Buffer(input.key, 'hex'));
const hmac = createHmac(alg, new Buffer(input.key, 'hex'));
hmac.end(input.data, 'hex');
var output = hmac.read();
let output = hmac.read();
output = input.truncate ? output.slice(0, input.truncate) : output;
output = output.toString('hex');

View File

@ -1,7 +1,7 @@
'use strict';
var test = require('tape');
var crypto = require('..');
const test = require('tape');
const crypto = require('..');
test('diffie-hellman mod groups', function (t) {
[
@ -14,18 +14,18 @@ test('diffie-hellman mod groups', function (t) {
].forEach(function (mod) {
t.test(mod, function (st) {
st.plan(3);
var dh1 = crypto.getDiffieHellman(mod);
var p1 = dh1.getPrime().toString('hex');
const dh1 = crypto.getDiffieHellman(mod);
const p1 = dh1.getPrime().toString('hex');
dh1.generateKeys();
var dh2 = crypto.getDiffieHellman(mod);
var p2 = dh2.getPrime().toString('hex');
const dh2 = crypto.getDiffieHellman(mod);
const p2 = dh2.getPrime().toString('hex');
dh2.generateKeys();
st.equals(p1, p2, 'equal primes');
var pubk1 = dh1.getPublicKey();
var pubk2 = dh2.getPublicKey();
const pubk1 = dh1.getPublicKey();
const pubk2 = dh2.getPublicKey();
st.notEquals(pubk1, pubk2, 'diff public keys');
var pub1 = dh1.computeSecret(pubk2).toString('hex');
var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
const pub1 = dh1.computeSecret(pubk2).toString('hex');
const pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
st.equals(pub1, pub2, 'equal secrets');
});
});
@ -39,21 +39,21 @@ test('diffie-hellman key lengths', function (t) {
].forEach(function (len) {
t.test(String(len), function (st) {
st.plan(3);
var dh2 = crypto.createDiffieHellman(len);
var prime2 = dh2.getPrime();
var p2 = prime2.toString('hex');
var dh1 = crypto.createDiffieHellman(prime2);
var p1 = dh1.getPrime().toString('hex');
const dh2 = crypto.createDiffieHellman(len);
const prime2 = dh2.getPrime();
const p2 = prime2.toString('hex');
const dh1 = crypto.createDiffieHellman(prime2);
const p1 = dh1.getPrime().toString('hex');
dh1.generateKeys();
dh2.generateKeys();
st.equals(p1, p2, 'equal primes');
var pubk1 = dh1.getPublicKey();
var pubk2 = dh2.getPublicKey();
const pubk1 = dh1.getPublicKey();
const pubk2 = dh2.getPublicKey();
st.notEquals(pubk1, pubk2, 'diff public keys');
var pub1 = dh1.computeSecret(pubk2).toString('hex');
var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
const pub1 = dh1.computeSecret(pubk2).toString('hex');
const pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
st.equals(pub1, pub2, 'equal secrets');
});
});

View File

@ -1,63 +1,63 @@
'use strict';
var mods = [
const mods = [
'secp256k1',
// 'secp224r1', // not supported in noble
'prime256v1'
// 'prime192v1' // not supported in noble
];
var test = require('tape');
var createECDH1 = require('../noble-ecdh-wrapper');
var createECDH2 = require('../noble-ecdh-wrapper');
const test = require('tape');
const createECDH1 = require('../noble-ecdh-wrapper');
const createECDH2 = require('../noble-ecdh-wrapper');
mods.forEach(function (mod) {
test('createECDH: ' + mod + ' uncompressed', function (t) {
t.plan(2);
var dh1 = createECDH1(mod);
const dh1 = createECDH1(mod);
dh1.generateKeys();
var dh2 = createECDH2(mod);
const dh2 = createECDH2(mod);
dh2.generateKeys();
var pubk1 = dh1.getPublicKey();
var pubk2 = dh2.getPublicKey();
const pubk1 = dh1.getPublicKey();
const pubk2 = dh2.getPublicKey();
t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys');
var pub1 = dh1.computeSecret(pubk2).toString('hex');
var pub2 = dh2.computeSecret(pubk1).toString('hex');
const pub1 = dh1.computeSecret(pubk2).toString('hex');
const pub2 = dh2.computeSecret(pubk1).toString('hex');
t.equals(pub1, pub2, 'equal secrets');
});
test('createECDH: ' + mod + ' compressed', function (t) {
t.plan(2);
var dh1 = createECDH1(mod);
const dh1 = createECDH1(mod);
dh1.generateKeys();
var dh2 = createECDH2(mod);
const dh2 = createECDH2(mod);
dh2.generateKeys();
var pubk1 = dh1.getPublicKey(null, 'compressed');
var pubk2 = dh2.getPublicKey(null, 'compressed');
const pubk1 = dh1.getPublicKey(null, 'compressed');
const pubk2 = dh2.getPublicKey(null, 'compressed');
t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys');
var pub1 = dh1.computeSecret(pubk2).toString('hex');
var pub2 = dh2.computeSecret(pubk1).toString('hex');
const pub1 = dh1.computeSecret(pubk2).toString('hex');
const pub2 = dh2.computeSecret(pubk1).toString('hex');
t.equals(pub1, pub2, 'equal secrets');
});
test('createECDH: ' + mod + ' set stuff', function (t) {
t.plan(4);
var dh1 = createECDH1(mod);
var dh2 = createECDH2(mod);
const dh1 = createECDH1(mod);
const dh2 = createECDH2(mod);
dh1.generateKeys();
dh2.generateKeys();
dh1.setPrivateKey(dh2.getPrivateKey());
dh1.setPublicKey(dh2.getPublicKey());
var priv1 = dh1.getPrivateKey('hex');
var priv2 = dh2.getPrivateKey('hex');
const priv1 = dh1.getPrivateKey('hex');
const priv2 = dh2.getPrivateKey('hex');
t.equals(priv1, priv2, 'same private key');
var pubk1 = dh1.getPublicKey();
var pubk2 = dh2.getPublicKey();
const pubk1 = dh1.getPublicKey();
const pubk2 = dh2.getPublicKey();
t.equals(pubk1.toString('hex'), pubk2.toString('hex'), 'same public keys, uncompressed');
t.equals(dh1.getPublicKey('hex', 'compressed'), dh2.getPublicKey('hex', 'compressed'), 'same public keys compressed');
// not supported in noble
// t.equals(dh1.getPublicKey('hex', 'hybrid'), dh2.getPublicKey('hex', 'hybrid'), 'same public keys hybrid');
var pub1 = dh1.computeSecret(pubk2).toString('hex');
var pub2 = dh2.computeSecret(pubk1).toString('hex');
const pub1 = dh1.computeSecret(pubk2).toString('hex');
const pub2 = dh2.computeSecret(pubk1).toString('hex');
t.equals(pub1, pub2, 'equal secrets');
});
});

View File

@ -1,9 +1,9 @@
'use strict';
var test = require('tape');
var cryptoB = require('../../');
var crypto = require('crypto');
var satisfies = require('semver').satisfies;
const test = require('tape');
const cryptoB = require('../../');
const crypto = require('crypto');
const satisfies = require('semver').satisfies;
test('diffie-hellman mod groups', function (t) {
[
@ -16,21 +16,21 @@ test('diffie-hellman mod groups', function (t) {
].forEach(function (mod) {
t.test(mod, function (st) {
st.plan(3);
var dh1 = cryptoB.getDiffieHellman(mod);
var p1 = dh1.getPrime().toString('hex');
const dh1 = cryptoB.getDiffieHellman(mod);
const p1 = dh1.getPrime().toString('hex');
dh1.generateKeys();
var dh2 = crypto.getDiffieHellman(mod);
var p2 = dh2.getPrime().toString('hex');
const dh2 = crypto.getDiffieHellman(mod);
const p2 = dh2.getPrime().toString('hex');
dh2.generateKeys();
st.equals(p1, p2, 'equal primes');
var pubk1 = dh1.getPublicKey();
var pubk2 = dh2.getPublicKey();
const pubk1 = dh1.getPublicKey();
const pubk2 = dh2.getPublicKey();
st.notEquals(pubk1, pubk2, 'diff public keys');
var pub1 = dh1.computeSecret(pubk2).toString('hex');
var pub2 = dh2.computeSecret(pubk1).toString('hex');
const pub1 = dh1.computeSecret(pubk2).toString('hex');
const pub2 = dh2.computeSecret(pubk1).toString('hex');
st.equals(pub1, pub2, 'equal secrets');
});
});
@ -44,23 +44,23 @@ test('diffie-hellman key lengths', function (t) {
512,
1024
].forEach(function (len) {
var modulusTooSmall = satisfies(process.version, '>= 17') && len < 512;
const modulusTooSmall = satisfies(process.version, '>= 17') && len < 512;
t.test(String(len), { skip: modulusTooSmall && 'node 17+ requires a length >= 512' }, function (st) {
var dh2 = cryptoB.createDiffieHellman(len);
var prime2 = dh2.getPrime();
var p2 = prime2.toString('hex');
var dh1 = crypto.createDiffieHellman(prime2);
var p1 = dh1.getPrime().toString('hex');
const dh2 = cryptoB.createDiffieHellman(len);
const prime2 = dh2.getPrime();
const p2 = prime2.toString('hex');
const dh1 = crypto.createDiffieHellman(prime2);
const p1 = dh1.getPrime().toString('hex');
dh1.generateKeys();
dh2.generateKeys();
st.equals(p1, p2, 'equal primes');
var pubk1 = dh1.getPublicKey();
var pubk2 = dh2.getPublicKey();
const pubk1 = dh1.getPublicKey();
const pubk2 = dh2.getPublicKey();
st.notEquals(pubk1, pubk2, 'diff public keys');
var pub1 = dh1.computeSecret(pubk2).toString('hex');
var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
const pub1 = dh1.computeSecret(pubk2).toString('hex');
const pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
st.equals(pub1, pub2, 'equal secrets');
st.end();

View File

@ -1,16 +1,16 @@
'use strict';
var tape = require('tape');
var crypto = require('../noble-pbkdf2-wrapper');
const tape = require('tape');
const crypto = require('../noble-pbkdf2-wrapper');
var vectors = require('hash-test-vectors/pbkdf2');
const vectors = require('hash-test-vectors/pbkdf2');
tape('pbkdf2', function (t) {
vectors.forEach(function (input) {
// skip inputs that will take way too long
if (input.iterations > 10000) { return; }
var key = crypto.pbkdf2Sync(input.password, input.salt, input.iterations, input.length);
const key = crypto.pbkdf2Sync(input.password, input.salt, input.iterations, input.length);
if (key.toString('hex') !== input.sha1) {
console.log(input);

View File

@ -1,16 +1,16 @@
'use strict';
var test = require('tape');
var crypto1 = require('../');
var rsa = {
const test = require('tape');
const crypto1 = require('../');
const rsa = {
'private': '2d2d2d2d2d424547494e205253412050524956415445204b45592d2d2d2d2d0a4d4949456a77494241414b422f6779376d6a615767506546645659445a5752434139424e69763370506230657332372b464b593068737a4c614f7734374578430a744157704473483438545841667948425977424c67756179666b344c4749757078622b43474d62526f337845703043626659314a62793236543976476a5243310a666f484444554a4738347561526279487161663469367a74346756522b786c4145496a6b614641414b38634f6f58415431435671474c4c6c6a554363684c38500a6a61486a2f7972695a2f53377264776c49334c6e41427877776d4c726d522f7637315774706d4f2f614e47384e2b31706f2b5177616768546b79513539452f5a0a7641754f6b4657486f6b32712f523650594161326a645a397a696d3046714f502b6e6b5161454452624246426d4271547635664647666b32577341664b662f520a47302f5646642b5a654d353235315465547658483639356e6c53476175566c3941674d42414145436766344c725748592f6c35346f7554685a577676627275670a70667a36734a583267396c3779586d576c455773504543566f2f375355627059467074364f5a7939397a53672b494b624771574b6664686f4b725477495674430a4c30595a304e6c6d646e414e53497a30726f785147375a786b4c352b764853772f506d443978345577662b437a38684154436d4e42763171633630646b7975570a34434c71653732716154695657526f4f316961675167684e634c6f6f36765379363545784c614344545068613779753276773468465a705769456a57346478660a7246644c696978353242433836596c416c784d452f724c6738494a5676696c62796f39615764586d784f6155544c527636506b4644312f6756647738563951720a534c4e39466c4b326b6b6a695830647a6f6962765a7733744d6e74337979644178305838372b734d5256616843316270336b56507a3448793045575834514a2f0a504d33317647697549546b324e43643531445874314c746e324f503546614a536d4361456a6830586b5534716f7559796a585774384275364254436c327675610a466730556a6939432b496b504c6d61554d624d494f7761546b386357714c74685378734c6537304a354f6b477267664b554d2f772b4248483150742f506a7a6a0a432b2b6c306b6946614f5644566141563947704c504c43426f4b2f50433952622f72784d4d6f43434e774a2f4e5a756564496e793277334c4d69693737682f540a7a53766572674e47686a5936526e7661386c4c584a36646c726b6350417970733367577778716a344e5230542b474d3062445550564c62374d303758563753580a7637564a476d35324a625247774d3173732b72385854544e656d65476b2b5752784737546774734d715947584c66423851786b2f66352f4d63633030546c38750a7758464e7366784a786d7436416273547233673336774a2f49684f6e69627a3941642b6e63686c426e4e3351655733434b48717a61523138766f717674566d320a6b4a66484b31357072482f7353476d786d6945476772434a545a78744462614e434f372f56426a6e4b756455554968434177734c747571302f7a7562397641640a384731736366497076357161534e7a6d4b6f5838624f77417276725336775037794b726354737557496c484438724a5649374945446e516f5470354738664b310a68774a2f4d4968384d35763072356455594576366f494a5747636c65364148314a6d73503557496166677137325a32323838704863434648774e59384467394a0a3736517377564c6e556850546c6d6d33454f4f50474574616d32694144357230416679746c62346c624e6f51736a32737a65584f4e4458422b366f7565616a680a564e454c55723848635350356c677a525a6a4a57366146497a6a394c44526d516e55414f6a475358564f517445774a2f4d43515a374e2f763464494b654452410a3864385545785a332b674748756d7a697a7447524a30745172795a483250616b50354937562b316c377145556e4a3263336d462b65317634314570394c4376680a627a72504b773964786831386734622b37624d707357506e7372614b6836697078633761614f615a5630447867657a347a635a753050316f6c4f30634e334b4d0a6e784a305064733352386241684e43446453324a5a61527035513d3d0a2d2d2d2d2d454e44205253412050524956415445204b45592d2d2d2d2d0a',
'public': '2d2d2d2d2d424547494e20525341205055424c4943204b45592d2d2d2d2d0a4d49494242674b422f6779376d6a615767506546645659445a5752434139424e69763370506230657332372b464b593068737a4c614f773437457843744157700a4473483438545841667948425977424c67756179666b344c4749757078622b43474d62526f337845703043626659314a62793236543976476a524331666f48440a44554a4738347561526279487161663469367a74346756522b786c4145496a6b614641414b38634f6f58415431435671474c4c6c6a554363684c38506a61486a0a2f7972695a2f53377264776c49334c6e41427877776d4c726d522f7637315774706d4f2f614e47384e2b31706f2b5177616768546b79513539452f5a7641754f0a6b4657486f6b32712f523650594161326a645a397a696d3046714f502b6e6b5161454452624246426d4271547635664647666b32577341664b662f5247302f560a46642b5a654d353235315465547658483639356e6c53476175566c3941674d424141453d0a2d2d2d2d2d454e4420525341205055424c4943204b45592d2d2d2d2d0a'
};
var crypto2 = require('public-encrypt/browser');
const crypto2 = require('public-encrypt/browser');
rsa['private'] = new Buffer(rsa['private'], 'hex');
rsa['public'] = new Buffer(rsa['public'], 'hex');
var encrypted = '0bcd6462ad7a563be2d42b0b73e0b0a163886304e7723b025f97605144fe1781e84acdc4031327d6bccd67fe13183e8fbdc8c5fe947b49d011ce3ebb08b11e83b87a77328ca57ee77cfdc78743b0749366643d7a21b2abcd4aa32dee9832938445540ee3007b7a70191c8dc9ff2ad76fe8dfaa5362d9d2c4b31a67b816d7b7970a293cb95bf3437a301bedb9f431b7075aa2f9df77b4385bea2a37982beda467260b384a58258b5eb4e36a0e0bf7dff83589636f5f97bf542084f0f76868c9f3f989a27fee5b8cd2bfee0bae1eae958df7c3184e5a40fda101196214f371606feca4330b221f30577804bbd4f61578a84e85dcd298849f509e630d275280';
const encrypted = '0bcd6462ad7a563be2d42b0b73e0b0a163886304e7723b025f97605144fe1781e84acdc4031327d6bccd67fe13183e8fbdc8c5fe947b49d011ce3ebb08b11e83b87a77328ca57ee77cfdc78743b0749366643d7a21b2abcd4aa32dee9832938445540ee3007b7a70191c8dc9ff2ad76fe8dfaa5362d9d2c4b31a67b816d7b7970a293cb95bf3437a301bedb9f431b7075aa2f9df77b4385bea2a37982beda467260b384a58258b5eb4e36a0e0bf7dff83589636f5f97bf542084f0f76868c9f3f989a27fee5b8cd2bfee0bae1eae958df7c3184e5a40fda101196214f371606feca4330b221f30577804bbd4f61578a84e85dcd298849f509e630d275280';
test('publicEncrypt/privateDecrypt', function (t) {
t.test('can decrypt', function (st) {
@ -21,7 +21,7 @@ test('publicEncrypt/privateDecrypt', function (t) {
});
t.test('can round trip', function (st) {
st.plan(2);
var msg = 'this is a message';
const msg = 'this is a message';
// note encryption is ranomized so can't test to see if they encrypt the same
st.equals(crypto1.privateDecrypt(rsa['private'], crypto2.publicEncrypt(rsa['public'], new Buffer(msg))).toString(), msg, 'round trip it');
st.equals(crypto2.privateDecrypt(rsa['private'], crypto1.publicEncrypt(rsa['public'], new Buffer(msg))).toString(), msg, 'round trip it');
@ -31,7 +31,7 @@ test('publicEncrypt/privateDecrypt', function (t) {
test('privateEncrypt/publicDecrypt', function (t) {
t.test('can round trip', function (st) {
st.plan(2);
var msg = 'this is a message';
const msg = 'this is a message';
// note encryption is ranomized so can't test to see if they encrypt the same
st.equals(crypto1.publicDecrypt(rsa['public'], crypto2.privateEncrypt(rsa['private'], new Buffer(msg))).toString(), msg, 'round trip it');
st.equals(crypto2.publicDecrypt(rsa['public'], crypto1.privateEncrypt(rsa['private'], new Buffer(msg))).toString(), msg, 'round trip it');

View File

@ -1,15 +1,15 @@
'use strict';
var test = require('tape');
var nodeCrypto = require('crypto');
var ourCrypto = require('../');
const test = require('tape');
const nodeCrypto = require('crypto');
const ourCrypto = require('../');
var rsa = {
const rsa = {
'private': '2d2d2d2d2d424547494e205253412050524956415445204b45592d2d2d2d2d0a4d4949456a77494241414b422f6779376d6a615767506546645659445a5752434139424e69763370506230657332372b464b593068737a4c614f7734374578430a744157704473483438545841667948425977424c67756179666b344c4749757078622b43474d62526f337845703043626659314a62793236543976476a5243310a666f484444554a4738347561526279487161663469367a74346756522b786c4145496a6b614641414b38634f6f58415431435671474c4c6c6a554363684c38500a6a61486a2f7972695a2f53377264776c49334c6e41427877776d4c726d522f7637315774706d4f2f614e47384e2b31706f2b5177616768546b79513539452f5a0a7641754f6b4657486f6b32712f523650594161326a645a397a696d3046714f502b6e6b5161454452624246426d4271547635664647666b32577341664b662f520a47302f5646642b5a654d353235315465547658483639356e6c53476175566c3941674d42414145436766344c725748592f6c35346f7554685a577676627275670a70667a36734a583267396c3779586d576c455773504543566f2f375355627059467074364f5a7939397a53672b494b624771574b6664686f4b725477495674430a4c30595a304e6c6d646e414e53497a30726f785147375a786b4c352b764853772f506d443978345577662b437a38684154436d4e42763171633630646b7975570a34434c71653732716154695657526f4f316961675167684e634c6f6f36765379363545784c614344545068613779753276773468465a705769456a57346478660a7246644c696978353242433836596c416c784d452f724c6738494a5676696c62796f39615764586d784f6155544c527636506b4644312f6756647738563951720a534c4e39466c4b326b6b6a695830647a6f6962765a7733744d6e74337979644178305838372b734d5256616843316270336b56507a3448793045575834514a2f0a504d33317647697549546b324e43643531445874314c746e324f503546614a536d4361456a6830586b5534716f7559796a585774384275364254436c327675610a466730556a6939432b496b504c6d61554d624d494f7761546b386357714c74685378734c6537304a354f6b477267664b554d2f772b4248483150742f506a7a6a0a432b2b6c306b6946614f5644566141563947704c504c43426f4b2f50433952622f72784d4d6f43434e774a2f4e5a756564496e793277334c4d69693737682f540a7a53766572674e47686a5936526e7661386c4c584a36646c726b6350417970733367577778716a344e5230542b474d3062445550564c62374d303758563753580a7637564a476d35324a625247774d3173732b72385854544e656d65476b2b5752784737546774734d715947584c66423851786b2f66352f4d63633030546c38750a7758464e7366784a786d7436416273547233673336774a2f49684f6e69627a3941642b6e63686c426e4e3351655733434b48717a61523138766f717674566d320a6b4a66484b31357072482f7353476d786d6945476772434a545a78744462614e434f372f56426a6e4b756455554968434177734c747571302f7a7562397641640a384731736366497076357161534e7a6d4b6f5838624f77417276725336775037794b726354737557496c484438724a5649374945446e516f5470354738664b310a68774a2f4d4968384d35763072356455594576366f494a5747636c65364148314a6d73503557496166677137325a32323838704863434648774e59384467394a0a3736517377564c6e556850546c6d6d33454f4f50474574616d32694144357230416679746c62346c624e6f51736a32737a65584f4e4458422b366f7565616a680a564e454c55723848635350356c677a525a6a4a57366146497a6a394c44526d516e55414f6a475358564f517445774a2f4d43515a374e2f763464494b654452410a3864385545785a332b674748756d7a697a7447524a30745172795a483250616b50354937562b316c377145556e4a3263336d462b65317634314570394c4376680a627a72504b773964786831386734622b37624d707357506e7372614b6836697078633761614f615a5630447867657a347a635a753050316f6c4f30634e334b4d0a6e784a305064733352386241684e43446453324a5a61527035513d3d0a2d2d2d2d2d454e44205253412050524956415445204b45592d2d2d2d2d0a',
'public': '2d2d2d2d2d424547494e20525341205055424c4943204b45592d2d2d2d2d0a4d49494242674b422f6779376d6a615767506546645659445a5752434139424e69763370506230657332372b464b593068737a4c614f773437457843744157700a4473483438545841667948425977424c67756179666b344c4749757078622b43474d62526f337845703043626659314a62793236543976476a524331666f48440a44554a4738347561526279487161663469367a74346756522b786c4145496a6b614641414b38634f6f58415431435671474c4c6c6a554363684c38506a61486a0a2f7972695a2f53377264776c49334c6e41427877776d4c726d522f7637315774706d4f2f614e47384e2b31706f2b5177616768546b79513539452f5a7641754f0a6b4657486f6b32712f523650594161326a645a397a696d3046714f502b6e6b5161454452624246426d4271547635664647666b32577341664b662f5247302f560a46642b5a654d353235315465547658483639356e6c53476175566c3941674d424141453d0a2d2d2d2d2d454e4420525341205055424c4943204b45592d2d2d2d2d0a'
};
var ec = {
const ec = {
'private': '2d2d2d2d2d424547494e2045432050524956415445204b45592d2d2d2d2d0a4d485143415145454944463658763853762f2f77475557442b6337383070704772553051645a5743417a78415150515838722f756f416347425375424241414b0a6f55514451674145495a656f7744796c6c73344b2f7766426a4f313862596f37674778386e595152696a6134652f71454d696b4f484a616937676565557265550a7235586b792f4178377332644774656773504e7350674765354d705176673d3d0a2d2d2d2d2d454e442045432050524956415445204b45592d2d2d2d2d0a',
'public': '2d2d2d2d2d424547494e205055424c4943204b45592d2d2d2d2d0a4d465977454159484b6f5a497a6a3043415159464b34454541416f4451674145495a656f7744796c6c73344b2f7766426a4f313862596f37674778386e5951520a696a6134652f71454d696b4f484a616937676565557265557235586b792f4178377332644774656773504e7350674765354d705176673d3d0a2d2d2d2d2d454e44205055424c4943204b45592d2d2d2d2d0a'
};
@ -20,38 +20,38 @@ ec['private'] = new Buffer(ec['private'], 'hex');
ec['public'] = new Buffer(ec['public'], 'hex');
function testit(keys, message, scheme) {
var pub = keys['public'];
var priv = keys['private'];
test(message.toString(), { skip: true }, function (t) {
const pub = keys['public'];
const priv = keys['private'];
test(message.toString(), function (t) {
t.test('js sign and verify', function (st) {
st.plan(1);
var mySign = ourCrypto.createSign(scheme);
var mySig = mySign.update(message).sign(priv);
var myVer = ourCrypto.createVerify(scheme);
const mySign = ourCrypto.createSign(scheme);
const mySig = mySign.update(message).sign(priv);
const myVer = ourCrypto.createVerify(scheme);
st.ok(myVer.update(message).verify(pub, mySig), 'validates');
});
t.test('node sign and verify', function (st) {
st.plan(1);
var mySign = nodeCrypto.createSign(scheme);
var mySig = mySign.update(message).sign(priv);
var myVer = nodeCrypto.createVerify(scheme);
const mySign = nodeCrypto.createSign(scheme);
const mySig = mySign.update(message).sign(priv);
const myVer = nodeCrypto.createVerify(scheme);
st.ok(myVer.update(message).verify(pub, mySig), 'validates');
});
t.test('node sign and js verify', function (st) {
st.plan(1);
var mySign = nodeCrypto.createSign(scheme);
var mySig = mySign.update(message).sign(priv);
var myVer = ourCrypto.createVerify(scheme);
const mySign = nodeCrypto.createSign(scheme);
const mySig = mySign.update(message).sign(priv);
const myVer = ourCrypto.createVerify(scheme);
st.ok(myVer.update(message).verify(pub, mySig), 'validates');
});
t.test('js sign and node verify', function (st) {
st.plan(1);
var mySign = ourCrypto.createSign(scheme);
var mySig = mySign.update(message).sign(priv);
var myVer = nodeCrypto.createVerify(scheme);
const mySign = ourCrypto.createSign(scheme);
const mySig = mySign.update(message).sign(priv);
const myVer = nodeCrypto.createVerify(scheme);
st.ok(myVer.update(message).verify(pub, mySig), 'validates');
});
});