node-ebics-client/lib/crypto/BigNumber.js

67 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2019-11-07 08:27:08 +00:00
'use strict';
const { jsbn: { BigInteger } } = require('node-forge');
class BigNumber {
constructor(value, radix = 10) {
if (value === null || value === undefined)
throw new Error('value is missing.');
this._n = new BigInteger(null);
if (value instanceof BigNumber)
this._n = value._n;
else if (value instanceof BigInteger)
this._n = value;
else if (typeof value === 'number')
this._n.fromInt(value);
else if (typeof value === 'string')
this._n.fromString(value, radix);
else if (Buffer.isBuffer(value))
this._n.fromString(value.toString('hex'), 16);
else if (Array.isArray(value))
this._n.fromString(Buffer.from(value).toString('hex'), 16);
else
throw new TypeError('Unsupported value type.');
}
2019-11-07 08:30:03 +00:00
toBEBuffer(length) {
2019-11-07 08:27:08 +00:00
const arr = this._n.toByteArray();
2019-11-07 08:30:03 +00:00
if (length === undefined)
return Buffer.from(arr);
if (arr.length > length)
2019-11-07 08:27:08 +00:00
throw new Error('Number out of range.');
2019-11-07 08:30:03 +00:00
while (arr.length < length)
2019-11-07 08:27:08 +00:00
arr.unshift(0);
2019-11-07 08:30:03 +00:00
return Buffer.from(arr);
2019-11-07 08:27:08 +00:00
}
toString(radix = 10) {
const result = this._n.toString(radix);
if (radix === 16)
return result.padStart(2, '0');
return result;
}
and(num) {
return new BigNumber(this._n.and(new BigNumber(num)._n));
}
mul(num) {
return new BigNumber(this._n.multiply(new BigNumber(num)._n));
}
mod(num) {
return new BigNumber(this._n.mod(new BigNumber(num)._n));
}
shrn(num) {
return new BigNumber(this._n.shiftRight(new BigNumber(num)._n));
}
}
module.exports = BigNumber;