import os
from cryptography.fernet import Fernet
from reedsolo import RSCodec
CHUNK_SIZE = 32
DATA_SEGMENTS = 4
TOTAL_SEGMENTS = 8
rsc = RSCodec(TOTAL_SEGMENTS - DATA_SEGMENTS)
key = Fernet.generate_key()
cipher = Fernet(key)
original_data = b"StorjTestData1234567890!ThisIsAChunkTestFile!!"
print(f"元データ: {original_data}")
encrypted_data = cipher.encrypt(original_data)
print(f"暗号化データ: {encrypted_data}")
chunks = [encrypted_data[i:i+CHUNK_SIZE] for i in range(0, len(encrypted_data), CHUNK_SIZE)]
def encode_chunk(chunk):
encoded = rsc.encode(chunk)
seg_size = len(encoded) // TOTAL_SEGMENTS
return [encoded[i:i+seg_size] for i in range(0, len(encoded), seg_size)]
distributed_chunks = []
for chunk in chunks:
segments = encode_chunk(chunk)
distributed_chunks.append(segments)
for i, chunk_segs in enumerate(distributed_chunks):
print(f"\n チャンク{i+1}:")
for j, seg in enumerate(chunk_segs):
print(f" ノード{j}: {seg}")
def recover_chunk(pieces):
combined = b''.join(pieces)
return rsc.decode(combined)
recovered_encrypted = b''
for chunk_segs in distributed_chunks:
selected_pieces = chunk_segs[:DATA_SEGMENTS]
recovered = recover_chunk(selected_pieces)
recovered_encrypted += recovered
decrypted_data = cipher.decrypt(recoved_encrypted)
print(f"\n復元された平文: {decrypted_data}")
$ python3 storj.py
元データ: b’StorjTestData1234567890!ThisIsAChunkTestFile!!’
暗号化データ: b’gAAAAABoLw7a8YpRJbYfA1bt6JOd9Rn28BVtmziV9-qXo9pWs41Or5LN8J0J3UKgAB7uTmpXjkHgTiEv_Dn4ajnfJATxU4lkoOS8xI67SmVeORdvPm6O6OEqGYBXfhQAGaBEtun2jHOw’
チャンク1:
ノード0: bytearray(b’gAAA’)
ノード1: bytearray(b’AABo’)
ノード2: bytearray(b’Lw7a’)
ノード3: bytearray(b’8YpR’)
ノード4: bytearray(b’JbYf’)
ノード5: bytearray(b’A1bt’)
ノード6: bytearray(b’6JOd’)
ノード7: bytearray(b’9Rn2′)
ノード8: bytearray(b”3\’\xf8\xd8″)
チャンク2:
ノード0: bytearray(b’8BVt’)
ノード1: bytearray(b’mziV’)
ノード2: bytearray(b’9-qX’)
ノード3: bytearray(b’o9pW’)
ノード4: bytearray(b’s41O’)
ノード5: bytearray(b’r5LN’)
ノード6: bytearray(b’8J0J’)
ノード7: bytearray(b’3UKg’)
ノード8: bytearray(b’\x1b\xe3=\xc7′)
チャンク3:
ノード0: bytearray(b’AB7u’)
ノード1: bytearray(b’TmpX’)
ノード2: bytearray(b’jkHg’)
ノード3: bytearray(b’TiEv’)
ノード4: bytearray(b’_Dn4′)
ノード5: bytearray(b’ajnf’)
ノード6: bytearray(b’JATx’)
ノード7: bytearray(b’U4lk’)
ノード8: bytearray(b’t\x915\xa3′)
チャンク4:
ノード0: bytearray(b’oOS8′)
ノード1: bytearray(b’xI67′)
ノード2: bytearray(b’SmVe’)
ノード3: bytearray(b’ORdv’)
ノード4: bytearray(b’Pm6O’)
ノード5: bytearray(b’6OEq’)
ノード6: bytearray(b’GYBX’)
ノード7: bytearray(b’fhQA’)
ノード8: bytearray(b’e\x02al’)
チャンク5:
ノード0: bytearray(b’Ga’)
ノード1: bytearray(b’BE’)
ノード2: bytearray(b’tu’)
ノード3: bytearray(b’n2′)
ノード4: bytearray(b’jH’)
ノード5: bytearray(b’Ow’)
ノード6: bytearray(b’\x00\xea’)
ノード7: bytearray(b’\x8b\x07′)