public class SecurityUtils
extends java.lang.Object
Constructor and Description |
---|
SecurityUtils() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
asymmetricSign(java.security.interfaces.RSAPrivateKey key,
byte[] source,
int offset,
int length)
Takes an
RSAPrivateKey , byte array to be signed, an offset,
and length to sign asymmetrically using SHA1 and returns a byte
array containing the signature. |
static boolean |
asymmetricVerify(java.security.interfaces.RSAPublicKey key,
byte[] source,
int offset,
int length,
byte[] signature,
int sigOff,
int sigLen)
Verifies that the signature is valid based on the provided key
and returns true if the it is valid, false if it is not.
|
static java.lang.String |
certificateThumbprintToString(byte[] buf)
Takes a sha1 digest byte array and returns a formatted string
hash of the encoded bytes for comparison purposes.
|
static byte[] |
decryptDESede(byte[] toDecrypt,
byte[] keyBytes)
Performs symmetric decryption using DESede.
|
static byte[] |
decryptRSA(byte[] source,
java.security.Key key)
Decrypts a byte array and RSA encrypted data using the supplied key.
|
static byte[] |
encryptDESede(byte[] toEncrypt,
byte[] keyBytes)
Performs symmetric encryption using DESede.
|
static byte[] |
generateDESedKey()
Generates a DESede Key
|
static java.lang.String |
getCertificateThumbprint(java.security.cert.X509Certificate certificate)
Returns a to-stringed hash of the certificate encoded bytes.
|
static byte[] |
getCertificateThumbprintBytes(java.security.cert.X509Certificate certificate)
Returns a hash of the certificate encoded bytes.
|
static org.bouncycastle.crypto.params.RSAKeyParameters |
getKeyParameter(java.security.interfaces.RSAKey key)
Returns a parameter object suitable for initializing a Bouncy Castle
RSAEngine asymmetric block cipher.
|
static org.bouncycastle.crypto.params.RSAKeyParameters |
getPrivateKeyParameter(java.security.interfaces.RSAPrivateKey key)
Returns a
RSAKeyParameters object suitable for initializing
a Bouncy Castle RSAEngine asymmetric block cipher when given an
RSAPrivateKey . |
static org.bouncycastle.crypto.params.RSAKeyParameters |
getPublicKeyParameter(java.security.interfaces.RSAPublicKey key)
Returns an
RSAKeyParameters object suitable for initializing a Bouncy Castle
RSAEngine asymmetric block cipher when given an RSAPublicKey . |
static byte[] |
md5(java.io.File file)
Performs an md5 digest of a provided file and returns
a byte array of the digest.
|
static byte[] |
md5(java.io.InputStream stream)
Performs an md5 digest of a provided
InputStream and returns
a byte array of the digest. |
static java.security.cert.X509Certificate |
parseX509Certificate(byte[] input)
Generates an
X509Certificate object and initializes it with the
data read from the byte array input. |
static java.util.List<java.security.cert.X509Certificate> |
parseX509Certificates(byte[] input)
Generates a List of
X509Certificate objects and initializes them
with the data read from the byte array input. |
static java.util.List<java.security.cert.X509Certificate> |
parseX509Certificates(java.io.InputStream inputStream)
Generates a List of
X509Certificate objects and initializes them
with the data read from the InputStream inputStream. |
static byte[] |
sha1(byte[] input)
Performs a sha1 digest on the given input array,
returning the digest as a byte array.
|
static java.lang.String |
sha1String(java.lang.String input)
Performs a sha1 digest on the input string encoded as UTF-8,
returns the digest as Base64-ed bytes.
|
static java.lang.String |
sha256(java.lang.String source)
Performs a sha256 encryption on the provided String and returns a String
representation of the encrypted data.
|
static java.lang.String |
sha256PasswordSalt(java.lang.String password)
Returns a string containing the salted password
|
static boolean |
sha256PasswordSaltVerify(java.lang.String password,
java.lang.String hashedPass)
Verifies the salted password by comparing with the unsalted password
and returning true if the password matches the salted version.
|
public static org.bouncycastle.crypto.params.RSAKeyParameters getKeyParameter(java.security.interfaces.RSAKey key)
RSAPublicKey
or
RSAPrivateKey
can be passed and the subsequent
RSAKeyParameters
will be returned.key
- an RSAKey of type RSAPublicKey
or
RSAPrivateKey
, must not be nullRSAKeyParameters
object suitable for initializing
a public or private Bouncy Castle RSAEngine
asymmetric block cipher based on the type of RSAKey
providedjava.lang.ClassCastException
- if the key is not an RSAPublicKey
or RSAPrivateKey
public static org.bouncycastle.crypto.params.RSAKeyParameters getPublicKeyParameter(java.security.interfaces.RSAPublicKey key)
RSAKeyParameters
object suitable for initializing a Bouncy Castle
RSAEngine asymmetric block cipher when given an RSAPublicKey
.key
- an RSAPublicKey
object, must not be nullRSAKeyParameters
object suitable for initializing a
Bouncy Castle RSAEngine asymmetric block cipherpublic static org.bouncycastle.crypto.params.RSAKeyParameters getPrivateKeyParameter(java.security.interfaces.RSAPrivateKey key)
RSAKeyParameters
object suitable for initializing
a Bouncy Castle RSAEngine asymmetric block cipher when given an
RSAPrivateKey
. If the passed key is an RSAPrivateCrtKey
it will return an RSAPrivateCrtKeyParameter
.key
- an RSAPrivateKey
object, must not be nullRSAKeyParameter
object suitable for initializing
a Bouncy Castle RSAEngine asymmetric block cipher.
If the passed key is an RSAPrivateCrtKey
it will return
an RSAPrivateCrtKeyParameter
public static byte[] sha1(byte[] input)
input
- a byte array of the input needing the digest,
must not be nullpublic static java.lang.String sha1String(java.lang.String input)
input
- a UTF-8 encoded String, must not be nulljava.lang.RuntimeException
- if the passed String cannot be
encoded due to String format issuespublic static byte[] md5(java.io.File file) throws java.io.IOException
file
- File
needed to be digested, must not be nullbyte
arrayjava.io.IOException
- if the File
cannot be accessed or foundpublic static byte[] md5(java.io.InputStream stream) throws java.io.IOException
InputStream
and returns
a byte array of the digest.stream
- the InputStream
that needs to be digested,
must not be nulljava.io.IOException
- if the InputStream
cannot be accessedpublic static java.security.cert.X509Certificate parseX509Certificate(byte[] input) throws java.security.cert.CertificateException
X509Certificate
object and initializes it with the
data read from the byte array input. The certificate provided in the
byte array containing DER encoded binary or PEM encoded ASCII.
If the certificate is provided in Base64 encoding, it must be bounded
at the beginning by -----BEGIN CERTIFICATE-----,
and must be bounded at the end by -----END CERTIFICATE-----.input
- a byte
array containing DER encoded binary or
PEM encoded ASCII, must not be null and must be
properly formedX509Certificate
java.security.cert.CertificateException
- If the data in the byte array does not
contain an inherent end-of-certificate marker (other than EOF) and
there is trailing data after the certificate is parsed,
a CertificateException is thrown.public static java.util.List<java.security.cert.X509Certificate> parseX509Certificates(byte[] input) throws java.security.cert.CertificateException
X509Certificate
objects and initializes them
with the data read from the byte array input. The certificate provided
in the byte array containing DER encoded binary or PEM encoded ASCII.
If the certificate is provided in Base64 encoding, it must be bounded
at the beginning by -----BEGIN CERTIFICATE-----,
and must be bounded at the end by -----END CERTIFICATE-----.input
- a byte
array containing DER encoded binary
or PEM encoded ASCII, must not be null and must be
properly formedX509Certificate
objectsjava.security.cert.CertificateException
- If the data in the byte array does not
contain an inherent end-of-certificate marker (other than EOF) and
there is trailing data after the certificate is parsed,
a CertificateException is thrown.public static java.util.List<java.security.cert.X509Certificate> parseX509Certificates(java.io.InputStream inputStream) throws java.security.cert.CertificateException
X509Certificate
objects and initializes them
with the data read from the InputStream
inputStream. the
certificate provided in the byte array containing DER encoded binary
or PEM encoded ASCII. If the certificate is provided in Base64 encoding,
it must be bounded at the beginning by -----BEGIN CERTIFICATE-----,
and must be bounded at the end by -----END CERTIFICATE-----.inputStream
- an InputStream
containing DER encoded
binary or PEM encoded ASCII, must not be null and
must be properly formedX509Certificate
objectsjava.security.cert.CertificateException
- If the data in the stream does not
contain an inherent end-of-certificate marker
(other than EOF) and there is trailing data after
the certificate is parsed, a CertificateException is thrown.public static byte[] asymmetricSign(java.security.interfaces.RSAPrivateKey key, byte[] source, int offset, int length) throws java.lang.Exception
RSAPrivateKey
, byte array to be signed, an offset,
and length to sign asymmetrically using SHA1 and returns a byte
array containing the signature.key
- an RSAPrivateKey
that will be used to sign the
byte array, must not be nullsource
- the byte array used to generate the signature,
must not be nulloffset
- the specified offset, must not be nulllength
- the specified length, must not be nulljava.lang.Exception
- if unable to generate a signaturepublic static boolean asymmetricVerify(java.security.interfaces.RSAPublicKey key, byte[] source, int offset, int length, byte[] signature, int sigOff, int sigLen)
key
- an used to verify the signature, must not
be nullsource
- a byte array of the source data, must not be nulloffset
- the offset of the source data, must not be nulllength
- the length of the source data, must not be nullsignature
- a byte array of the signature, must not be nullsigOff
- the signatures offset, must not be nullsigLen
- the signature length, must not be nullpublic static byte[] decryptRSA(byte[] source, java.security.Key key) throws java.lang.Exception
source
- a byte array containing the data to decrypt,
must not be nullkey
- the Key
to use for decryption, must not be nulljava.lang.Exception
- if the Key
is invalid, the block size is
incorrect, or the padding is badpublic static byte[] encryptDESede(byte[] toEncrypt, byte[] keyBytes) throws java.lang.Exception
toEncrypt
- A byte array to encrypt. Must not be nullkeyBytes
- The key to use. must be 24 bytes longjava.lang.Exception
- if the key is invalid, the block size is
incorrect, or the padding is badpublic static byte[] decryptDESede(byte[] toDecrypt, byte[] keyBytes) throws java.lang.Exception
toDecrypt
- a byte[] to decrypt. Must not be nullkeyBytes
- The key to use. must be 24 bytes longjava.lang.Exception
- if the key is invalid, the block size is
incorrect, or the padding is badpublic static byte[] generateDESedKey() throws java.lang.Exception
java.lang.Exception
- if key generation failspublic static java.lang.String getCertificateThumbprint(java.security.cert.X509Certificate certificate) throws java.lang.Exception
certificate
- the X509Certificate
whose
thumbprint is needed, must not be nulljava.lang.Exception
- if the certificate is invalid or the
String cannot be builtpublic static java.lang.String certificateThumbprintToString(byte[] buf) throws java.lang.Exception
buf
- a byte array containing a sha1 digest, must not be nulljava.lang.Exception
- if the byte array cannot be formattedpublic static byte[] getCertificateThumbprintBytes(java.security.cert.X509Certificate certificate) throws java.security.cert.CertificateEncodingException
certificate
- the X509Certificate
to be hashed,
must not be nulljava.security.cert.CertificateEncodingException
- if the certificate cannot be encodedpublic static java.lang.String sha256(java.lang.String source)
source
- the String to be encrypted,
if null the String "" will be usedpublic static java.lang.String sha256PasswordSalt(java.lang.String password)
password
- the password to be salted, must not be nullpublic static boolean sha256PasswordSaltVerify(java.lang.String password, java.lang.String hashedPass)
password
- the unsalted password to compare against
the salted passwordhashedPass
- the salted password, must not be null