This code is a script that is used to download, decrypt, and extract data from a remote server using the Arweave network. The script uses the Arweave unique address (ar) and the cryptographic key (key) to securely access the data.
The script starts by importing several modules that are necessary for its operation, including os, base64, hashlib, json, cryptography, requests, and Session. It then defines several variables, including filename, debug, Seed, KeyDerivation, and session.
The Seed class holds the key, ar, and checksum that are used to access the data on the Arweave network. The KeyDerivation class is used to generate a new key that is used to encrypt the data. The session variable is used to create a new session and make a request to the Arweave network.
The script then defines several functions, including decrypt, get, and extract. The decrypt function takes the data and key as input, and decrypts the data using the AESGCM algorithm. The get function takes the ar as input and retrieves the data from the Arweave network. The extract function takes the key, ar, and checksum as input and extracts the data by verifying the checksum of the data using the sha3_256 algorithm.
The script also has a main block of code that is executed when the script is run. In this block, the script loads the seed data, generates a new key using the KeyDerivation class, and then uses the extract function to download, decrypt, and extract the data from the Arweave network. If the debug variable is set to True, the script will print out the data, otherwise, it will write the data to a file.
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.杭州学军中学相关.20211001.113873b9.7z"
debug: bool = False
class Seed:
key: bytes = urlsafe_b64decode(
b"MOfhR-o3i5HlzojCpxdtdO_vcqWbQBagCRjq79HxiNTQb95ayTN1rD_LX7knX-f_bTmLu3CnQRTtTbaS_agS_w=="
)
ar: str = "8E2C9gHv6cEzBw4hfaqVC651SusFHgUXPPhBV6IAt14"
checksum: str = "EsNpUwIdKDSW0R6ErsVnuH9qWvMRSILvRY9zI3ORRt0="
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"]))