public final class ScramFunctions extends Object
| Modifier and Type | Method and Description |
|---|---|
static String |
authMessage(ClientFirstMessage clientFirstMessage,
ServerFirstMessage serverFirstMessage,
byte[] cbindData)
The AuthMessage is computed by concatenating messages from the authentication exchange.
|
static byte[] |
clientKey(@NotNull ScramMechanism scramMechanism,
byte[] saltedPassword)
Generates a client key, from the salted password.
|
static byte[] |
clientProof(byte[] clientKey,
byte[] clientSignature)
Computes the SCRAM client proof to be sent to the server on the client-final-message.
|
static byte[] |
clientSignature(@NotNull ScramMechanism scramMechanism,
byte[] storedKey,
@NotNull String authMessage)
Computes the SCRAM client signature.
|
static byte[] |
hash(@NotNull ScramMechanism scramMechanism,
byte[] message)
Computes the hash function of a given value, based on the SCRAM mechanism hash function.
|
static byte[] |
hmac(@NotNull ScramMechanism scramMechanism,
byte[] key,
byte[] message)
Computes the HMAC of the message and key, using the given SCRAM mechanism.
|
static String |
nonce(int nonceSize,
SecureRandom random)
Generates a random string (called a 'nonce'), composed of ASCII printable characters, except
comma (',').
|
static byte[] |
salt(int saltSize,
@NotNull SecureRandom random)
Generates a random salt that can be used to generate a salted password.
|
static byte[] |
saltedPassword(@NotNull ScramMechanism scramMechanism,
@NotNull StringPreparation stringPreparation,
char[] password,
byte[] salt,
int iterationCount)
Compute the salted password, based on the given SCRAM mechanism, the String preparation
algorithm, the provided salt and the number of iterations.
|
static byte[] |
serverKey(@NotNull ScramMechanism scramMechanism,
byte[] saltedPassword)
Generates a server key, from the salted password.
|
static byte[] |
serverSignature(@NotNull ScramMechanism scramMechanism,
byte[] serverKey,
@NotNull String authMessage)
Compute the SCRAM server signature.
|
static byte[] |
storedKey(@NotNull ScramMechanism scramMechanism,
byte[] clientKey)
Generates a stored key, from the salted password.
|
static boolean |
verifyClientProof(@NotNull ScramMechanism scramMechanism,
byte[] clientProof,
byte[] storedKey,
@NotNull String authMessage)
Verifies that a provided client proof is correct.
|
static boolean |
verifyServerSignature(ScramMechanism scramMechanism,
byte[] serverKey,
String authMessage,
byte[] serverSignature)
Verifies that a provided server proof is correct.
|
public static byte[] saltedPassword(@NotNull
@NotNull ScramMechanism scramMechanism,
@NotNull
@NotNull StringPreparation stringPreparation,
char[] password,
byte[] salt,
int iterationCount)
SaltedPassword := Hi(Normalize(password), salt, i)
scramMechanism - The SCRAM mechanismstringPreparation - The String preparationpassword - The non-salted passwordsalt - The bytes representing the saltiterationCount - The number of iterationspublic static byte[] hmac(@NotNull
@NotNull ScramMechanism scramMechanism,
byte[] key,
byte[] message)
HMAC(key, str)
scramMechanism - The SCRAM mechanismmessage - The message to compute the HMACkey - The key used to initialize the MACpublic static byte[] clientKey(@NotNull
@NotNull ScramMechanism scramMechanism,
byte[] saltedPassword)
ClientKey := HMAC(SaltedPassword, "Client Key")
scramMechanism - The SCRAM mechanismsaltedPassword - The salted passwordpublic static byte[] serverKey(@NotNull
@NotNull ScramMechanism scramMechanism,
byte[] saltedPassword)
ServerKey := HMAC(SaltedPassword, "Server Key")
scramMechanism - The SCRAM mechanismsaltedPassword - The salted passwordpublic static byte[] hash(@NotNull
@NotNull ScramMechanism scramMechanism,
byte[] message)
H(str)
scramMechanism - The SCRAM mechanismmessage - The message to hashpublic static byte[] storedKey(@NotNull
@NotNull ScramMechanism scramMechanism,
byte[] clientKey)
StoredKey := H(ClientKey)
scramMechanism - The SCRAM mechanismclientKey - The client keypublic static byte[] clientSignature(@NotNull
@NotNull ScramMechanism scramMechanism,
byte[] storedKey,
@NotNull
@NotNull String authMessage)
ClientSignature := HMAC(StoredKey, AuthMessage)
scramMechanism - The SCRAM mechanismstoredKey - The stored keyauthMessage - The auth messagepublic static byte[] clientProof(byte[] clientKey,
byte[] clientSignature)
ClientProof := ClientKey XOR ClientSignature
clientKey - The client keyclientSignature - The client signaturepublic static byte[] serverSignature(@NotNull
@NotNull ScramMechanism scramMechanism,
byte[] serverKey,
@NotNull
@NotNull String authMessage)
ServerSignature := HMAC(ServerKey, AuthMessage)
scramMechanism - The SCRAM mechanismserverKey - The server keyauthMessage - The auth messagepublic static boolean verifyClientProof(@NotNull
@NotNull ScramMechanism scramMechanism,
byte[] clientProof,
byte[] storedKey,
@NotNull
@NotNull String authMessage)
scramMechanism - The SCRAM mechanismclientProof - The provided client proofstoredKey - The stored keyauthMessage - The auth messagepublic static boolean verifyServerSignature(ScramMechanism scramMechanism, byte[] serverKey, String authMessage, byte[] serverSignature)
scramMechanism - The SCRAM mechanismserverKey - The server keyauthMessage - The auth messageserverSignature - The provided server signaturepublic static String nonce(int nonceSize, SecureRandom random)
nonceSize - The length of the nonce, in characters/bytesrandom - The SecureRandom to useIllegalArgumentException - if the nonceSize is not positive, or if random is nullpublic static byte[] salt(int saltSize,
@NotNull
@NotNull SecureRandom random)
saltSize - The length of the salt, in bytesrandom - The SecureRandom to useIllegalArgumentException - if the saltSize is not positive, or if random is nullpublic static String authMessage(ClientFirstMessage clientFirstMessage, ServerFirstMessage serverFirstMessage, byte[] cbindData)
AuthMessage := client-first-message-bare + "," +
server-first-message + "," +
client-final-message-without-proof
clientFirstMessage - the ClientFirstMessageserverFirstMessage - the ServerFirstMessagecbindData - the channel binding data, or nullCopyright © 2017–2025 OnGres, Inc. All rights reserved.