Last active
August 29, 2015 14:18
-
-
Save clayg/959a5d77353a91a4b10a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
from hashlib import md5 | |
from pyeclib.ec_iface import ECDriver | |
try: | |
ec_type = sys.argv[1] | |
except IndexError: | |
sys.exit('usage: %s <ec_type>' % sys.argv[0]) | |
driver = ECDriver(k=4, m=2, ec_type=ec_type) | |
SEGSIZE = 1048576 | |
def make_fragment_payloads(driver, test_body): | |
# split up the body into buffers | |
chunks = [test_body[x:x + SEGSIZE] | |
for x in range(0, len(test_body), SEGSIZE)] | |
# encode the buffers into fragment payloads | |
fragment_payloads = [] | |
for chunk in chunks: | |
fragments = driver.encode(chunk) | |
if not fragments: | |
break | |
fragment_payloads.append(fragments) | |
return fragment_payloads | |
test_body = ('crazy' * SEGSIZE)[:-565] | |
fragment_payloads = make_fragment_payloads(driver, test_body) | |
index_to_rebuild = 5 | |
index_set1 = [0, 1, 2, 4] | |
rebuild_hash1 = md5() | |
decode_hash1 = md5() | |
for frags in fragment_payloads: | |
payload = [frags[i] for i in index_set1] | |
fragment = driver.reconstruct(payload, [index_to_rebuild])[0] | |
rebuild_hash1.update(fragment) | |
segment = driver.decode(payload) | |
decode_hash1.update(segment) | |
fragments = driver.encode(segment) | |
# assert fragment == fragments[index_to_rebuild] | |
index_set2 = [0, 1, 2, 3] | |
rebuild_hash2 = md5() | |
decode_hash2 = md5() | |
for frags in fragment_payloads: | |
payload = [frags[i] for i in index_set2] | |
fragment = driver.reconstruct(payload, [index_to_rebuild])[0] | |
rebuild_hash2.update(fragment) | |
segment = driver.decode(payload) | |
decode_hash2.update(segment) | |
fragments = driver.encode(segment) | |
assert fragment == fragments[index_to_rebuild] | |
print "decode doesn't care about index" | |
print md5(test_body).hexdigest() | |
print decode_hash1.hexdigest() | |
print decode_hash2.hexdigest() | |
print "reconstruct seems to care about index" | |
print rebuild_hash1.hexdigest() | |
print rebuild_hash2.hexdigest() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment