model-scan-3 / scan_hash.py
pengdaqian
init scan
cb34746
raw
history blame
1.96 kB
import hashlib
import os.path
import sys
from typing import Dict
import binascii
import crcmod
import pure_blake3
def get_hash_string(hash_bytes: bytes) -> str:
return binascii.hexlify(hash_bytes).decode()
def compute_AutoV1Hash(file_stream) -> str:
minFileSize = 0x100000 * 2
if file_stream.seek(0, 2) < minFileSize:
return None
file_stream.seek(0x100000)
buffer = file_stream.read(0x10000)
hashBytes = hashlib.sha256(buffer).digest()
hashString = get_hash_string(hashBytes)
return hashString[:8]
def ComputeCRC32Hash(file_stream) -> str:
crc32 = crcmod.predefined.Crc('crc-32c')
file_stream.seek(0)
for chunk in iter(lambda: file_stream.read(4096), b""):
crc32.update(chunk)
return get_hash_string(crc32.digest())
def generate_model_hashes(file_path: str) -> Dict[str, str]:
if file_path.startswith("http"):
import urllib.request
tmp_path = f'/tmp/clamd_{file_path.split("/")[-1]}'
if os.path.exists(tmp_path):
pass
else:
urllib.request.urlretrieve(file_path, tmp_path)
file_path = tmp_path
sha256 = hashlib.sha256()
blake3Hasher = pure_blake3.Hasher()
with open(file_path, "rb") as fileStream:
for chunk in iter(lambda: fileStream.read(4096), b""):
sha256.update(chunk)
blake3Hasher.update(chunk)
sha256HashString = get_hash_string(sha256.digest())
autoV1HashString = compute_AutoV1Hash(open(file_path, "rb"))
autoV2HashString = sha256HashString[:10]
blake3HashString = blake3Hasher.finalize().hex()
crc32HashString = ComputeCRC32Hash(open(file_path, "rb"))
result = {
"SHA256": sha256HashString,
"AutoV1": autoV1HashString,
"AutoV2": autoV2HashString,
"BLAKE3": blake3HashString,
"CRC32": crc32HashString,
}
return result
if __name__ == "__main__":
print(generate_model_hashes(".gitignore"))