public class CryptoUtil
extends java.lang.Object
Modifier | Constructor and Description |
---|---|
protected |
CryptoUtil()
Initialize the new instance with default values.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
asPlainText(java.lang.String aString)
Returns the given string as it is if it does not start with a prefix in
curly brackets (e.g.
|
java.lang.String |
base64EncodedWithPrefix(byte[] hash,
HashMechanism hashMechanism) |
java.lang.String |
base64HashedUTF8(java.lang.String aString,
java.lang.String hashMechanism)
Returns the given string UTF-8 char encoded, hashed with the specified hashing
mechanism (e.g.
|
byte[] |
computeHash(byte[] content,
CheckedHashAlgorithm hashAlgorithm)
Computes a hash value for the given input data using the specified hashing algorithm.
|
byte[] |
computeHash(byte[] content,
java.lang.String algorithm)
Computes a hash value for the given input data using the specified hashing algorithm.
|
byte[] |
computeHash(java.lang.String text,
CheckedCharsetName charEncoding,
java.lang.String algorithm)
Computes a hash value for the given text using the specified hashing algorithm.
|
byte[] |
computeHash(java.lang.String text,
java.lang.String charEncoding,
java.lang.String algorithm)
Computes a hash value for the given text using the specified hashing algorithm.
|
byte[] |
computeUTF8Hash(java.lang.String text,
java.lang.String algorithmName)
Computes a hash value for the given text using UTF-8 character encoding an the
specified hashing algorithm.
|
protected byte[] |
concatArrays(byte[] bytes1,
byte[] bytes2) |
byte[] |
createSHAhash(java.lang.String aString)
Returns the SHA-1 hash representation of the given string in the current
platform's character encoding.
|
byte[] |
createSSHAhash(java.lang.String aString)
Returns the SSHA hash representation of the given string
|
byte[] |
createSSHAhash(java.lang.String aString,
byte[] salt)
Returns the SSHA hash representation (SHA-1 based) of the given string.
|
static CryptoUtil |
current()
Returns the only instance this class supports (design pattern "Singleton")
|
protected java.lang.String |
decryptString(java.lang.String aString) |
java.lang.String |
defaultEncrypted(java.lang.String aString)
Returns the given string encrypted with the default algorithm and base64
encoded with a prefix naming the algorithm in curly brackets (e.g.
|
boolean |
equals(java.lang.String str1,
java.lang.String str2)
Compares whether or not the two given strings are equal.
|
boolean |
equals(java.lang.String str1,
java.lang.String str2,
CheckedCharsetName charEncoding)
Compares whether or not the two given strings are equal.
|
protected boolean |
equalsHashedAgainstEncoded(java.lang.String hashed,
java.lang.String maybeEncoded,
CheckedCharsetName charEncoding) |
boolean |
equalsUTF8(java.lang.String str1,
java.lang.String str2)
Compares whether or not the two given strings are equal.
|
byte[] |
generateSalt(int length)
Generates a random salt with the specified length.
|
byte[] |
generateSalt20()
Generates a random salt with the default length of 20.
|
java.lang.String |
getFingerprintOf(java.security.cert.X509Certificate cert)
Returns the fingerprint (i.e.
|
java.lang.String |
getFingerprintOf(java.security.cert.X509Certificate cert,
java.lang.String hexSeparator)
Returns the fingerprint (i.e.
|
protected void |
handleException(java.lang.Exception ex) |
java.lang.String |
hashed(byte[] data,
HashMechanism hashMechanism)
Returns the given data hashed with the specified hashing
mechanism (e.g.
|
java.lang.String |
hashedUTF8(java.lang.String aString,
HashMechanism hashMechanism)
Returns the given string UTF-8 char encoded, hashed with the specified hashing
mechanism (e.g.
|
java.lang.String |
hashedUTF8(java.lang.String aString,
java.lang.String hashMechanismName)
Returns the given string UTF-8 char encoded, hashed with the specified hashing
mechanism (e.g.
|
protected boolean |
isEncrypted(java.lang.String str) |
protected boolean |
isHashed(java.lang.String str) |
protected byte[] |
obfuscate1(java.lang.String aStr)
Encrypts the given string according to IAP1 algorithm
|
protected java.lang.String |
plainText1(java.lang.String string)
Decrypts the given string which must be IAP1 obfuscated
|
java.lang.String |
shaEncrypted(java.lang.String aString)
Returns the given string SHA encrypted and base64 encoded with
a prefix "{SHA}".
|
java.lang.String |
sshaEncrypted(java.lang.String aString)
Returns the given string SSHA encrypted and base64 encoded with
a prefix "{SSHA}".
|
java.lang.String |
sshaEncrypted(java.lang.String aString,
byte[] salt)
Returns the given string SSHA encrypted and base64 encoded with
a prefix "{SSHA}".
|
protected StringUtil |
str() |
protected void |
transform1(byte[] bytes,
int keyNum)
Does a XOR transformation of each byte in the given byte array using the
key specified by the given keyNum.
|
protected java.lang.String |
unencoded(java.lang.String str)
Tries to Base64 decode the given string.
|
byte[] |
xor1(java.lang.String string)
Returns the byte array containing the xor encoded representation
of the given input string.
|
java.lang.String |
xor1Encrypted(java.lang.String aString)
Returns the given string XOR1 encrypted and base64 encoded with
a prefix "{XOR1}".
|
protected CryptoUtil()
public static CryptoUtil current()
public java.lang.String asPlainText(java.lang.String aString)
aString
- The string to return or to convert into plain textpublic java.lang.String sshaEncrypted(java.lang.String aString)
aString
- The string to be encryptedpublic java.lang.String sshaEncrypted(java.lang.String aString, byte[] salt)
aString
- The string to be encryptedsalt
- The salt valuepublic java.lang.String shaEncrypted(java.lang.String aString)
aString
- The string to be encryptedpublic java.lang.String base64HashedUTF8(java.lang.String aString, java.lang.String hashMechanism)
CryptoConstants.SHA_2_384_NAME
)
and base64 encoded with a prefix (e.g. "{SHA256}) for the used mechanism".
Finally the whole string gets base64 encoded and returned.aString
- The string to be hashed.hashMechanism
- The hashing mechanism to be used (Not algorithm names!).
See CryptoConstants.SHA_1_NAME
.CryptoRuntimeException
- If the specified mechanism is not supported.public java.lang.String hashedUTF8(java.lang.String aString, java.lang.String hashMechanismName)
CryptoConstants.SHA_2_512_NAME
)
and base64 encoded with a prefix (e.g. "{SHA256}) for the used mechanism".aString
- The string to be hashed.hashMechanismName
- The hashing mechanism to be used (Not algorithm names!). See CryptoConstants
.CryptoRuntimeException
- If the specified mechanism is not supported.public java.lang.String hashedUTF8(java.lang.String aString, HashMechanism hashMechanism)
HashMechanism.SALTED_SHA_2_256
)
and base64 encoded with a prefix (e.g. "{SSHA256}") for the used mechanism.aString
- The string to be hashed.hashMechanism
- The hashing mechanism to be used.CryptoRuntimeException
- If the specified mechanism is not supported.public java.lang.String hashed(byte[] data, HashMechanism hashMechanism)
HashMechanism.SALTED_SHA_2_512
)
and base64 encoded with a prefix (e.g. "{SSHA512}") for the used mechanism.data
- The data to be hashed.hashMechanism
- The hashing mechanism to be used.CryptoRuntimeException
- If the specified mechanism is not supported.public java.lang.String base64EncodedWithPrefix(byte[] hash, HashMechanism hashMechanism)
public java.lang.String xor1Encrypted(java.lang.String aString)
aString
- The string to be encryptedpublic java.lang.String defaultEncrypted(java.lang.String aString)
Currently it uses the XOR encryption.
public boolean equals(java.lang.String str1, java.lang.String str2)
The sting character encoding used here is the platform's default encoding.
public boolean equalsUTF8(java.lang.String str1, java.lang.String str2)
The sting character encoding used here is the platform's default encoding.
public boolean equals(java.lang.String str1, java.lang.String str2, CheckedCharsetName charEncoding)
Comparing two hashed or XOR encrypted values always return false.
str1
- First string to compare with second.str2
- Second string to compare with first.charEncoding
- The character encoding used to convert strings to byte arrays.public byte[] xor1(java.lang.String string)
public byte[] createSSHAhash(java.lang.String aString)
public byte[] createSSHAhash(java.lang.String aString, byte[] salt)
aString
- the string to be hashed.salt
- the salt value for SSHA.public byte[] createSHAhash(java.lang.String aString)
public byte[] computeHash(byte[] content, java.lang.String algorithm)
content
- The content to be hashed (must not be null).algorithm
- The name of the has algorithm (see CryptoConstants
). Must not be null.CryptoRuntimeExcption
- If the given algorithm is not supported.public byte[] computeHash(byte[] content, CheckedHashAlgorithm hashAlgorithm)
content
- The content to be hashed (must not be null).hashAlgorithm
- The hash mechanism to use. Must not be null.CryptoRuntimeExcption
- If the given algorithm is not supported.public byte[] computeHash(java.lang.String text, java.lang.String charEncoding, java.lang.String algorithm)
text
- The content to be hashed (must not be null).charEncoding
- The character encoding to be used when converting the given text to bytes.algorithm
- The name of the has algorithm (see CryptoConstants
). Must not be null.CryptoRuntimeExcption
- If the given charEncoding or algorithm is not supported.public byte[] computeHash(java.lang.String text, CheckedCharsetName charEncoding, java.lang.String algorithm)
text
- The content to be hashed (must not be null).charEncoding
- The character encoding to be used when converting the given text to bytes.algorithm
- The name of the has algorithm (see CryptoConstants
). Must not be null.CryptoRuntimeExcption
- If the given charEncoding or algorithm is not supported.public byte[] computeUTF8Hash(java.lang.String text, java.lang.String algorithmName)
text
- The content to be hashed (must not be null).algorithmName
- The name of the has algorithm (see CryptoConstants
). Must not be null.CryptoRuntimeExcption
- If the given charEncoding or algorithm is not supported.public byte[] generateSalt(int length)
length
- The length of the salt to be generated.public byte[] generateSalt20()
public java.lang.String getFingerprintOf(java.security.cert.X509Certificate cert) throws java.security.cert.CertificateEncodingException
java.security.cert.CertificateEncodingException
public java.lang.String getFingerprintOf(java.security.cert.X509Certificate cert, java.lang.String hexSeparator) throws java.security.cert.CertificateEncodingException
java.security.cert.CertificateEncodingException
protected java.lang.String decryptString(java.lang.String aString)
protected boolean equalsHashedAgainstEncoded(java.lang.String hashed, java.lang.String maybeEncoded, CheckedCharsetName charEncoding)
protected java.lang.String unencoded(java.lang.String str)
protected boolean isEncrypted(java.lang.String str)
protected boolean isHashed(java.lang.String str)
protected byte[] obfuscate1(java.lang.String aStr)
protected java.lang.String plainText1(java.lang.String string)
protected void transform1(byte[] bytes, int keyNum)
protected byte[] concatArrays(byte[] bytes1, byte[] bytes2)
protected void handleException(java.lang.Exception ex)
protected StringUtil str()