Skip to content
Snippets Groups Projects
Commit 8ffbb94a authored by Ben Adida's avatar Ben Adida
Browse files

updated random to not use java

parent f06622d4
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,7 @@ Random = {};
Random.GENERATOR = null;
Random.setupGenerator = function() {
if (Random.GENERATOR == null && !USE_SJCL) {
/* if (Random.GENERATOR == null && !USE_SJCL) {
if (BigInt.use_applet) {
var foo = BigInt.APPLET.newSecureRandom();
Random.GENERATOR = BigInt.APPLET.newSecureRandom();
......@@ -18,37 +18,17 @@ Random.setupGenerator = function() {
Random.GENERATOR = new java.security.SecureRandom();
}
}
*/
};
Random.getRandomInteger = function(max) {
var bit_length = max.bitLength();
Random.setupGenerator();
var random;
if (USE_SJCL) {
random = sjcl.random.randomWords(bit_length / 32, 0);
// we get a bit array instead of a BigInteger in this case
var rand_bi = new BigInt(sjcl.codec.hex.fromBits(random), 16);
return rand_bi.mod(max);
} else if (BigInt.use_applet) {
random = BigInt.APPLET.randomBigInteger(bit_length, Random.GENERATOR);
} else {
random = new java.math.BigInteger(bit_length, Random.GENERATOR);
}
return BigInt._from_java_object(random).mod(max);
};
Random.getRandomPrime = function(n_bits) {
Random.setupGenerator();
var certainty = 80;
var prime;
if (USE_SJCL) {
throw "getRandomPrime not implemented for SJCL";
} else if (BigInt.use_applet) {
prime = BigInt.APPLET.randomPrimeBigInteger(n_bits, certainty, Random.GENERATOR);
} else {
prime = new java.math.BigInteger(n_bits, certainty, Random.GENERATOR);
}
return BigInt._from_java_object(prime);
};
......@@ -8,7 +8,8 @@ Random = {};
Random.GENERATOR = null;
Random.setupGenerator = function() {
if (Random.GENERATOR == null && !USE_SJCL) {
// no longer needed
/* if (Random.GENERATOR == null && !USE_SJCL) {
if (BigInt.use_applet) {
var foo = BigInt.APPLET.newSecureRandom();
Random.GENERATOR = BigInt.APPLET.newSecureRandom();
......@@ -18,37 +19,17 @@ Random.setupGenerator = function() {
Random.GENERATOR = new java.security.SecureRandom();
}
}
*/
};
Random.getRandomInteger = function(max) {
var bit_length = max.bitLength();
Random.setupGenerator();
var random;
if (USE_SJCL) {
random = sjcl.random.randomWords(bit_length / 32, 0);
// we get a bit array instead of a BigInteger in this case
var rand_bi = new BigInt(sjcl.codec.hex.fromBits(random), 16);
return rand_bi.mod(max);
} else if (BigInt.use_applet) {
random = BigInt.APPLET.randomBigInteger(bit_length, Random.GENERATOR);
} else {
random = new java.math.BigInteger(bit_length, Random.GENERATOR);
}
return BigInt._from_java_object(random).mod(max);
};
Random.getRandomPrime = function(n_bits) {
Random.setupGenerator();
var certainty = 80;
var prime;
if (USE_SJCL) {
throw "getRandomPrime not implemented for SJCL";
} else if (BigInt.use_applet) {
prime = BigInt.APPLET.randomPrimeBigInteger(n_bits, certainty, Random.GENERATOR);
} else {
prime = new java.math.BigInteger(n_bits, certainty, Random.GENERATOR);
}
return BigInt._from_java_object(prime);
};
......@@ -8,7 +8,7 @@ Random = {};
Random.GENERATOR = null;
Random.setupGenerator = function() {
if (Random.GENERATOR == null && !USE_SJCL) {
/* if (Random.GENERATOR == null && !USE_SJCL) {
if (BigInt.use_applet) {
var foo = BigInt.APPLET.newSecureRandom();
Random.GENERATOR = BigInt.APPLET.newSecureRandom();
......@@ -18,37 +18,17 @@ Random.setupGenerator = function() {
Random.GENERATOR = new java.security.SecureRandom();
}
}
*/
};
Random.getRandomInteger = function(max) {
var bit_length = max.bitLength();
Random.setupGenerator();
var random;
if (USE_SJCL) {
random = sjcl.random.randomWords(bit_length / 32, 0);
// we get a bit array instead of a BigInteger in this case
var rand_bi = new BigInt(sjcl.codec.hex.fromBits(random), 16);
return rand_bi.mod(max);
} else if (BigInt.use_applet) {
random = BigInt.APPLET.randomBigInteger(bit_length, Random.GENERATOR);
} else {
random = new java.math.BigInteger(bit_length, Random.GENERATOR);
}
return BigInt._from_java_object(random).mod(max);
};
Random.getRandomPrime = function(n_bits) {
Random.setupGenerator();
var certainty = 80;
var prime;
if (USE_SJCL) {
throw "getRandomPrime not implemented for SJCL";
} else if (BigInt.use_applet) {
prime = BigInt.APPLET.randomPrimeBigInteger(n_bits, certainty, Random.GENERATOR);
} else {
prime = new java.math.BigInteger(n_bits, certainty, Random.GENERATOR);
}
return BigInt._from_java_object(prime);
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment