Expand description
§HMAC Signer
hmac_serialiser_rs
is a Rust library for generating and verifying HMAC signatures for secure data transmission.
It uses the ring
crate for HMAC operations and serde
for serialising and deserialising data.
Moreover, it uses the base64
crate for encoding and decoding data.
§License
This library is licensed under the MIT license.
§Features
- Supports various encoding schemes for signatures.
- Flexible HMAC signer logic for custom data types.
- Provides a convenient interface for signing and verifying data.
§Example
use hmac_serialiser_rs::{Encoder, HmacSigner, KeyInfo, SignerLogic, Algorithm};
use serde::{Serialize, Deserialize};
use std::error::Error;
#[derive(Serialize, Deserialize, Debug)]
struct UserData {
// Add your data fields here
username: String,
email: String,
}
impl hmac_serialiser_rs::Data for UserData {
fn get_exp(&self) -> Option<chrono::DateTime<chrono::Utc>> {
// Add logic to retrieve expiration time if needed
None
}
}
fn main() -> Result<(), Box<dyn Error>> {
// Define your secret key, salt, and optional info
let key_info = KeyInfo {
key: b"your_secret_key".to_vec(),
salt: b"your_salt".to_vec(),
info: vec![], // empty info
};
// Initialize the HMAC signer
let signer = HmacSigner::new(key_info, Algorithm::SHA256, Encoder::UrlSafe);
// Serialize your data
let user_data = UserData {
username: "user123".to_string(),
email: "user123@example.com".to_string(),
};
let token = signer.sign(&user_data);
println!("Token: {}", token);
// Verify the token
let verified_data: UserData = signer.unsign(&token)?;
println!("Verified data: {:?}", verified_data);
Ok(())
}
§Supported Encoders
Standard
: Standard base64 encoding.UrlSafe
: URL-safe base64 encoding.StandardNoPadding
: Standard base64 encoding without padding.UrlSafeNoPadding
: URL-safe base64 encoding without padding.
§Supported HMAC Algorithms
SHA1
SHA256
SHA384
SHA512
§Traits
Data
: A trait for data structures that can be signed and verified.SignerLogic
: A trait for defining signer logic.
§Errors
Errors are represented by the Error
enum, which includes:
InvalidInput
: Invalid input data.InvalidSignature
: Invalid signature provided.InvalidToken
: Invalid token provided.HkdfExpandError
: Error during key expansion.HkdfFillError
: Error during key filling.TokenExpired
: Token has expired.
§Contributing
Contributions are welcome! Feel free to open issues and pull requests on GitHub.
Modules§
Structs§
- Hmac
Signer - A struct that holds the HMAC signer logic.
- KeyInfo
- A struct that holds the key information required for key expansion.
Enums§
- Encoder
- An enum for defining the encoding scheme for the payload and the signature.
Traits§
- Data
- A trait for custom data types that can be signed and verified.
- Signer
Logic - A trait for defining the signer logic.