Class Sha2Crypt
Based on the C implementation released into the Public Domain by Ulrich Drepper <drepper@redhat.com> http://www.akkadia.org/drepper/SHA-crypt.txt
Conversion to Kotlin and from there to Java in 2012 by Christian Hammers <ch@lathspell.de> and likewise put into the Public Domain.
This class is immutable and thread-safe.
SHA-crypt hashing has a quadratic input-length step. To bound CPU and memory consumption when plaintext is supplied by an
untrusted caller, plaintext is limited to 4096 bytes by default. The limit can be changed with the
org.apache.commons.codec.digest.Sha2Crypt.keyMax system property; this property is intended for trusted JVM
configuration only.
- Since:
- 1.7
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intDefault maximum plaintext (key) length in bytes.(package private) static final StringSystem property used to override the default maximum plaintext (key) length.private static final intDefault number of rounds if not explicitly specified.private static final intMaximum number of rounds.private static final intDefault maximum number of rounds accepted from a caller-supplied salt string.(package private) static final StringSystem property used to override the default maximum number of rounds.private static final intMinimum number of rounds.private static final StringPrefix for optional rounds specification.private static final PatternThe pattern to match valid salt values.private static final intThe number of bytes the final hash value will have (SHA-256 variant).(package private) static final StringThe prefixes that can be used to identify this crypt() variant (SHA-256).private static final intThe number of bytes the final hash value will have (SHA-512 variant).(package private) static final StringThe prefixes that can be used to identify this crypt() variant (SHA-512). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static intfirstNonZeroIndex(String value) Finds the first non-zero digit, retaining one zero for an all-zero value.private static intGets the maximum plaintext (key) length in bytes, as configured by theorg.apache.commons.codec.digest.Sha2Crypt.keyMaxsystem property.private static intGets the maximum number of rounds accepted from a caller-supplied salt string, as configured by theorg.apache.commons.codec.digest.Sha2Crypt.roundsMaxsystem property.static Stringsha256Crypt(byte[] keyBytes) Generates a libc crypt() compatible "$5$" hash value with random salt.static Stringsha256Crypt(byte[] keyBytes, String salt) Generates a libc6 crypt() compatible "$5$" hash value.static Stringsha256Crypt(byte[] keyBytes, String salt, Random random) Generates a libc6 crypt() compatible "$5$" hash value.private static Stringsha2Crypt(byte[] keyBytes, String salt, String saltPrefix, int blocksize, String algorithm, int maxKeyLen, int maxRounds) Generates a libc6 crypt() compatible "$5$" or "$6$" SHA2 based hash value.static Stringsha512Crypt(byte[] keyBytes) Generates a libc crypt() compatible "$6$" hash value with random salt.static Stringsha512Crypt(byte[] keyBytes, String salt) Generates a libc6 crypt() compatible "$6$" hash value.static Stringsha512Crypt(byte[] keyBytes, String salt, Random random) Generates a libc6 crypt() compatible "$6$" hash value.
-
Field Details
-
KEY_MAX_DEFAULT
private static final int KEY_MAX_DEFAULTDefault maximum plaintext (key) length in bytes.- See Also:
-
KEY_MAX_PROPERTY
System property used to override the default maximum plaintext (key) length.- See Also:
-
ROUNDS_DEFAULT
private static final int ROUNDS_DEFAULTDefault number of rounds if not explicitly specified.- See Also:
-
ROUNDS_MAX
private static final int ROUNDS_MAXMaximum number of rounds.- See Also:
-
ROUNDS_MAX_DEFAULT
private static final int ROUNDS_MAX_DEFAULTDefault maximum number of rounds accepted from a caller-supplied salt string.- See Also:
-
ROUNDS_MIN
private static final int ROUNDS_MINMinimum number of rounds.- See Also:
-
ROUNDS_PREFIX
Prefix for optional rounds specification.- See Also:
-
ROUNDS_MAX_PROPERTY
System property used to override the default maximum number of rounds.- See Also:
-
SHA256_BLOCKSIZE
private static final int SHA256_BLOCKSIZEThe number of bytes the final hash value will have (SHA-256 variant).- See Also:
-
SHA256_PREFIX
The prefixes that can be used to identify this crypt() variant (SHA-256).- See Also:
-
SHA512_BLOCKSIZE
private static final int SHA512_BLOCKSIZEThe number of bytes the final hash value will have (SHA-512 variant).- See Also:
-
SHA512_PREFIX
The prefixes that can be used to identify this crypt() variant (SHA-512).- See Also:
-
SALT_PATTERN
The pattern to match valid salt values.
-
-
Constructor Details
-
Sha2Crypt
Deprecated.Will be private in the next major version.Consider private.
-
-
Method Details
-
firstNonZeroIndex
Finds the first non-zero digit, retaining one zero for an all-zero value.- Parameters:
value- a non-empty decimal string- Returns:
- the index of the first significant digit
-
getMaxKeyLen
private static int getMaxKeyLen()Gets the maximum plaintext (key) length in bytes, as configured by theorg.apache.commons.codec.digest.Sha2Crypt.keyMaxsystem property.- Returns:
- the maximum plaintext (key) length in bytes.
-
getMaxRounds
private static int getMaxRounds()Gets the maximum number of rounds accepted from a caller-supplied salt string, as configured by theorg.apache.commons.codec.digest.Sha2Crypt.roundsMaxsystem property.- Returns:
- the maximum number of rounds accepted from a caller-supplied salt string.
-
sha256Crypt
Generates a libc crypt() compatible "$5$" hash value with random salt.See
Crypt.crypt(String, String)for details.A salt is generated for you using
SecureRandom.- Parameters:
keyBytes- Plaintext to hash. Each array element is set to0before returning.- Returns:
- The Complete hash value.
- Throws:
IllegalArgumentException- Thrown ifkeyBytesexceeds the configured maximum lengthIllegalArgumentException- Thrown if aNoSuchAlgorithmExceptionis caught.
-
sha256Crypt
Generates a libc6 crypt() compatible "$5$" hash value.See
Crypt.crypt(String, String)for details.- Parameters:
keyBytes- Plaintext to hash. Each array element is set to0before returning.salt- real salt value without prefix or "rounds=". The salt may be null, in which case a salt is generated for you usingSecureRandom. If one does not want to useSecureRandom, you can pass your ownRandominsha256Crypt(byte[], String, Random).- Returns:
- The Complete hash value including salt.
- Throws:
IllegalArgumentException- Thrown ifkeyBytesexceeds the configured maximum lengthIllegalArgumentException- Thrown if the salt does not match the allowed pattern.IllegalArgumentException- Thrown if aNoSuchAlgorithmExceptionis caught.
-
sha256Crypt
Generates a libc6 crypt() compatible "$5$" hash value.See
Crypt.crypt(String, String)for details.- Parameters:
keyBytes- plaintext to hash. Each array element is set to0before returning.salt- real salt value without prefix or "rounds=".random- The instance ofRandomto use for generating the salt. Consider usingSecureRandomfor more secure salts.- Returns:
- The Complete hash value including salt.
- Throws:
IllegalArgumentException- Thrown ifkeyBytesexceeds the configured maximum lengthIllegalArgumentException- Thrown if the salt does not match the allowed pattern.IllegalArgumentException- Thrown if aNoSuchAlgorithmExceptionis caught.- Since:
- 1.12
-
sha2Crypt
private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix, int blocksize, String algorithm, int maxKeyLen, int maxRounds) Generates a libc6 crypt() compatible "$5$" or "$6$" SHA2 based hash value.This is a nearly line by line conversion of the original C function. The numbered comments are from the algorithm description, the short C-style ones from the original C code and the ones with "Remark" from me.
See
Crypt.crypt(String, String)for details.- Parameters:
keyBytes- plaintext to hash. Each array element is set to0before returning.salt- real salt value without prefix or"rounds="; may not be null.saltPrefix- either$5$or$6$.blocksize- A value that differs between$5$and$6$.algorithm-MessageDigestalgorithm identifier string.maxKeyLen- The maximum plaintext (key) length in bytes.maxRounds- The maximum number of rounds accepted from a caller-supplied salt string.- Returns:
- The Complete hash value including prefix and salt.
- Throws:
IllegalArgumentException- Thrown if the given salt isnullor does not match the allowed pattern.IllegalArgumentException- Thrown if aNoSuchAlgorithmExceptionis caught.- See Also:
-
sha512Crypt
Generates a libc crypt() compatible "$6$" hash value with random salt.See
Crypt.crypt(String, String)for details.A salt is generated for you using
SecureRandom.- Parameters:
keyBytes- Plaintext to hash. Each array element is set to0before returning.- Returns:
- Complete hash value.
- Throws:
IllegalArgumentException- Thrown ifkeyBytesexceeds the configured maximum lengthIllegalArgumentException- Thrown if aNoSuchAlgorithmExceptionis caught.
-
sha512Crypt
Generates a libc6 crypt() compatible "$6$" hash value.See
Crypt.crypt(String, String)for details.- Parameters:
keyBytes- Plaintext to hash. Each array element is set to0before returning.salt- Real salt value without prefix or "rounds=". The salt may be null, in which case a salt is generated for you usingSecureRandom; if you want to use aRandomobject other thanSecureRandomthen we suggest you provide it usingsha512Crypt(byte[], String, Random).- Returns:
- Complete hash value including salt.
- Throws:
IllegalArgumentException- Thrown ifkeyBytesexceeds the configured maximum lengthIllegalArgumentException- Thrown if the salt does not match the allowed pattern.IllegalArgumentException- Thrown if aNoSuchAlgorithmExceptionis caught.
-
sha512Crypt
Generates a libc6 crypt() compatible "$6$" hash value.See
Crypt.crypt(String, String)for details.- Parameters:
keyBytes- Plaintext to hash. Each array element is set to0before returning.salt- Real salt value without prefix or "rounds=". The salt may be null, in which case a salt is generated for you usingSecureRandom.random- The instance ofRandomto use for generating the salt. Consider usingSecureRandomfor more secure salts.- Returns:
- Complete hash value including salt.
- Throws:
IllegalArgumentException- Thrown if the salt does not match the allowed pattern.IllegalArgumentException- Thrown if aNoSuchAlgorithmExceptionis caught.- Since:
- 1.12
-