> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-framework/modularity/jwt/signing-jws.md).

# Signing (JWS)

Sign and verify JWTs with HMAC, RSA, and EC keys.

A JSON Web Signature (JWS) is a JWT whose payload has been signed — anyone with the verification key can confirm the token was issued by the holder of the signing key and that the claims have not been tampered with.

This guide walks through the three signing families that `bx-jwt` supports:

* **HMAC** (HS256, HS384, HS512) — symmetric, one key signs and verifies
* **RSA** (RS256, RS384, RS512) — asymmetric, private signs / public verifies
* **EC** (ES256, ES384, ES512) — asymmetric, smaller keys than RSA

## HMAC

HMAC is the simplest option: a single shared secret signs and verifies the token. Use it when both ends of the conversation are services you control.

```javascript
// Generate an RFC 7518–compliant 256-bit secret
secret = jwtGenerateSecret( 256 );

token   = jwtCreate( { sub: "user-123", iss: "my-api" }, secret, "HS256" );
payload = jwtVerify( token, secret, "HS256" );
```

{% hint style="warning" %}
HMAC keys must meet the minimum lengths in RFC 7518 §3.2 (HS256 = 32 bytes, HS384 = 48 bytes, HS512 = 64 bytes). [`jwtGenerateSecret()`](/boxlang-framework/modularity/jwt/reference/built-in-functions/jwtgeneratesecret.md) always produces a compliant key.
{% endhint %}

## RSA

RSA gives you asymmetric signing: a private key issues tokens, a public key verifies them. Distribute the public key freely — give the private key only to the issuer.

```javascript
keys    = jwtGenerateKeyPair( "RS256" );  // 2048-bit RSA pair
token   = jwtCreate( { sub: "user-123" }, keys.privateKey, "RS256" );
payload = jwtVerify( token, keys.publicKey, "RS256" );
```

For long-lived production keys, persist the PEM strings to disk or to a secret manager and reference them through the [key registry](/boxlang-framework/modularity/jwt/key-management.md).

## EC (Elliptic Curve)

EC signing produces dramatically smaller signatures and key material than RSA at equivalent security levels.

```javascript
keys    = jwtGenerateKeyPair( "ES256" );  // P-256
token   = jwtCreate( { sub: "user-123" }, keys.privateKey, "ES256" );
payload = jwtVerify( token, keys.publicKey, "ES256" );
```

## Verifying with Claim Assertions

Pass a `claims` struct to `jwtVerify()` to enforce expected values:

```javascript
payload = jwtVerify( token, secret, "HS256", {
    claims: { iss: "my-api", aud: "mobile-app" }
} );
```

A mismatch throws `bxjwt.JWTVerificationException`.

## Verify vs Validate vs Decode

| BIF                                                                                              | Returns             | Throws on failure | Use when…                                                  |
| ------------------------------------------------------------------------------------------------ | ------------------- | ----------------- | ---------------------------------------------------------- |
| [`jwtVerify()`](/boxlang-framework/modularity/jwt/reference/built-in-functions/jwtverify.md)     | claims struct       | Yes               | You need the claims and want exception-driven flow.        |
| [`jwtValidate()`](/boxlang-framework/modularity/jwt/reference/built-in-functions/jwtvalidate.md) | boolean             | No                | Simple yes/no check (e.g. middleware guard).               |
| [`jwtDecode()`](/boxlang-framework/modularity/jwt/reference/built-in-functions/jwtdecode.md)     | `{header, payload}` | No                | You need to read the header (e.g. `kid`) before verifying. |

## Refreshing Tokens

Use [`jwtRefresh()`](/boxlang-framework/modularity/jwt/reference/built-in-functions/jwtrefresh.md) to re-issue an existing token with fresh `iat`, `jti`, and an optional new `exp`. All application claims are preserved.

```javascript
newToken = jwtRefresh( oldToken, secret, "HS256", {
    allowExpired : true,
    expireIn     : 3600
} );
```

## Related

* [Encryption (JWE)](/boxlang-framework/modularity/jwt/encryption-jwe.md)
* [Key Management](/boxlang-framework/modularity/jwt/key-management.md)
* [Key Rotation](/boxlang-framework/modularity/jwt/key-rotation.md)
* [Security Best Practices](/boxlang-framework/modularity/jwt/security-best-practices.md)
* [Algorithms](/boxlang-framework/modularity/jwt/reference/algorithms.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://boxlang.ortusbooks.com/boxlang-framework/modularity/jwt/signing-jws.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
