[docs]@staticmethoddefverifypw(password,salt,encryptedVerifier,encryptedVerifierHash):r""" Return True if the given password is valid. >>> password = 'password1' >>> salt = b'\xe8w,\x1d\x91\xc5j7\x96Ga\xb2\x80\x182\x17' >>> encryptedVerifier = b'\xc9\xe9\x97\xd4T\x97=1\x0b\xb1\xbap\x14&\x83~' >>> encryptedVerifierHash = b'\xb1\xde\x17\x8f\x07\xe9\x89\xc4M\xae^L\xf9j\xc4\x07' >>> DocumentRC4.verifypw(password, salt, encryptedVerifier, encryptedVerifierHash) True """# https://msdn.microsoft.com/en-us/library/dd952648(v=office.12).aspxblock=0key=_makekey(password,salt,block)cipher=Cipher(algorithms.ARC4(key),mode=None,backend=default_backend())decryptor=cipher.decryptor()verifier=decryptor.update(encryptedVerifier)verfiferHash=decryptor.update(encryptedVerifierHash)hash=md5(verifier).digest()logging.debug([verfiferHash,hash])returnhash==verfiferHash
[docs]@staticmethoddefdecrypt(password,salt,ibuf,blocksize=0x200):r""" Return decrypted data. """obuf=io.BytesIO()block=0key=_makekey(password,salt,block)forc,bufinenumerate(iter(functools.partial(ibuf.read,blocksize),b"")):cipher=Cipher(algorithms.ARC4(key),mode=None,backend=default_backend())decryptor=cipher.decryptor()dec=decryptor.update(buf)+decryptor.finalize()obuf.write(dec)# From wvDecrypt:# at this stage we need to rekey the rc4 algorithm# Dieter Spaar <spaar@mirider.augusta.de> figured out# this rekeying, big kudos to himblock+=1key=_makekey(password,salt,block)obuf.seek(0)returnobuf