org.pfsw.security.crypto
Class HashedString

java.lang.Object
  extended by org.pfsw.security.crypto.HashedString

public class HashedString
extends java.lang.Object

Container for a SHA hashed strings including salt for SSHA. Supports SHA-1 (160 bit) as well as SHA-2 (256, 383, 512 bit). SHA-2 224 bit is not supported before Java 8 or with a crypto provider explicitly added to the classpath.


Field Summary
protected static CryptoUtil CRYPTU
           
 
Constructor Summary
HashedString(byte[] content, byte[] salt, HashMechanism mechanism)
           
HashedString(byte[] content, byte[] salt, java.lang.String hashType)
           
HashedString(byte[] content, HashMechanism mechanism)
           
HashedString(byte[] content, java.lang.String hashType)
           
HashedString(java.lang.String string)
          Initialize the new instance with a hashed string.
 
Method Summary
 java.lang.String asString()
          Returns the contents base64 with a curly bracket prefix that defines the used hashing mechanism.
protected  HashMechanism getHashMechanism()
           
 byte[] getHashValue()
          Returns the hash value (without appended salt, if any).
 java.lang.String getHashValueBase64()
          Returns the hash value (without appended salt, if any) as Base64 encoded string.
 byte[] getHashWithSaltValue()
          Returns the hash value with appended salt value (if any).
 java.lang.String getHashWithSaltValueBase64()
          Returns the hash value with appended salt value (if any) as Base64 encoded string.
protected  byte[] getSalt()
           
 byte[] getSaltValue()
          Returns the salt that has been used to hash the string or null if no salt was used.
protected  byte[] getStrHash()
           
protected  void init(byte[] content, byte[] saltData, HashMechanism mechanism)
           
protected  void init(java.lang.String string)
          Initialized this HashedString instance from a Base64 encoded hash that is prefixed with an algorithm identifier.
 boolean isEqual(byte[] plainText)
          Compares the given plain text string with the hashed string.
 boolean isEqual(java.lang.String string)
          Compares the given clear text string with the hashed value.
 boolean isEqualUTF8(java.lang.String string)
          Compares the given clear text string with the hashed value (which was hashed as UTF-8).
protected  boolean isHashed()
           
 boolean isSalted()
          Returns true if the value is salted.
protected  void setHashMechanism(HashMechanism info)
           
protected  void setIsHashed(boolean newValue)
           
protected  void setSalt(byte[] newValue)
           
protected  void setStrHash(byte[] newValue)
           
protected  byte[][] split(byte[] src, int n)
           
protected  org.pfsw.text.StringUtil str()
           
protected  byte[] stringToBytes(java.lang.String string)
          Converts the given string to a byte array.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CRYPTU

protected static final CryptoUtil CRYPTU
Constructor Detail

HashedString

public HashedString(java.lang.String string)
Initialize the new instance with a hashed string.

Parameters:
string - A base64 encoded hash of a string with a prefix (e.g. {SHA} or {SSHA} or {SHA256} )

HashedString

public HashedString(byte[] content,
                    java.lang.String hashType)

HashedString

public HashedString(byte[] content,
                    HashMechanism mechanism)

HashedString

public HashedString(byte[] content,
                    byte[] salt,
                    java.lang.String hashType)

HashedString

public HashedString(byte[] content,
                    byte[] salt,
                    HashMechanism mechanism)
Method Detail

getHashValue

public byte[] getHashValue()
Returns the hash value (without appended salt, if any).


getSaltValue

public byte[] getSaltValue()
Returns the salt that has been used to hash the string or null if no salt was used.


getHashWithSaltValue

public byte[] getHashWithSaltValue()
Returns the hash value with appended salt value (if any). If no salt value was used then the result is the same as of getHashValue().


getHashValueBase64

public java.lang.String getHashValueBase64()
Returns the hash value (without appended salt, if any) as Base64 encoded string.


getHashWithSaltValueBase64

public java.lang.String getHashWithSaltValueBase64()
Returns the hash value with appended salt value (if any) as Base64 encoded string. If no salt value was used then the result is the same as of getHashValueBase64().


isSalted

public boolean isSalted()
Returns true if the value is salted.


isEqualUTF8

public boolean isEqualUTF8(java.lang.String string)
Compares the given clear text string with the hashed value (which was hashed as UTF-8). Returns true if the hash of the given string equals the hash in this object.


isEqual

public boolean isEqual(java.lang.String string)
Compares the given clear text string with the hashed value. Returns true if the hash of the given string equals the hash in this object.


isEqual

public boolean isEqual(byte[] plainText)
Compares the given plain text string with the hashed string. Returns true if the hash of the given plain text equals the hash in this object.


asString

public java.lang.String asString()
Returns the contents base64 with a curly bracket prefix that defines the used hashing mechanism.

Example: "{SSHA256}lKv8GwCDbjB6wvYroiAKaGZXUdWYgtYAnpQWLTrWHPUSzuooRrOT5Yp70mbNwfx0IziQ+g=="


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

init

protected void init(java.lang.String string)
Initialized this HashedString instance from a Base64 encoded hash that is prefixed with an algorithm identifier.

Parameters:
string - Base64 encoded hash with prefix.

init

protected void init(byte[] content,
                    byte[] saltData,
                    HashMechanism mechanism)

split

protected byte[][] split(byte[] src,
                         int n)

stringToBytes

protected byte[] stringToBytes(java.lang.String string)
Converts the given string to a byte array. This default implementation is using the platform's default character encoding. Subclasses my override this method to use other encoding (e.g. UTF-8).


getStrHash

protected byte[] getStrHash()

setStrHash

protected void setStrHash(byte[] newValue)

getSalt

protected byte[] getSalt()

setSalt

protected void setSalt(byte[] newValue)

isHashed

protected boolean isHashed()

setIsHashed

protected void setIsHashed(boolean newValue)

getHashMechanism

protected HashMechanism getHashMechanism()

setHashMechanism

protected void setHashMechanism(HashMechanism info)

str

protected org.pfsw.text.StringUtil str()