| Crates.io | entropy |
| lib.rs | entropy |
| version | 0.4.3 |
| created_at | 2017-04-20 02:11:13.920997+00 |
| updated_at | 2026-01-23 20:21:08.835104+00 |
| description | Calculates the Shannon entropy of arrays of bytes and strings |
| homepage | https://github.com/smackysnacks/entropy |
| repository | https://github.com/smackysnacks/entropy |
| max_upload_size | |
| id | 11261 |
| size | 27,290 |
A Rust library for calculating Shannon entropy and metric entropy of byte sequences.
Add this to your Cargo.toml:
[dependencies]
entropy = "0.4"
use entropy::{shannon_entropy, metric_entropy};
// Calculate Shannon entropy (in bits)
let h = shannon_entropy("hello, world");
assert_eq!(h, 3.0220551);
// Works with byte slices too
let h = shannon_entropy(b"\x00\x01\x02\x03");
// Calculate metric entropy (shannon_entropy / input.len())
let m = metric_entropy("hello, world");
assert_eq!(m, 0.25183794);
Shannon entropy measures the average amount of information contained in a message, expressed in bits. For byte data:
| Entropy Value | Meaning |
|---|---|
| 0 | Completely uniform (e.g., "aaaa") |
| 8 | Maximum randomness (all 256 byte values equally distributed) |