Skip to content

Instantly share code, notes, and snippets.

@x684867
Created December 31, 2014 20:32
Show Gist options
  • Save x684867/1b1411c7418538cdf294 to your computer and use it in GitHub Desktop.
Save x684867/1b1411c7418538cdf294 to your computer and use it in GitHub Desktop.
Key distribution
#!/usr/bin/env python
#
import hashlib
def hasher(s):
m=hashlib.sha1()
m.update(s)
return m.hexdigest()
def stringify_keyspace(a):
s=''
for i in a: s+=chr(i)
return s[::-1]
def deserialize_hash(h):
a=[]
for i in h: a.append(i)
return a
def stringify_block(h,b):
s=''
for i in range(0,b): s+=h[i]
return s
"""
Analyze frequency of starting blocks (of size b) in
The hash (h) to determine optimal block size for
subtree roots.
"""
def analyze_distribution(h,b,a,grouping=0,blockcount=0):
try:
a[grouping][stringify_block(h,b)]+=1
except:
a[grouping][stringify_block(h,b)]=1
return a
"""
create the analysis table (empty)
"""
def init_analysis(groupingStart=0,groupingFinish=100):
a=[]
for i in range(groupingStart,groupingFinish):
a.append({})
return a
def increment_array(a,i,zeroState=0,highState=255):
if a[i] == highState:
try:
increment_array(a,i+1)
except:
a.append(zeroState)
a[i]=zeroState
else:
a[i]+=1
blocksize=4
keyspace=[0]
counter=0
groupingStart=0
groupingEnd=1
analysis=init_analysis(groupingStart,groupingEnd)
while len(keyspace) <=2:
increment_array(keyspace,0)
hash_string=hasher(stringify_keyspace(keyspace))
hash_array=deserialize_hash(hash_string)
#Todo: determine grouping (e.g. tenths or 2TB blocks)
grouping=0
analysis=analyze_distribution(hash_array,blocksize,analysis,grouping)
counter+=1
print("{}: {}".format(hash_string,keyspace))
block_count=0
for group in analysis:
for i in group:
block_count+=1
print "="*60
print "Analysis:"
print "="*60
print "block count:{}".format(block_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment