Tooling for managing asset compression, storage, and retrieval
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.1 KiB

  1. import hashlib
  2. import io
  3. import logging
  4. # Size of the buffer to read files with
  5. BUF_SIZE = 4096
  6. LOG = logging.getLogger("acm.utility")
  7. def get_file_sha256sum(input_file):
  8. sha256sum = hashlib.sha256()
  9. with open(input_file, 'rb') as f:
  10. for byte_block in iter(lambda: f.read(BUF_SIZE), b""):
  11. sha256sum.update(byte_block)
  12. return sha256sum.hexdigest()
  13. def get_string_sha256sum(content: str, encoding='utf-8') -> str:
  14. sha256sum = hashlib.sha256()
  15. with io.BytesIO(content.encode(encoding)) as c:
  16. for byte_block in iter(lambda: c.read(BUF_SIZE), b''):
  17. sha256sum.update(byte_block)
  18. return sha256sum.hexdigest()
  19. def get_string_hex(content: str) -> hex:
  20. return hex(int(content, base=16))
  21. def get_hex_xor(first: hex, second: hex) -> hex:
  22. return hex(int(first, base=16) ^ int(second, base=16))
  23. def get_string_xor(first: str, second: str, *extra: str) -> str:
  24. result = get_hex_xor(get_string_hex(first), get_string_hex(second))
  25. for next_hex in extra:
  26. result = get_hex_xor(result, get_string_hex(next_hex))
  27. return str(result)