# Class SecurityUtils (2.1.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [2.1.0 (latest)](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.SecurityUtils)
- [2.0.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/2.0.3/com.google.api.client.util.SecurityUtils)
- [1.47.1](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.47.1/com.google.api.client.util.SecurityUtils)
- [1.46.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.46.3/com.google.api.client.util.SecurityUtils)
- [1.45.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.45.3/com.google.api.client.util.SecurityUtils)
- [1.44.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.44.2/com.google.api.client.util.SecurityUtils)
- [1.43.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.43.2/com.google.api.client.util.SecurityUtils)
- [1.42.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.42.3/com.google.api.client.util.SecurityUtils)
- [1.41.8](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.41.8/com.google.api.client.util.SecurityUtils)

    public final class SecurityUtils

Utilities related to Java security.

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> SecurityUtils

## Inherited Members

[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Static Methods

### createMtlsKeyStore(InputStream certAndKey)

    public static KeyStore createMtlsKeyStore(InputStream certAndKey)

[Beta](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.Beta)   

Create a keystore for mutual TLS with the certificate and private key provided.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `certAndKey` | `https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html` Certificate and private key input stream. The stream should contain one certificate and one unencrypted private key. If there are multiple certificates, only the first certificate will be used. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html` | keystore for mutual TLS. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/GeneralSecurityException.html` |   |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |

### getDefaultKeyStore()

    public static KeyStore getDefaultKeyStore()

Returns the default key store using KeyStore#getDefaultType().

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html` |   |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/KeyStoreException.html` |   |

### getEs256SignatureAlgorithm()

    public static Signature getEs256SignatureAlgorithm()

Returns the SHA-256 with ECDSA signature algorithm

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/Signature.html` |   |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/NoSuchAlgorithmException.html` |   |

### getJavaKeyStore()

    public static KeyStore getJavaKeyStore()

Returns the Java KeyStore (JKS).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html` |   |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/KeyStoreException.html` |   |

### getPkcs12KeyStore()

    public static KeyStore getPkcs12KeyStore()

Returns the PKCS12 key store.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html` |   |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/KeyStoreException.html` |   |

### getPrivateKey(KeyStore keyStore, String alias, String keyPass)

    public static PrivateKey getPrivateKey(KeyStore keyStore, String alias, String keyPass)

Returns the private key from the key store.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `keyStore` | `https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html` key store |
| `alias` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` alias under which the key is stored |
| `keyPass` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` password protecting the key |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/PrivateKey.html` | private key |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/GeneralSecurityException.html` |   |

### getRsaKeyFactory()

    public static KeyFactory getRsaKeyFactory()

Returns the RSA key factory.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/KeyFactory.html` |   |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/NoSuchAlgorithmException.html` |   |

### getSha1WithRsaSignatureAlgorithm()

    public static Signature getSha1WithRsaSignatureAlgorithm()

Returns the SHA-1 with RSA signature algorithm.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/Signature.html` |   |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/NoSuchAlgorithmException.html` |   |

### getSha256WithRsaSignatureAlgorithm()

    public static Signature getSha256WithRsaSignatureAlgorithm()

Returns the SHA-256 with RSA signature algorithm.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/Signature.html` |   |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/NoSuchAlgorithmException.html` |   |

### getX509CertificateFactory()

    public static CertificateFactory getX509CertificateFactory()

Returns the X.509 certificate factory.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/cert/CertificateFactory.html` |   |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/cert/CertificateException.html` |   |

### loadKeyStore(KeyStore keyStore, InputStream keyStream, String storePass)

    public static void loadKeyStore(KeyStore keyStore, InputStream keyStream, String storePass)

Loads a key store from a stream.

Example usage:

KeyStore keyStore = SecurityUtils.getJavaKeyStore();
SecurityUtils.loadKeyStore(keyStore, new FileInputStream("certs.jks"), "password");

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `keyStore` | `https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html` key store |
| `keyStream` | `https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html` input stream to the key store stream (closed at the end of this method in a finally block) |
| `storePass` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` password protecting the key store file |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |
| `https://docs.oracle.com/javase/8/docs/api/java/security/GeneralSecurityException.html` |   |

### loadKeyStoreFromCertificates(KeyStore keyStore, CertificateFactory certificateFactory, InputStream certificateStream)

    public static void loadKeyStoreFromCertificates(KeyStore keyStore, CertificateFactory certificateFactory, InputStream certificateStream)

Loads a key store with certificates generated from the specified stream using 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));

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `keyStore` | `https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html` key store (for example [#getJavaKeyStore()](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.SecurityUtils#com_google_api_client_util_SecurityUtils_getJavaKeyStore__)) |
| `certificateFactory` | `https://docs.oracle.com/javase/8/docs/api/java/security/cert/CertificateFactory.html` certificate factory (for example [#getX509CertificateFactory()](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.SecurityUtils#com_google_api_client_util_SecurityUtils_getX509CertificateFactory__)) |
| `certificateStream` | `https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html` certificate stream |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/GeneralSecurityException.html` |   |

### loadPrivateKeyFromKeyStore(KeyStore keyStore, InputStream keyStream, String storePass, String alias, String keyPass)

    public static PrivateKey loadPrivateKeyFromKeyStore(KeyStore keyStore, InputStream keyStream, String storePass, String alias, String keyPass)

Retrieves a private key from the specified key store stream and specified key store.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `keyStore` | `https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html` key store |
| `keyStream` | `https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html` input stream to the key store (closed at the end of this method in a finally block) |
| `storePass` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` password protecting the key store file |
| `alias` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` alias under which the key is stored |
| `keyPass` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` password protecting the key |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/PrivateKey.html` | key from the key store |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |
| `https://docs.oracle.com/javase/8/docs/api/java/security/GeneralSecurityException.html` |   |

### sign(Signature signatureAlgorithm, PrivateKey privateKey, byte\[\] contentBytes)

    public static byte[] sign(Signature signatureAlgorithm, PrivateKey privateKey, byte[] contentBytes)

Signs content using a private key.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `signatureAlgorithm` | `https://docs.oracle.com/javase/8/docs/api/java/security/Signature.html` signature algorithm |
| `privateKey` | `https://docs.oracle.com/javase/8/docs/api/java/security/PrivateKey.html` private key |
| `contentBytes` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[]` content to sign |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[]` | signed content |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/InvalidKeyException.html` |   |
| `https://docs.oracle.com/javase/8/docs/api/java/security/SignatureException.html` |   |

### verify(Signature signatureAlgorithm, PublicKey publicKey, byte\[\] signatureBytes, byte\[\] contentBytes)

    public static boolean verify(Signature signatureAlgorithm, PublicKey publicKey, byte[] signatureBytes, byte[] contentBytes)

Verifies the signature of signed content based on a public key.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `signatureAlgorithm` | `https://docs.oracle.com/javase/8/docs/api/java/security/Signature.html` signature algorithm |
| `publicKey` | `https://docs.oracle.com/javase/8/docs/api/java/security/PublicKey.html` public key |
| `signatureBytes` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[]` signature bytes |
| `contentBytes` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[]` content bytes |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` | whether the signature was verified |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/InvalidKeyException.html` |   |
| `https://docs.oracle.com/javase/8/docs/api/java/security/SignatureException.html` |   |

### verify(Signature signatureAlgorithm, X509TrustManager trustManager, List\<String\> certChainBase64, byte\[\] signatureBytes, byte\[\] contentBytes)

    public static X509Certificate verify(Signature signatureAlgorithm, X509TrustManager trustManager, List<String> certChainBase64, byte[] signatureBytes, byte[] contentBytes)

Verifies the signature of signed content based on a certificate chain.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `signatureAlgorithm` | `https://docs.oracle.com/javase/8/docs/api/java/security/Signature.html` signature algorithm |
| `trustManager` | `https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/X509TrustManager.html` trust manager used to verify the certificate chain |
| `certChainBase64` | `https://docs.oracle.com/javase/8/docs/api/java/util/List.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>` Certificate chain used for verification. The certificates must be base64 encoded DER, the leaf certificate must be the first element. |
| `signatureBytes` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[]` signature bytes |
| `contentBytes` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[]` content bytes |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/cert/X509Certificate.html` | The signature certificate if the signature could be verified, null otherwise. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/security/InvalidKeyException.html` |   |
| `https://docs.oracle.com/javase/8/docs/api/java/security/SignatureException.html` |   |