java.lang.Object
com.aoapps.security.HashedKey
- All Implemented Interfaces:
Serializable
,Comparable<HashedKey>
A hashed random key.
- Author:
- AO Industries, Inc.
- See Also:
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Deprecated.static final int
Deprecated.This is the value matching the previous default algorithm, please useHashedKey.Algorithm.getHashBytes()
instead.static final HashedKey
A singleton that must be used in places where no key is set.static final String
Indicates that no key is set.static final HashedKey.Algorithm
The algorithm recommended for use with new keys. -
Constructor Summary
ConstructorDescriptionHashedKey
(byte[] hash) Deprecated.This represents a hash using the previous default algorithm, please useHashedKey(com.aoapps.security.HashedKey.Algorithm, byte[])
instead.HashedKey
(HashedKey.Algorithm algorithm, byte[] hash) Deprecated.Please usevalueOf(com.aoapps.security.HashedKey.Algorithm, byte[])
, which is able to automatically return theNO_KEY
singleton. -
Method Summary
Modifier and TypeMethodDescriptionint
boolean
Checks if equal to another hashed key, alwaysfalse
when either isNO_KEY
.static byte[]
Deprecated.This generates a key for the previous default algorithm, using the previous default of 256-bit length, please useHashedKey.Algorithm.generateKey()
instead.byte[]
getHash()
static byte[]
hash
(byte[] key) Deprecated.This generates a hash for the previous default algorithm and does not zero the key, please useHashedKey.Algorithm.hash(byte[])
instead.int
hashCode()
The hash code is taken from the last 32 bits of the hash.static void
boolean
Checks if this matches the provided key, alwaysfalse
when isNO_KEY
.toString()
Gets the string representation of the hashed key The format is subject to change over time, but will maintain backward compatibility.static HashedKey
valueOf
(HashedKey.Algorithm algorithm, byte[] hash) Restores aHashedKey
from its individual fields.static HashedKey
Parses the result oftoString()
.
-
Field Details
-
NO_KEY_VALUE
Indicates that no key is set.- See Also:
-
ALGORITHM
Deprecated.This is the value matching the previous default algorithm, please useHashedKey.Algorithm.getAlgorithmName()
instead. -
RECOMMENDED_ALGORITHM
The algorithm recommended for use with new keys. This may change at any time, but previous algorithms will remain supported. -
HASH_BYTES
Deprecated.This is the value matching the previous default algorithm, please useHashedKey.Algorithm.getHashBytes()
instead.The number of bytes in the SHA-256 hash. -
NO_KEY
A singleton that must be used in places where no key is set.
-
-
Constructor Details
-
HashedKey
@Deprecated public HashedKey(HashedKey.Algorithm algorithm, byte[] hash) throws IllegalArgumentException Deprecated.Please usevalueOf(com.aoapps.security.HashedKey.Algorithm, byte[])
, which is able to automatically return theNO_KEY
singleton.- Parameters:
algorithm
- The algorithm previously used to hash the key- Throws:
IllegalArgumentException
- whenhash.length != algorithm.getHashBytes()
-
HashedKey
Deprecated.This represents a hash using the previous default algorithm, please useHashedKey(com.aoapps.security.HashedKey.Algorithm, byte[])
instead.- Throws:
IllegalArgumentException
- whenhash.length != HASH_BYTES
-
-
Method Details
-
generateKey
Deprecated.This generates a key for the previous default algorithm, using the previous default of 256-bit length, please useHashedKey.Algorithm.generateKey()
instead.Generates a random plaintext key ofHASH_BYTES
bytes in length using a defaultSecureRandom
instance, which is not a strong instance to avoid blocking.- Returns:
- The caller must zero this array once no longer needed.
- See Also:
-
hash
Deprecated.This generates a hash for the previous default algorithm and does not zero the key, please useHashedKey.Algorithm.hash(byte[])
instead.Hashes the given key.- Parameters:
key
- Is not zeroed- See Also:
-
valueOf
Parses the result oftoString()
.- Parameters:
hashedKey
- whennull
, returnsnull
- Throws:
IllegalArgumentException
-
valueOf
public static HashedKey valueOf(HashedKey.Algorithm algorithm, byte[] hash) throws IllegalArgumentException Restores aHashedKey
from its individual fields. This is useful for reading the object from a database, for example.- Parameters:
algorithm
- The algorithm previously used to hash the key- Throws:
IllegalArgumentException
- whenhash.length != algorithm.getHashBytes()
-
toString
Gets the string representation of the hashed key The format is subject to change over time, but will maintain backward compatibility.Please see
valueOf(java.lang.String)
for the inverse operation. -
equals
Checks if equal to another hashed key, alwaysfalse
when either isNO_KEY
.Performs comparisons in length-constant time. https://crackstation.net/hashing-security.htm
-
hashCode
public int hashCode()The hash code is taken from the last 32 bits of the hash. The last 32 bits are selected because the first bits might include zero padding when the hash length is not a multiple ofByte.SIZE
. -
compareTo
- Specified by:
compareTo
in interfaceComparable<HashedKey>
-
getAlgorithm
-
getHash
public byte[] getHash()- Returns:
- Defensive copy
-
matches
Checks if this matches the provided key, alwaysfalse
when isNO_KEY
.This is most direct when the specific hash to verify against is already known. However, when searching for a hashed value by original key, such as in a mapping or database table, one would create a new instance to act as the look-up value.
Performs comparisons in length-constant time. https://crackstation.net/hashing-security.htm
- Parameters:
key
- Is destroyed before this method returns. If the original key is needed, pass a clone to this method.- See Also:
-
main
-
HashedKey.Algorithm.getAlgorithmName()
instead.