#sd-jwt #disclosure #sha-256 #selective-disclosure


Rust implementation of the Selective Disclosure for JWTs (SD-JWT)

5 releases

0.2.1 Mar 12, 2024
0.2.0 Feb 5, 2024
0.1.2 Jan 14, 2024
0.1.1 Jan 8, 2024
0.1.0 Dec 11, 2023

#160 in Cryptography

Download history 146/week @ 2024-01-20 96/week @ 2024-01-27 135/week @ 2024-02-03 144/week @ 2024-02-10 203/week @ 2024-02-17 258/week @ 2024-02-24 149/week @ 2024-03-02 481/week @ 2024-03-09 233/week @ 2024-03-16 108/week @ 2024-03-23 99/week @ 2024-03-30 2/week @ 2024-04-06 17/week @ 2024-04-13 77/week @ 2024-04-20 54/week @ 2024-04-27 171/week @ 2024-05-04

319 downloads per month
Used in 2 crates (via identity_credential)


880 lines

Apache 2.0 license Discord StackExchange

SD-JWT Reference implementation

Rust implementation of the Selective Disclosure for JWTs (SD-JWT) version 07


This library supports

  • Encoding:
    • creating disclosers and replacing values in objects and arrays with the digest of their disclosure.
    • Adding decoys to objects and arrays.
  • Decoding
    • Recursively replace digests in objects and arrays with their corresponding disclosure value.

Sha-256 hash function is shipped by default, encoding/decoding with other hash functions is possible.

Getting started

Include the library in your cargo.toml.

sd-jwt-payload = { version = "0.2.1" }


See sd_jwt.rs for a runnable example.


This library consists of the major structs:

  1. SdObjectEncoder: creates SD objects.
  2. SdObjectDecoder: decodes SD objects.
  3. Disclosure: used by the SdObjectEncoder and SdObjectDecoder to represent a disclosure.
  4. SdJwt: creates/parses SD-JWTs.
  5. Hasher: a trait to provide hash functions to the encoder/decoder.
  6. Sha256Hasher: implements Hasher for the Sha-256 hash function.


Any JSON object can be encoded

  let object = json!({
    "given_name": "John",
    "family_name": "Doe",
    "address": {
      "street_address": "123 Main St",
      "region": "Anystate",
    "phone": [
      "+49 123456",
      "+49 234567"
  let mut encoder: SdObjectEncoder = object.try_into()?;

This creates a stateful encoder with Sha-256 hash function by default to create disclosure digests.

Note: SdObjectEncoder is generic over Hasher which allows custom encoding with other hash functions.

The encoder can encode any of the object's values or array elements, using the conceal method. Suppose the value of street_address should be selectively disclosed as well as the value of address and the first phone value.

  let disclosure1 = encoder.conceal("/address/street_address"], None)?;
  let disclosure2 = encoder.conceal("/address", None)?;
  let disclosure3 = encoder.conceal("/phone/0", None)?;

Note: the conceal method takes a JSON Pointer to determine the element to conceal inside the JSON object.

The encoder also supports adding decoys. For instance, the amount of phone numbers and the amount of claims need to be hidden.

  encoder.add_decoys("/phone", 3).unwrap(); //Adds 3 decoys to the array `phone`.
  encoder.add_decoys("", 6).unwrap(); // Adds 6 decoys to the top level object.

Add the hash function claim.

  encoder.add_sd_alg_property(); // This adds "_sd_alg": "sha-256"

Now encoder.object()? will return the encoded object.

  "given_name": "John",
  "family_name": "Doe",
  "phone": [
      "...": "eZVn0KkQm_T8x-x57VxYt-_MmNG91Sh34E-bZEnNfWY"
    "+49 234567",
      "...": "KAiJIx0tktQRXBxZSBVVld9298bZIp2WkpkDYDa3CWQ"
      "...": "CBKARPh6sdTCJyliZ7pBOYzix7Z4Bb4yRh0EykHX2Uw"
      "...": "oi1KgsYXgqBFXUXvbVaHSGYYaWhkB5RL55T90Gl_5s0"
  "_sd": [
  "_sd_alg": "sha-256"

Note: no JWT claims like exp or iat are added. If necessary, these need to be added and validated manually.

Creating SD-JWT

Since creating JWTs is outside the scope of this library, see sd_jwt.rs example where josekit is used to create jwt with the object above as the claim set.

Create SD-JWT

  let sd_jwt: SdJwt = SdJwt::new(jwt, disclosures.clone(), None);
  let sd_jwt: String = sd_jwt.presentation();


Parse the SD-JWT string to extract the JWT and the disclosures in order to decode the claims and construct the disclosed values.

*Note: Validating the signature of the JWT and extracting the claim set is outside the scope of this library.

  let sd_jwt: SdJwt = SdJwt::parse(sd_jwt_string)?;
  let claims_set: // extract claims from `sd_jwt.jwt`.
  let decoder = SdObjectDecoder::new();
  let decoded_object = decoder.decode(claims_set, &sd_jwt.disclosures)?;


  "given_name": "John",
  "family_name": "Doe",
  "phone": [
    "+49 123456",
    "+49 234567"
  "address": {
    "region": "Anystate",
    "street_address": "123 Main St"


  • street_address and address are recursively decoded.
  • _sd_alg property was removed.


Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)


Distributed under the Apache License. See LICENSE for more information.

(back to top)


~72K SLoC