This code is a program that is used to decrypt and download a file from the internet. The program uses a key, a unique identifier called an "ar" and a checksum to decrypt and verify the file's integrity. The program uses the Arweave network to download the file and then it uses the AESGCM algorithm to decrypt the file using the provided key. The key, ar, and checksum are all defined in a class called 'Seed'. The program also uses the sha3_256 and sha3_512 algorithms to verify the checksum of the file and ensure that the downloaded file is the correct one. The program then uses a KeyDerivation class to generate a key for decryption. Finally, the program writes the decrypted file to the local disk using the filename specified in the code.
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-002.Oblivion.白马湖中学相关.20210925.9c1ea043.7z"
debug: bool = False
class Seed:
key: bytes = urlsafe_b64decode(
b"wSfTeqx_GZnqnePgjujqWIscDZbEiXGO0tdpOhczPuOWOWolma3gkYEhFx61hdiKA6RvUcjMjdg9B9A2PHoUBw=="
)
ar: str = "SNc3VxAtzs4S2moTWL3Nb6Fb2vd5P-feZqVsyOtyHGo"
checksum: str = "QlRtkM_GA_hHpRbKaLfcfx_cdq_qiPRGQzSCDACMCh4="
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"]))