serde_hash
A Rust library for seamlessly integrating HashIds with Serde serialization and deserialization. This library provides a convenient way to obfuscate numeric IDs in your JSON output without changing your application's internal data structures.
Features
- Automatically convert numeric IDs to hash strings during serialization
- Transparently decode hash strings back to numeric IDs during deserialization
- Configurable hash generation with customizable salt, minimum length, and character set
- Simple attribute-based field marking with
#[hash] - Secure random salt generation
Installation
Add serde_hash and serde_hash_derive to your Cargo.toml:
[]
= "0.1.3"
= { = "1.0", = ["derive"] }
= "1.0" # If using JSON serialization
Supported Types
The #[hash] attribute is only compatible with unsigned integer types such as u64 and u32, and vectors of these types like Vec<u64> and Vec<u32>. It cannot be used with floating-point (f32, f64) or signed integer (i32, i64) types or their vector equivalents.
| Name | Type |
|---|---|
| 128bit Unsigned Int | u128 |
| 64bit Unsigned Int | u64 |
| 32bit Unsigned Int | u32 |
| 16bit Unsigned Int | u16 |
| 8bit Unsigned Int | u8 |
| Pointer-sized Int | usize |
| Optional 128bit Unsigned Int | Option<u128> |
| Optional 64bit Unsigned Int | Option<u64> |
| Optional 32bit Unsigned Int | Option<u32> |
| Optional 16bit Unsigned Int | Option<u16> |
| Optional 8bit Unsigned Int | Option<u8> |
| Optional Pointer-sized Int | Option<usize> |
| Vector (128bit) | Vec<u128> |
| Vector (64bit) | Vec<u64> |
| Vector (64bit) | Vec<u64> |
| Vector (32bit) | Vec<u32> |
| Vector (16bit) | Vec<u16> |
| Vector (8bit) | Vec<u8> |
| Vector (Pointer-sized) | Vec<usize> |
Usage
Configuration Options
To customize your hash settings, such as the salt value or the minimum length of the generated hash strings, utilize the SerdeHashOptions builder. The following options are available:
| Name | Default Value | Description |
|---|---|---|
| salt | Generated randomly | The cryptographic salt used for hash generation |
| min_length | 8 | Minimum length of the generated hash string |
| alphabet | Alphanumeric (a-zA-Z0-9) | Characters used for hash encoding |
Simpliest example:
use SerdeHashOptions;
A more complete example:
use SerdeHashOptions;
Basic Example
To use serde_hash with your data structures, add the HashIds derive macro and mark the fields you want to hash with #[hash]:
use SerdeHashOptions;
use HashIds;
// Define your data structure with the HashIds derive macro
Deserialization Example
The library seamlessly handles deserialization, converting the hash strings back to original numeric IDs:
use SerdeHashOptions;
use HashIds;
Using with Vectors
The #[hash] attribute can also be applied to vectors of unsigned integers. When applied to a vector field, each element in the vector will be hashed individually:
use SerdeHashOptions;
use HashIds;
When serialized, each value in the vector will be individually hashed, and during deserialization, each hashed string will be converted back to its original numeric value. This feature is useful when working with collections of IDs that need to be obfuscated in your API responses.
Generating Secure Salt
For production use, it's recommended to use a cryptographically secure random salt:
use ;
use HashIds;
Why Use serde_hash?
- Obfuscation: Hide your internal database IDs from API consumers
- Predictability: Unlike UUID generation, the same ID will always hash to the same string with the same settings
- Transparency: Your application code can work with numeric IDs while your API exposes hash strings
- Simplicity: Add one derive macro and configure once, no need to manually encode/decode IDs
License
This project is built with Rust and uses the hash-ids crate for hash ID generation.