Begone jsbn.js! I tell you begonegit status
This commit is contained in:
parent
1812166a53
commit
a2868a5c14
|
@ -18,8 +18,8 @@
|
|||
// A Digital signature algorithm implementation
|
||||
|
||||
/**
|
||||
* @requires bn.js
|
||||
* @requires crypto/hash
|
||||
* @requires crypto/public_key/jsbn
|
||||
* @requires crypto/random
|
||||
* @requires config
|
||||
* @requires util
|
||||
|
@ -28,7 +28,7 @@
|
|||
|
||||
import BN from 'bn.js';
|
||||
import hash from '../hash';
|
||||
import random from '../random.js';
|
||||
import random from '../random';
|
||||
import config from '../../config';
|
||||
import util from '../../util';
|
||||
|
||||
|
|
|
@ -18,16 +18,16 @@
|
|||
// Wrapper of an instance of an Elliptic Curve
|
||||
|
||||
/**
|
||||
* @requires bn.js
|
||||
* @requires crypto/public_key/elliptic/key
|
||||
* @requires crypto/public_key/jsbn
|
||||
* @requires enums
|
||||
* @requires util
|
||||
* @module crypto/public_key/elliptic/curve
|
||||
*/
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { ec as EC, eddsa as EdDSA } from 'elliptic';
|
||||
import KeyPair from './key';
|
||||
import BigInteger from '../jsbn';
|
||||
import random from '../../random';
|
||||
import enums from '../../../enums';
|
||||
import util from '../../../util';
|
||||
|
@ -188,8 +188,8 @@ async function generate(curve) {
|
|||
const keyPair = await curve.genKeyPair();
|
||||
return {
|
||||
oid: curve.oid,
|
||||
Q: new BigInteger(util.hexidump(keyPair.getPublic()), 16),
|
||||
d: new BigInteger(util.hexidump(keyPair.getPrivate()), 16),
|
||||
Q: new BN(keyPair.getPublic()),
|
||||
d: new BN(keyPair.getPrivate()),
|
||||
hash: curve.hash,
|
||||
cipher: curve.cipher
|
||||
};
|
||||
|
|
|
@ -18,8 +18,8 @@
|
|||
// Wrapper for a KeyPair of an Elliptic Curve
|
||||
|
||||
/**
|
||||
* @requires bn.js
|
||||
* @requires crypto/public_key/elliptic/curves
|
||||
* @requires crypto/public_key/jsbn
|
||||
* @requires crypto/hash
|
||||
* @requires util
|
||||
* @requires enums
|
||||
|
@ -29,8 +29,8 @@
|
|||
* @module crypto/public_key/elliptic/key
|
||||
*/
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { webCurves, nodeCurves } from './curves';
|
||||
import BigInteger from '../jsbn';
|
||||
import hash from '../../hash';
|
||||
import util from '../../../util';
|
||||
import enums from '../../../enums';
|
||||
|
@ -210,8 +210,7 @@ async function nodeSign(curve, hash_algo, message, keyPair) {
|
|||
}
|
||||
|
||||
async function nodeVerify(curve, hash_algo, { r, s }, message, publicKey) {
|
||||
const signature = ECDSASignature.encode(
|
||||
{ r: new BigInteger(util.hexidump(r), 16), s: new BigInteger(util.hexidump(s), 16) }, 'der');
|
||||
const signature = ECDSASignature.encode({ r: new BN(r), s: new BN(s) }, 'der');
|
||||
const key = jwkToPem(
|
||||
{
|
||||
"kty": "EC",
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -20,7 +20,6 @@
|
|||
/**
|
||||
* @requires bn.js
|
||||
* @requires asmcrypto.js
|
||||
* @requires crypto/public_key/jsbn
|
||||
* @requires crypto/random
|
||||
* @requires config
|
||||
* @requires util
|
||||
|
|
|
@ -96,40 +96,6 @@ export default {
|
|||
return buf;
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a secure random big integer of bits length
|
||||
* @param {Integer} bits Bit length of the MPI to create
|
||||
* @return {BigInteger} Resulting big integer
|
||||
*/
|
||||
getRandomBigInteger: function(bits) {
|
||||
if (bits < 1) {
|
||||
throw new Error('Illegal parameter value: bits < 1');
|
||||
}
|
||||
const numBytes = Math.floor((bits + 7) / 8);
|
||||
|
||||
let randomBits = util.Uint8Array2str(this.getRandomBytes(numBytes));
|
||||
if (bits % 8 > 0) {
|
||||
randomBits = String.fromCharCode(
|
||||
((2 ** (bits % 8)) - 1) & randomBits.charCodeAt(0)
|
||||
) + randomBits.substring(1);
|
||||
}
|
||||
const mpi = new type_mpi(randomBits);
|
||||
return mpi.toBigInteger();
|
||||
},
|
||||
|
||||
getRandomBigIntegerInRange: function(min, max) {
|
||||
if (max.compareTo(min) <= 0) {
|
||||
throw new Error('Illegal parameter value: max <= min');
|
||||
}
|
||||
|
||||
const range = max.subtract(min);
|
||||
let r = this.getRandomBigInteger(range.bitLength());
|
||||
while (r.compareTo(range) > 0) {
|
||||
r = this.getRandomBigInteger(range.bitLength());
|
||||
}
|
||||
return min.add(r);
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a secure random MPI in specified range
|
||||
* @param {module:type/mpi} min Lower bound, included
|
||||
|
|
|
@ -30,13 +30,11 @@
|
|||
* of the MPI in bits followed by a string of octets that contain the
|
||||
* actual integer.
|
||||
* @requires bn.js
|
||||
* @requires crypto/public_key/jsbn
|
||||
* @requires util
|
||||
* @module type/mpi
|
||||
*/
|
||||
|
||||
import BN from 'bn.js';
|
||||
import BigInteger from '../crypto/public_key/jsbn';
|
||||
import util from '../util';
|
||||
|
||||
/**
|
||||
|
@ -46,8 +44,6 @@ export default function MPI(data) {
|
|||
/** An implementation dependent integer */
|
||||
if (data instanceof BN) {
|
||||
this.fromBN(data);
|
||||
} else if (data instanceof BigInteger) {
|
||||
this.fromBigInteger(data);
|
||||
} else if (util.isUint8Array(data)) {
|
||||
this.fromUint8Array(data);
|
||||
} else if (util.isString(data)) {
|
||||
|
@ -133,14 +129,6 @@ MPI.prototype.fromBN = function (bn) {
|
|||
this.data = bn.clone();
|
||||
};
|
||||
|
||||
MPI.prototype.toBigInteger = function () {
|
||||
return new BigInteger(util.hexidump(this.write()), 16);
|
||||
};
|
||||
|
||||
MPI.prototype.fromBigInteger = function (bn) {
|
||||
this.data = new BN(bn.toByteArray());
|
||||
};
|
||||
|
||||
MPI.fromClone = function (clone) {
|
||||
const bn = new BN();
|
||||
clone.data.copy(bn);
|
||||
|
|
49
src/util.js
49
src/util.js
|
@ -37,21 +37,6 @@ export default {
|
|||
return Uint8Array.prototype.isPrototypeOf(data);
|
||||
},
|
||||
|
||||
isEmailAddress: function(data) {
|
||||
if (!this.isString(data)) {
|
||||
return false;
|
||||
}
|
||||
const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+([a-zA-Z]{2,}|xn--[a-zA-Z\-0-9]+)))$/;
|
||||
return re.test(data);
|
||||
},
|
||||
|
||||
isUserId: function(data) {
|
||||
if (!this.isString(data)) {
|
||||
return false;
|
||||
}
|
||||
return /</.test(data) && />$/.test(data);
|
||||
},
|
||||
|
||||
/**
|
||||
* Get transferable objects to pass buffers with zero copy (similar to "pass by reference" in C++)
|
||||
* See: https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage
|
||||
|
@ -539,25 +524,6 @@ export default {
|
|||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts an IE11 web crypto api result to a promise.
|
||||
* This is required since IE11 implements an old version of the
|
||||
* Web Crypto specification that does not use promises.
|
||||
* @param {Object} cryptoOp The return value of an IE11 web cryptro api call
|
||||
* @param {String} errmsg An error message for a specific operation
|
||||
* @return {Promise} The resulting Promise
|
||||
*/
|
||||
promisifyIE11Op: function(cryptoOp, errmsg) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
cryptoOp.onerror = function () {
|
||||
reject(new Error(errmsg));
|
||||
};
|
||||
cryptoOp.oncomplete = function (e) {
|
||||
resolve(e.target.result);
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Detect Node.js runtime.
|
||||
*/
|
||||
|
@ -600,5 +566,20 @@ export default {
|
|||
}
|
||||
|
||||
return require('zlib');
|
||||
},
|
||||
|
||||
isEmailAddress: function(data) {
|
||||
if (!this.isString(data)) {
|
||||
return false;
|
||||
}
|
||||
const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+([a-zA-Z]{2,}|xn--[a-zA-Z\-0-9]+)))$/;
|
||||
return re.test(data);
|
||||
},
|
||||
|
||||
isUserId: function(data) {
|
||||
if (!this.isString(data)) {
|
||||
return false;
|
||||
}
|
||||
return /</.test(data) && />$/.test(data);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user