From 34051f0a9f38f754f1668f68fdba7c16292c05c9 Mon Sep 17 00:00:00 2001 From: nanov Date: Thu, 7 Nov 2019 08:58:52 +0200 Subject: [PATCH] feat: migrate Crypto to own BigNumber --- lib/crypto/Crypto.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/crypto/Crypto.js b/lib/crypto/Crypto.js index 282183d..7a6db0e 100644 --- a/lib/crypto/Crypto.js +++ b/lib/crypto/Crypto.js @@ -2,15 +2,14 @@ const crypto = require('crypto'); -const BN = require('bn.js'); - +const BigNumber = require('../BigNumber.js'); const mgf1 = require('./MGF1'); const modPow = (base, power, mod) => { - let result = new BN(1); + let result = new BigNumber(1); 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); power = power.shrn(1); } @@ -28,10 +27,13 @@ const emsaPSS = (msg, salt) => { const dbMask = mgf1.generate(mTickHash, db.length); 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)}`; - 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')]); }; @@ -65,9 +67,10 @@ module.exports = class Crypto { } static sign(key, msg, salt = crypto.randomBytes(32)) { - const base = new BN(emsaPSS(msg, salt)); - const power = new BN(key.d()); - const mod = new BN(key.n()); + // console.log(key.d()); + const base = new BigNumber(emsaPSS(msg, salt)); + const power = new BigNumber(key.d()); + const mod = new BigNumber(key.n()); return (modPow(base, power, mod)).toBuffer().toString('base64'); }