Skip to content

Instantly share code, notes, and snippets.

@ablanathtanalba
Created September 10, 2019 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ablanathtanalba/d529e55c2a97bb80b815db3dd5348348 to your computer and use it in GitHub Desktop.
Save ablanathtanalba/d529e55c2a97bb80b815db3dd5348348 to your computer and use it in GitHub Desktop.
testing
import math
def estimate_max_entropy(str):
MAX_LS_LEN_FOR_ENTROPY_EST = 256
SEPS = "._-"
BIN = "01"
DEC = "0123456789"
HEX = "abcdef" + DEC
ALPHA = "abcdefghijklmnopqrstuvwxyz"
ALPHANUM = ALPHA + DEC
B64 = ALPHANUM + ALPHA.upper() + "/+"
URL = ALPHANUM + ALPHA.upper() + "~%"
if len(str) > MAX_LS_LEN_FOR_ENTROPY_EST:
return len(str)
max_symbols = None
same_case = str.lower() == str or str.upper() == str
if same_case:
str = str.lower()
chr_classes = [BIN, DEC, HEX, ALPHA, ALPHANUM, B64, URL]
for chr_class in chr_classes:
group = chr_class + SEPS
each_char_in_group = None
for char in str:
if char in str:
if not each_char_in_group:
each_char_in_group = True
else:
each_char_in_group = False
if each_char_in_group:
max_symbols = len(chr_class)
break
# clunkier than the JS version
if not max_symbols:
char_codes = []
char_list = list(str)
for char in char_list:
char_codes.append(ord(char))
# arbitrary values, meh
min_char_code = char_codes[0]
max_char_code = char_codes[1]
for char in char_codes:
if char < min_char_code:
min_char_code = char
elif char > max_char_code:
max_char_code = char
max_symbols = max_char_code - min_char_code + 1
max_bits = (math.log(max_symbols)/math.log(2)) * len(str)
return max_bits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment