Skip to content

Instantly share code, notes, and snippets.

@mjec

mjec/main.rs Secret

Created April 29, 2022 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjec/afe8c1e06d5ff40b3dca230bfc509cfd to your computer and use it in GitHub Desktop.
Save mjec/afe8c1e06d5ff40b3dca230bfc509cfd to your computer and use it in GitHub Desktop.
use hmac::{Hmac, Mac};
use jwt::{Header, Token, VerifyWithKey};
use sha2::Sha256;
use std::collections::BTreeMap;
const DATA: &str = "420";
const SECRET_KEY: &str = "verysecretkey";
fn main() {
let secret_key = Hmac::<Sha256>::new_from_slice(SECRET_KEY.as_bytes()).unwrap();
let token = "eyJ0eXAiOiJKV1QiLCJraWQiOm51bGwsImFsZyI6IkhTMjU2In0.eyJpc3MiOm51bGwsInN1YiI6IjQyMCIsImF1ZCI6bnVsbCwiZXhwIjpudWxsLCJuYmYiOm51bGwsImlhdCI6bnVsbCwianRpIjpudWxsfQ._sN8Ur-b-38g4X2yQsIuhs4Z1dWjPW-7SHSFgmYa4xM";
println!("{}", token);
match VerifyWithKey::<Token<Header, BTreeMap<String, Option<String>>, _>>::verify_with_key(
token,
&secret_key,
) {
Ok(token) => {
assert_eq!(Some(DATA.to_string()), token.claims()["sub"]);
}
Err(e) => {
panic!("Token is not valid: {}", e)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment