import functools import io import logging from hashlib import sha1 from struct import pack from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.ciphers import Cipher try: # NOTE: Avoid DeprecationWarning since cryptography>=43.0 # TODO: .algorithm differs from the official documentation from cryptography.hazmat.decrepit.ciphers.algorithms import ARC4 except ImportError: from cryptography.hazmat.primitives.ciphers.algorithms import ARC4 logger = logging.getLogger(__name__) logger.addHandler(logging.NullHandler()) def _makekey(password, salt, keyLength, block, algIdHash=0x00008004): r""" Return a intermediate key. """ # https://msdn.microsoft.com/en-us/library/dd920677(v=office.12).aspx password = password.encode("UTF-16LE") h0 = sha1(salt + password).digest() blockbytes = pack(" figured out # this rekeying, big kudos to him block += 1 key = _makekey(password, salt, keySize, block) obuf.seek(0) return obuf