Package com.ongres.scram.common
Class CryptoUtil
java.lang.Object
com.ongres.scram.common.CryptoUtil
Utility static methods for cryptography related tasks.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intThe interval at which the PBKDF2 loop checks for thread interruption. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static byte[]Compute the "Hi" function for SCRAM.(package private) static byte[]hmac(SecretKeySpec secretKeySpec, Mac mac, byte[] message) Computes the HMAC of a given message.private static byte[]passwordToUtf8Bytes(char[] password) Convert password to UTF-8 bytes and secure clear the backing array.(package private) static byte[]salt(int saltSize, SecureRandom random) Generates a random salt.(package private) static byte[]xor(byte[] value1, byte[] value2) Computes a byte-by-byte xor operation.
-
Field Details
-
INTERRUPT_CHECK_STRIDE
private static final int INTERRUPT_CHECK_STRIDEThe interval at which the PBKDF2 loop checks for thread interruption.Checking the thread state via
Thread.isInterrupted()on every iteration requires a native JVM call, which significantly degrades hashing throughput. This stride value batches iterations, allowing the loop to execute rapidly while remaining responsive to shutdown signals.Note: This value MUST be a power of two. This allows the compiler to use a highly optimized bitwise AND operation
(i & (STRIDE - 1))rather than a slower modulo operator.- See Also:
-
-
Constructor Details
-
CryptoUtil
private CryptoUtil()
-
-
Method Details
-
hi
Compute the "Hi" function for SCRAM.Hi(str, salt, i): U1 := HMAC(str, salt + INT(1)) U2 := HMAC(str, U1) ... Ui-1 := HMAC(str, Ui-2) Ui := HMAC(str, Ui-1) Hi := U1 XOR U2 XOR ... XOR Ui where "i" is the iteration count, "+" is the string concatenation operator, and INT(g) is a 4-octet encoding of the integer g, most significant octet first. Hi() is, essentially, PBKDF2 [RFC2898] with HMAC() as the pseudorandom function (PRF) and with dkLen == output length of HMAC() == output length of H().- Parameters:
mac- The Mac instance to usepassword- The char array to compute the Hi functionsalt- The saltiterationCount- The number of iterations- Returns:
- The bytes of the computed Hi value
- Throws:
ScramRuntimeException- if unsupported key for Mac algorithm, or if thread is interrupted
-
passwordToUtf8Bytes
private static byte[] passwordToUtf8Bytes(char[] password) Convert password to UTF-8 bytes and secure clear the backing array.- Parameters:
password- The password to convert- Returns:
- The UTF-8 bytes of the password
-
hmac
Computes the HMAC of a given message.HMAC(key, str): Apply the HMAC keyed hash algorithm (defined in [RFC2104]) using the octet string represented by "key" as the key and the octet string "str" as the input string. The size of the result is the hash result size for the hash function in use. For example, it is 20 octets for SHA-1 (see [RFC3174]).- Parameters:
secretKeySpec- A key of the given algorithmmac- A MAC instance of the given algorithmmessage- The message to compute the HMAC- Returns:
- The bytes of the computed HMAC value
- Throws:
ScramRuntimeException- unsupported key for HMAC algorithm
-
xor
static byte[] xor(byte[] value1, byte[] value2) Computes a byte-by-byte xor operation.XOR: Apply the exclusive-or operation to combine the octet string on the left of this operator with the octet string on the right of this operator. The length of the output and each of the two inputs will be the same for this use.- Parameters:
value1- first value to apply xorvalue2- second value to apply xor- Returns:
- xor operation
-
salt
Generates a random salt. Normally the output is encoded to Base64.- Parameters:
saltSize- The length of the salt, in bytesrandom- The SecureRandom to use- Returns:
- The bye[] representing the salt
- Throws:
IllegalArgumentException- if the saltSize is not positive, or if random is null
-