log2 now complains if it is given a number that is not a power of 2.
1 parent 83d3976 commit 0a22e17ccbc39d18f0d259818ef3d0724ec1c158
Mark George authored on 13 Dec 2018
Showing 1 changed file
View
24
src/main/java/helpers/ScryptHelper.java
 
/**
* Wrapper for Bouncy Castle's scrypt implementation.
*
* Generates salted scrypt hashes in a format similar to Modular Crypt Format (MCF).
* Generates salted scrypt hashes in a format similar to Modular Crypt Format
* (MCF).
*
* @author Mark George <mark.george@otago.ac.nz>
*/
public final class ScryptHelper {
return result;
}
 
/**
* Checks a hash (as generated by the hash method) against a
* password.
* Checks a hash (as generated by the hash method) against a password.
*
* @param mcfHash The MCF formatted hash.
* @param password The password.
*
}
}
 
private static int log2(int operand) {
int result = 0;
 
do {
operand = operand >> 1;
result++;
} while (operand > 1);
 
return result;
double log2 = Math.log(operand) / Math.log(2);
if (log2 % 1 != 0) {
throw new IllegalArgumentException("N must be a power of 2.");
} else {
return Math.toIntExact(Math.round(log2));
}
}
 
/*
* Source: https://stackoverflow.com/a/9670279