feat: migrate Crypto to own BigNumber

This commit is contained in:
nanov 2019-11-07 08:58:52 +02:00
parent 14779088f1
commit 34051f0a9f

View File

@ -2,15 +2,14 @@
const crypto = require('crypto'); const crypto = require('crypto');
const BN = require('bn.js'); const BigNumber = require('../BigNumber.js');
const mgf1 = require('./MGF1'); const mgf1 = require('./MGF1');
const modPow = (base, power, mod) => { const modPow = (base, power, mod) => {
let result = new BN(1); let result = new BigNumber(1);
while (power > 0) { while (power > 0) {
result = power.and(new BN(1)) == 1 ? (result.mul(base)).mod(mod) : result; // eslint-disable-line result = power.and(new BigNumber(1)) == 1 ? (result.mul(base)).mod(mod) : result; // eslint-disable-line
base = (base.mul(base)).mod(mod); base = (base.mul(base)).mod(mod);
power = power.shrn(1); power = power.shrn(1);
} }
@ -28,10 +27,13 @@ const emsaPSS = (msg, salt) => {
const dbMask = mgf1.generate(mTickHash, db.length); const dbMask = mgf1.generate(mTickHash, db.length);
const maskedDb = mgf1.xor(db, dbMask); const maskedDb = mgf1.xor(db, dbMask);
let maskedDbMsb = mgf1.rjust(new BN(maskedDb.slice(0, 1), 2).toString(2), 8, '0'); let maskedDbMsb = mgf1.rjust(BigNumber.fromBuffer(maskedDb.slice(0, 1)).toString(2), 8, '0');
maskedDbMsb = `0${maskedDbMsb.substr(1)}`; maskedDbMsb = `0${maskedDbMsb.substr(1)}`;
maskedDb[0] = (new BN(maskedDbMsb, 2).toBuffer())[0]; // eslint-disable-line // console.log((new BN(maskedDbMsb, 2).toBuffer())[0], new BigNumber(maskedDbMsb, 2).toBuffer()[0]);
// maskedDb[0] = (new BN(maskedDbMsb, 2).toBuffer())[0]; // eslint-disable-line
maskedDb[0] = new BigNumber(maskedDbMsb, 2).toBuffer()[0]; // eslint-disable-line
return Buffer.concat([maskedDb, mTickHash, Buffer.from('BC', 'hex')]); return Buffer.concat([maskedDb, mTickHash, Buffer.from('BC', 'hex')]);
}; };
@ -65,9 +67,10 @@ module.exports = class Crypto {
} }
static sign(key, msg, salt = crypto.randomBytes(32)) { static sign(key, msg, salt = crypto.randomBytes(32)) {
const base = new BN(emsaPSS(msg, salt)); // console.log(key.d());
const power = new BN(key.d()); const base = new BigNumber(emsaPSS(msg, salt));
const mod = new BN(key.n()); const power = new BigNumber(key.d());
const mod = new BigNumber(key.n());
return (modPow(base, power, mod)).toBuffer().toString('base64'); return (modPow(base, power, mod)).toBuffer().toString('base64');
} }