# Class JsonWebSignature (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.json.webtoken.JsonWebSignature)
- [2.0.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/2.0.3/com.google.api.client.json.webtoken.JsonWebSignature)
- [1.47.1](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.47.1/com.google.api.client.json.webtoken.JsonWebSignature)
- [1.46.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.46.3/com.google.api.client.json.webtoken.JsonWebSignature)
- [1.45.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.45.3/com.google.api.client.json.webtoken.JsonWebSignature)
- [1.44.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.44.2/com.google.api.client.json.webtoken.JsonWebSignature)
- [1.43.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.43.2/com.google.api.client.json.webtoken.JsonWebSignature)
- [1.42.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.42.3/com.google.api.client.json.webtoken.JsonWebSignature)
- [1.41.8](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.41.8/com.google.api.client.json.webtoken.JsonWebSignature)

    public class JsonWebSignature extends JsonWebToken

[JSON Web Signature(JWS)](https://tools.ietf.org/html/rfc7515).

Sample usage:

public static void printPayload(JsonFactory jsonFactory, String tokenString) throws IOException {
JsonWebSignature jws = JsonWebSignature.parse(jsonFactory, tokenString);
System.out.println(jws.getPayload());
}

Implementation is not thread-safe.

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> [JsonWebToken](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebToken) \> JsonWebSignature

## Inherited Members

[JsonWebToken.getHeader()](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebToken#com_google_api_client_json_webtoken_JsonWebToken_getHeader__) [JsonWebToken.getPayload()](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebToken#com_google_api_client_json_webtoken_JsonWebToken_getPayload__) [JsonWebToken.toString()](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebToken#com_google_api_client_json_webtoken_JsonWebToken_toString__) [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.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

### parse(JsonFactory jsonFactory, String tokenString)

    public static JsonWebSignature parse(JsonFactory jsonFactory, String tokenString)

Parses the given JWS token string and returns the parsed [JsonWebSignature](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebSignature).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `jsonFactory` | `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.JsonFactory` JSON factory |
| `tokenString` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` JWS token string |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebSignature` | parsed JWS |

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

### parser(JsonFactory jsonFactory)

    public static JsonWebSignature.Parser parser(JsonFactory jsonFactory)

Returns a new instance of a JWS parser.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `jsonFactory` | `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.JsonFactory` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebSignature.Parser` |   |

### signUsingRsaSha256(PrivateKey privateKey, JsonFactory jsonFactory, JsonWebSignature.Header header, JsonWebToken.Payload payload)

    public static String signUsingRsaSha256(PrivateKey privateKey, JsonFactory jsonFactory, JsonWebSignature.Header header, JsonWebToken.Payload payload)

Signs a given JWS header and payload based on the given private key using RSA and SHA-256 as
described in [JWS using
RSA SHA-256](http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-11#appendix-A.2).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `privateKey` | `https://docs.oracle.com/javase/8/docs/api/java/security/PrivateKey.html` private key |
| `jsonFactory` | `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.JsonFactory` JSON factory |
| `header` | `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebSignature.Header` JWS header |
| `payload` | `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebToken.Payload` JWS payload |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` | signed JWS string |

| **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` |   |

## Constructors

### JsonWebSignature(JsonWebSignature.Header header, JsonWebToken.Payload payload, byte\[\] signatureBytes, byte\[\] signedContentBytes)

    public JsonWebSignature(JsonWebSignature.Header header, JsonWebToken.Payload payload, byte[] signatureBytes, byte[] signedContentBytes)

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `header` | `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebSignature.Header` header |
| `payload` | `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebToken.Payload` payload |
| `signatureBytes` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[]` bytes of the signature |
| `signedContentBytes` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[]` bytes of the signed content |

## Methods

### getHeader()

    public JsonWebSignature.Header getHeader()

Returns the header.

Overriding is only supported for the purpose of calling the super implementation and
changing the return type, but nothing else.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebSignature.Header` |   |

**Overrides** [JsonWebToken.getHeader()](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.json.webtoken.JsonWebToken#com_google_api_client_json_webtoken_JsonWebToken_getHeader__)

### getSignatureBytes()

    public final byte[] getSignatureBytes()

Returns the bytes of the signature.

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

### getSignedContentBytes()

    public final byte[] getSignedContentBytes()

Returns the bytes of the signature content.

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

### verifySignature()

    public final X509Certificate verifySignature()

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

Verifies the signature of the content using the certificate chain embedded in the signature.

Currently only `"RS256"` algorithm is verified, but others may be added in the future.
For any other algorithm it returns `null`.

The certificate chain is verified using the system default trust manager.

The leaf certificate of the certificate chain must be an SSL server certificate.

| **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/GeneralSecurityException.html` |   |

### verifySignature(PublicKey publicKey)

    public final boolean verifySignature(PublicKey publicKey)

Verifies the signature of the content.

Currently only `"RS256"` and `"ES256"` algorithms are verified, but others may
be added in the future. For any other algorithm it returns `false`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `publicKey` | `https://docs.oracle.com/javase/8/docs/api/java/security/PublicKey.html` public key |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` | whether the algorithm is recognized and it is verified |

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

### verifySignature(X509TrustManager trustManager)

    public final X509Certificate verifySignature(X509TrustManager trustManager)

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

Verifies the signature of the content using the certificate chain embedded in the signature.

Currently only `"RS256"` and `"ES256"` algorithms are verified, but others may
be added in the future. For any other algorithm it returns `null`.

The leaf certificate of the certificate chain must be an SSL server certificate.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `trustManager` | `https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/X509TrustManager.html` trust manager used to verify the X509 certificate chain embedded in this message |

| **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/GeneralSecurityException.html` |   |