public final class SecurityUtils
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static java.security.KeyStore |
getDefaultKeyStore()
Returns the default key store using
KeyStore.getDefaultType(). |
static java.security.KeyStore |
getJavaKeyStore()
Returns the Java KeyStore (JKS).
|
static java.security.KeyStore |
getPkcs12KeyStore()
Returns the PKCS12 key store.
|
static java.security.PrivateKey |
getPrivateKey(java.security.KeyStore keyStore,
java.lang.String alias,
java.lang.String keyPass)
Returns the private key from the key store.
|
static java.security.KeyFactory |
getRsaKeyFactory()
Returns the RSA key factory.
|
static java.security.Signature |
getSha1WithRsaSignatureAlgorithm()
Returns the SHA-1 with RSA signature algorithm.
|
static java.security.Signature |
getSha256WithRsaSignatureAlgorithm()
Returns the SHA-256 with RSA signature algorithm.
|
static java.security.cert.CertificateFactory |
getX509CertificateFactory()
Returns the X.509 certificate factory.
|
static void |
loadKeyStore(java.security.KeyStore keyStore,
java.io.InputStream keyStream,
java.lang.String storePass)
Loads a key store from a stream.
|
static void |
loadKeyStoreFromCertificates(java.security.KeyStore keyStore,
java.security.cert.CertificateFactory certificateFactory,
java.io.InputStream certificateStream)
Loads a key store with certificates generated from the specified stream using
CertificateFactory.generateCertificates(InputStream). |
static java.security.PrivateKey |
loadPrivateKeyFromKeyStore(java.security.KeyStore keyStore,
java.io.InputStream keyStream,
java.lang.String storePass,
java.lang.String alias,
java.lang.String keyPass)
Retrieves a private key from the specified key store stream and specified key store.
|
static byte[] |
sign(java.security.Signature signatureAlgorithm,
java.security.PrivateKey privateKey,
byte[] contentBytes)
Signs content using a private key.
|
static boolean |
verify(java.security.Signature signatureAlgorithm,
java.security.PublicKey publicKey,
byte[] signatureBytes,
byte[] contentBytes)
Verifies the signature of signed content based on a public key.
|
static java.security.cert.X509Certificate |
verify(java.security.Signature signatureAlgorithm,
javax.net.ssl.X509TrustManager trustManager,
java.util.List<java.lang.String> certChainBase64,
byte[] signatureBytes,
byte[] contentBytes)
Verifies the signature of signed content based on a certificate chain.
|
public static java.security.KeyStore getDefaultKeyStore()
throws java.security.KeyStoreException
KeyStore.getDefaultType().java.security.KeyStoreExceptionpublic static java.security.KeyStore getJavaKeyStore()
throws java.security.KeyStoreException
java.security.KeyStoreExceptionpublic static java.security.KeyStore getPkcs12KeyStore()
throws java.security.KeyStoreException
java.security.KeyStoreExceptionpublic static void loadKeyStore(java.security.KeyStore keyStore,
java.io.InputStream keyStream,
java.lang.String storePass)
throws java.io.IOException,
java.security.GeneralSecurityException
Example usage:
KeyStore keyStore = SecurityUtils.getJavaKeyStore();
SecurityUtils.loadKeyStore(keyStore, new FileInputStream("certs.jks"), "password");
keyStore - key storekeyStream - input stream to the key store stream (closed at the end of this method in a
finally block)storePass - password protecting the key store filejava.io.IOExceptionjava.security.GeneralSecurityExceptionpublic static java.security.PrivateKey getPrivateKey(java.security.KeyStore keyStore,
java.lang.String alias,
java.lang.String keyPass)
throws java.security.GeneralSecurityException
keyStore - key storealias - alias under which the key is storedkeyPass - password protecting the keyjava.security.GeneralSecurityExceptionpublic static java.security.PrivateKey loadPrivateKeyFromKeyStore(java.security.KeyStore keyStore,
java.io.InputStream keyStream,
java.lang.String storePass,
java.lang.String alias,
java.lang.String keyPass)
throws java.io.IOException,
java.security.GeneralSecurityException
keyStore - key storekeyStream - input stream to the key store (closed at the end of this method in a finally
block)storePass - password protecting the key store filealias - alias under which the key is storedkeyPass - password protecting the keyjava.io.IOExceptionjava.security.GeneralSecurityExceptionpublic static java.security.KeyFactory getRsaKeyFactory()
throws java.security.NoSuchAlgorithmException
java.security.NoSuchAlgorithmExceptionpublic static java.security.Signature getSha1WithRsaSignatureAlgorithm()
throws java.security.NoSuchAlgorithmException
java.security.NoSuchAlgorithmExceptionpublic static java.security.Signature getSha256WithRsaSignatureAlgorithm()
throws java.security.NoSuchAlgorithmException
java.security.NoSuchAlgorithmExceptionpublic static byte[] sign(java.security.Signature signatureAlgorithm,
java.security.PrivateKey privateKey,
byte[] contentBytes)
throws java.security.InvalidKeyException,
java.security.SignatureException
signatureAlgorithm - signature algorithmprivateKey - private keycontentBytes - content to signjava.security.InvalidKeyExceptionjava.security.SignatureExceptionpublic static boolean verify(java.security.Signature signatureAlgorithm,
java.security.PublicKey publicKey,
byte[] signatureBytes,
byte[] contentBytes)
throws java.security.InvalidKeyException,
java.security.SignatureException
signatureAlgorithm - signature algorithmpublicKey - public keysignatureBytes - signature bytescontentBytes - content bytesjava.security.InvalidKeyExceptionjava.security.SignatureExceptionpublic static java.security.cert.X509Certificate verify(java.security.Signature signatureAlgorithm,
javax.net.ssl.X509TrustManager trustManager,
java.util.List<java.lang.String> certChainBase64,
byte[] signatureBytes,
byte[] contentBytes)
throws java.security.InvalidKeyException,
java.security.SignatureException
signatureAlgorithm - signature algorithmtrustManager - trust manager used to verify the certificate chaincertChainBase64 - Certificate chain used for verification. The certificates must be base64
encoded DER, the leaf certificate must be the first element.signatureBytes - signature bytescontentBytes - content bytesjava.security.InvalidKeyExceptionjava.security.SignatureExceptionpublic static java.security.cert.CertificateFactory getX509CertificateFactory()
throws java.security.cert.CertificateException
java.security.cert.CertificateExceptionpublic static void loadKeyStoreFromCertificates(java.security.KeyStore keyStore,
java.security.cert.CertificateFactory certificateFactory,
java.io.InputStream certificateStream)
throws java.security.GeneralSecurityException
CertificateFactory.generateCertificates(InputStream).
For each certificate, KeyStore.setCertificateEntry(String, Certificate) is called with
an alias that is the string form of incrementing non-negative integers starting with 0 (0, 1,
2, 3, ...).
Example usage:
KeyStore keyStore = SecurityUtils.getJavaKeyStore();
SecurityUtils.loadKeyStoreFromCertificates(keyStore, SecurityUtils.getX509CertificateFactory(),
new FileInputStream(pemFile));
keyStore - key store (for example getJavaKeyStore())certificateFactory - certificate factory (for example
getX509CertificateFactory())certificateStream - certificate streamjava.security.GeneralSecurityExceptionCopyright © 2011-2018 Google. All Rights Reserved.