Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const genSalt = (): bigInt.BigInteger => {
const buf = crypto.randomBytes(54)
const salt = bigInt.fromArray(Array.from(buf), 256, false).minus(340)
// 4 * (4^3) + 4 * (4^2) + 4 * (4^1) + 4 * (4^0) = 340
// Only return values greater than the largest possible solution
if (salt.lt(340)) {
return genSalt()
}
return salt
}
public static fromUIntBytes(bytes : number[]) : LongInt {
return new LongInt(bigInt.fromArray(bytes, 256, false));
}
public static fromVarUIntBytes(bytes : number[]) : LongInt {
return new LongInt(bigInt.fromArray(bytes, 128, false));
}
function sig2bigint(sig, signed) {
const sign = signed && sig.slice(-1)[0] == 1;
const j = bigInt.fromArray(sig.slice().reverse().map(x => (x+1)>>1), 2);
return sign ? j.minus(bigInt.one.shiftLeft(sig.length)) : j;
}
public static fromVarIntBytes(bytes : number[], isNegative : boolean) : LongInt {
return new LongInt(bigInt.fromArray(bytes, 128, isNegative));
}
toBigint: function () {
if (this.type == "biguint") {
return Bigint.fromArray(this.data, 256);
} else {
var val = Bigint.fromArray(this.data, 256);
var tmp = this.data[0];
if (tmp > 0x80) {
switch (this.data.length) {
case 16:
return val.subtract(MaxUint128Plus1);
case 32:
return val.subtract(MaxUint256Plus1);
default:
throw new Error("invalid array length:" + this.data.length);
}
} else {
return val;
}
}
},
isBytes: function() {