bit-entropy
v1.0.0
Published
Calculates the bit-level entropy of a string or ID space given its cardinality and length.
Downloads
8
Maintainers
Readme
bit-entropy
Canonical URL:
https://alexstevovich.com/a/bit-entropy-nodejs
Software URL:
https://midnightcitylights.com/software/bit-entropy-nodejs
A utility to calculate the bit-level entropy of a string or identifier space based on its cardinality (number of unique symbols) and length.
This utility is based on information theory and provides a quick way to calculate the amount of information (in bits) that can be encoded using a string of a given length over a finite character set.
Installation
npm install bit-entropyExample
import { bitEntropy } from 'bit-entropy';
console.log(bitEntropy(2, 8)); // 8 bits (e.g., binary byte)
console.log(bitEntropy(16, 32)); // 128 bits (e.g., UUID v4)
console.log(bitEntropy(62, 10)); // ~59.5 bits (Base62 string of length 10)API
bitEntropy(cardinality, length)
Calculates the entropy in bits of a string or ID space.
Parameters:
cardinality(number): The number of unique symbols in the character set (e.g., 62 for Base62).length(number): The length of the string or identifier.
Returns:
number: The total entropy in bits.
Notes
- Cardinality refers to the number of unique symbols available (e.g., 62 for Base62 encoding).
- Length refers to the length of the string or identifier.
- This utility is based on the formula:
entropy = length * log2(cardinality).
License
Licensed under the Apache License 2.0.
