Package com.ongres.scram.common
Enum ScramMechanisms
- java.lang.Object
-
- java.lang.Enum<ScramMechanisms>
-
- com.ongres.scram.common.ScramMechanisms
-
- All Implemented Interfaces:
ScramMechanism,java.io.Serializable,java.lang.Comparable<ScramMechanisms>
public enum ScramMechanisms extends java.lang.Enum<ScramMechanisms> implements ScramMechanism
SCRAM Mechanisms supported by this library. At least, SCRAM-SHA-1 and SCRAM-SHA-256 are provided, since both the hash and the HMAC implementations are provided by the Java JDK version 6 or greater.MessageDigest: "Every implementation of the Java platform is required to support the following standard MessageDigest algorithms: MD5, SHA-1, SHA-256".Mac: "Every implementation of the Java platform is required to support the following standard Mac algorithms: HmacMD5, HmacSHA1, HmacSHA256".- See Also:
- SASL SCRAM Family Mechanisms
-
-
Enum Constant Summary
Enum Constants Enum Constant Description SCRAM_SHA_1SCRAM_SHA_1_PLUSSCRAM_SHA_256SCRAM_SHA_256_PLUS
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<java.lang.String,ScramMechanisms>BY_NAME_MAPPINGprivate static java.lang.StringCHANNEL_BINDING_SUFFIXprivate booleanchannelBindingprivate java.lang.StringhashAlgorithmNameprivate java.lang.StringhmacAlgorithmNameprivate intkeyLengthprivate java.lang.StringmechanismNameprivate static java.lang.StringPBKDF2_PREFIX_ALGORITHM_NAMEprivate intpriorityprivate static java.lang.StringSCRAM_MECHANISM_NAME_PREFIX
-
Constructor Summary
Constructors Modifier Constructor Description privateScramMechanisms(java.lang.String name, java.lang.String hashAlgorithmName, int keyLength, java.lang.String hmacAlgorithmName, boolean channelBinding, int priority)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intalgorithmKeyLength()Returns the length of the key length of the algorithm.static java.util.Optional<ScramMechanisms>byName(java.lang.String name)Gets a SCRAM mechanism, given its standard IANA name.protected java.lang.StringgetHashAlgorithmName()Method that returns the name of the hash algorithm.protected java.lang.StringgetHmacAlgorithmName()Method that returns the name of the HMAC algorithm.javax.crypto.MacgetMacInstance()Gets a constructedMacinstance, according to the algorithm of the SCRAM mechanism.java.security.MessageDigestgetMessageDigestInstance()Gets a constructedMessageDigestinstance, according to the algorithm of the SCRAM mechanism.java.lang.StringgetName()The name of the mechanism, which must be a value registered under IANA: SASL SCRAM Family Mechanismsjavax.crypto.SecretKeyFactorysecretKeyFactory()Gets a SecretKeyFactory for the given algorithm.javax.crypto.spec.SecretKeySpecsecretKeySpec(byte[] key)Generates a key of the algorith used, based on the key given.static java.util.Optional<ScramMechanism>selectMatchingMechanism(boolean channelBinding, java.lang.String... peerMechanisms)This class classifies SCRAM mechanisms by two properties: whether they support channel binding; and a priority, which is higher for safer algorithms (like SHA-256 vs SHA-1).booleansupportsChannelBinding()Whether this mechanism supports channel bindingstatic ScramMechanismsvalueOf(java.lang.String name)Returns the enum constant of this type with the specified name.static ScramMechanisms[]values()Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Enum Constant Detail
-
SCRAM_SHA_1
public static final ScramMechanisms SCRAM_SHA_1
-
SCRAM_SHA_1_PLUS
public static final ScramMechanisms SCRAM_SHA_1_PLUS
-
SCRAM_SHA_256
public static final ScramMechanisms SCRAM_SHA_256
-
SCRAM_SHA_256_PLUS
public static final ScramMechanisms SCRAM_SHA_256_PLUS
-
-
Field Detail
-
SCRAM_MECHANISM_NAME_PREFIX
private static final java.lang.String SCRAM_MECHANISM_NAME_PREFIX
- See Also:
- Constant Field Values
-
CHANNEL_BINDING_SUFFIX
private static final java.lang.String CHANNEL_BINDING_SUFFIX
- See Also:
- Constant Field Values
-
PBKDF2_PREFIX_ALGORITHM_NAME
private static final java.lang.String PBKDF2_PREFIX_ALGORITHM_NAME
- See Also:
- Constant Field Values
-
BY_NAME_MAPPING
private static final java.util.Map<java.lang.String,ScramMechanisms> BY_NAME_MAPPING
-
mechanismName
private final java.lang.String mechanismName
-
hashAlgorithmName
private final java.lang.String hashAlgorithmName
-
keyLength
private final int keyLength
-
hmacAlgorithmName
private final java.lang.String hmacAlgorithmName
-
channelBinding
private final boolean channelBinding
-
priority
private final int priority
-
-
Method Detail
-
values
public static ScramMechanisms[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (ScramMechanisms c : ScramMechanisms.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static ScramMechanisms valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
java.lang.IllegalArgumentException- if this enum type has no constant with the specified namejava.lang.NullPointerException- if the argument is null
-
getHashAlgorithmName
protected java.lang.String getHashAlgorithmName()
Method that returns the name of the hash algorithm. It is protected since should be of no interest for direct users. The instance is supposed to provide abstractions over the algorithm names, and are not meant to be directly exposed.- Returns:
- The name of the hash algorithm
-
getHmacAlgorithmName
protected java.lang.String getHmacAlgorithmName()
Method that returns the name of the HMAC algorithm. It is protected since should be of no interest for direct users. The instance is supposed to provide abstractions over the algorithm names, and are not meant to be directly exposed.- Returns:
- The name of the HMAC algorithm
-
getName
public java.lang.String getName()
Description copied from interface:ScramMechanismThe name of the mechanism, which must be a value registered under IANA: SASL SCRAM Family Mechanisms- Specified by:
getNamein interfaceScramMechanism- Returns:
- The mechanism name
-
supportsChannelBinding
public boolean supportsChannelBinding()
Description copied from interface:ScramMechanismWhether this mechanism supports channel binding- Specified by:
supportsChannelBindingin interfaceScramMechanism- Returns:
- True if it supports channel binding, false otherwise
-
getMessageDigestInstance
public java.security.MessageDigest getMessageDigestInstance()
Description copied from interface:ScramMechanismGets a constructedMessageDigestinstance, according to the algorithm of the SCRAM mechanism.- Specified by:
getMessageDigestInstancein interfaceScramMechanism- Returns:
- The MessageDigest instance
-
getMacInstance
public javax.crypto.Mac getMacInstance()
Description copied from interface:ScramMechanismGets a constructedMacinstance, according to the algorithm of the SCRAM mechanism.- Specified by:
getMacInstancein interfaceScramMechanism- Returns:
- The Mac instance
-
secretKeySpec
public javax.crypto.spec.SecretKeySpec secretKeySpec(byte[] key)
Description copied from interface:ScramMechanismGenerates a key of the algorith used, based on the key given.- Specified by:
secretKeySpecin interfaceScramMechanism- Parameters:
key- The bytes of the key to use- Returns:
- The instance of SecretKeySpec
-
secretKeyFactory
public javax.crypto.SecretKeyFactory secretKeyFactory()
Description copied from interface:ScramMechanismGets a SecretKeyFactory for the given algorithm.- Specified by:
secretKeyFactoryin interfaceScramMechanism- Returns:
- The SecretKeyFactory
-
algorithmKeyLength
public int algorithmKeyLength()
Description copied from interface:ScramMechanismReturns the length of the key length of the algorithm.- Specified by:
algorithmKeyLengthin interfaceScramMechanism- Returns:
- The length (in bits)
-
byName
public static java.util.Optional<ScramMechanisms> byName(java.lang.String name)
Gets a SCRAM mechanism, given its standard IANA name.- Parameters:
name- The standard IANA full name of the mechanism.- Returns:
- An Optional instance that contains the ScramMechanism if it was found, or empty otherwise.
-
selectMatchingMechanism
public static java.util.Optional<ScramMechanism> selectMatchingMechanism(boolean channelBinding, java.lang.String... peerMechanisms)
This class classifies SCRAM mechanisms by two properties: whether they support channel binding; and a priority, which is higher for safer algorithms (like SHA-256 vs SHA-1). Given a list of SCRAM mechanisms supported by the peer, pick one that matches the channel binding requirements and has the highest priority.- Parameters:
channelBinding- The type of matching mechanism searched forpeerMechanisms- The mechanisms supported by the other peer- Returns:
- The selected mechanism, or null if no mechanism matched
-
-