This code is a script for decrypting and extracting data stored on the Arweave decentralized file storage network. It uses the AESGCM encryption algorithm to decrypt the data and the sha3_256 and sha3_512 hash functions to verify the integrity of the data. The script uses the "Session" class from the "requests" library to interact with the Arweave network and retrieve the encrypted data using the provided "ar" and "checksum" values. The decryption key is generated using the "KeyDerivation" class and the "iv" and "next" values from a seed file. The decrypted data is then either printed to the console for debugging or written to a specified file on the local machine.
import os
from base64 import urlsafe_b64decode
from hashlib import sha3_256, sha3_512
from json import loads
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from requests.models import Response
from requests.sessions import Session
filename: str = r"A-001.Algha_Porthos.杭州绿城育华学校相关.20210925.b47ea2d6.7z"
debug: bool = False
class Seed:
key: bytes = urlsafe_b64decode(
b"f0L4LKN1z92bbS6HLabU-d9DWc8QGW0sNhMHIl8zGDNKAA3WPrPjyO5N4F07bHlczQOw7X_6V0sEw1biBZDzOg=="
)
ar: str = "OkhK-pddJc2_blhAkbedsOu3e4YC8Ykab6wNENs2BgM"
checksum: str = "EVfbTS4kENG299QZrGwPxst4F8ng_8S7pNCzAkq4DM0="
class KeyDerivation:
def __init__(self, iv: bytes, next: bytes) -> None:
assert len(iv) == 256, "iv must be 2048 bits"
assert len(next) == 64, "next must be 512 bits"
self.__generator__ = sha3_512(iv)
self.__next__ = next
def __call__(self) -> bytes:
self.__generator__.update(self.__generator__.digest())
self.__generator__.update(self.__next__)
return self.__generator__.digest()
def decrypt(data: bytes, key: bytes) -> bytes:
assert len(key) == 64, "key must be 512 bits"
return AESGCM(key=key[:32]).decrypt(
nonce=key[-12:], data=data, associated_data=key[32:-12]
)
session = Session()
def get(ar: str) -> Response:
print("ar", ar)
while True:
try:
resp = session.get(f"https://arweave.net/{ar}/d")
resp.raise_for_status()
return resp
except Exception as err:
print("error", err)
def extract(key: bytes, ar: str, checksum: str) -> bytes:
checksum = urlsafe_b64decode(checksum)
checked = False
while not checked:
data = get(ar).content[12:]
checked = checksum == (sha3_256(data).digest())
return decrypt(data=data, key=key)
if __name__ == "__main__":
seed = loads(extract(key=Seed.key, ar=Seed.ar, checksum=Seed.checksum))
keyDerivation = KeyDerivation(
iv=urlsafe_b64decode(seed["crypto"]["iv"]),
next=urlsafe_b64decode(seed["crypto"]["next"]),
)
print("debug", debug)
if debug:
for obj in seed["store"]:
key = keyDerivation()
extract(key=key, ar=obj["ar"], checksum=obj["checksum"])
else:
import sys
if getattr(sys, "frozen", False):
current = os.path.dirname(sys.executable)
else:
current = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(current, filename), "wb") as fout:
for obj in seed["store"]:
key = keyDerivation()
fout.write(extract(key=key, ar=obj["ar"], checksum=obj["checksum"]))