Crate jws

source ·
Expand description

This library provides JSON Web Signature encoding, decoding, signing and verification.

Currently, encoding and decoding is available only for the JWS Compact Serialization scheme in the compact module.

Signing and verifying is done through the Signer and Verifier traits, for which some implementations based on rust-crypto are available in the crypto module.

Example:

use jws::{JsonObject, JsonValue};
use jws::compact::{decode_verify, encode_sign};
use jws::crypto::{signer_hs512, HmacVerifier};

fn encode_decode() -> jws::Result<()> {
  // Add custom header parameters.
  let mut header = JsonObject::new();
  header.insert(String::from("typ"), JsonValue::from("text/plain"));

  // Encode and sign the message.
  let encoded = encode_sign(header, b"payload", signer_hs512(b"secretkey"))?;

  // Decode and verify the message.
  let decoded = decode_verify(encoded.data().as_bytes(), HmacVerifier::new(&b"secretkey"[..]))?;

  assert_eq!(decoded.payload, b"payload");
  assert_eq!(decoded.header.get("typ").and_then(|x| x.as_str()), Some("text/plain"));

  Ok(())
}

To sign and verify a message

Re-exports

pub use crate::header::AvailableHeaders;
pub use crate::header::HeadersRef;
pub use crate::header::HeadersMut;

Modules

JWS Compact Serialization implementaton.
Verifier and Signer implementations using rust-crypto.
Types for working with message headers.

Structs

An error that can occur during JWS processing.

Enums

Indicates the type of an error that can occur during JWS processing.

Traits

A signer for JWS messages.
A verifier for JWS messages.

Type Definitions

A JSON object.
std::result::Result with the error type filled in.