8 releases

0.4.3 Jan 23, 2026
0.4.2 Oct 18, 2023
0.4.1 Jun 3, 2021
0.3.0 Apr 20, 2017
0.1.1 Apr 20, 2017

#231 in Algorithms

Download history 2073/week @ 2025-12-14 1014/week @ 2025-12-21 1006/week @ 2025-12-28 1403/week @ 2026-01-04 1563/week @ 2026-01-11 1834/week @ 2026-01-18 3073/week @ 2026-01-25 1308/week @ 2026-02-01 2330/week @ 2026-02-08 1970/week @ 2026-02-15 2183/week @ 2026-02-22 2833/week @ 2026-03-01 2387/week @ 2026-03-08 2206/week @ 2026-03-15 2251/week @ 2026-03-22 2170/week @ 2026-03-29

9,165 downloads per month
Used in 15 crates (10 directly)

MIT license

10KB
84 lines

entropy

Crates.io Documentation License

A Rust library for calculating Shannon entropy and metric entropy of byte sequences.

Installation

Add this to your Cargo.toml:

[dependencies]
entropy = "0.4"

Usage

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);

What is Shannon Entropy?

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)

Common Use Cases

  • Cryptography - Measuring randomness of keys or random number generators
  • Data compression - Estimating how compressible data is
  • Malware analysis - Detecting packed or encrypted executables
  • Password strength - Estimating password complexity

No runtime deps