Best Python code snippet using pandera_python
pyaescrypt.py
Source:pyaescrypt.py
...23 with open(path, "rb") as fIn:24 fdata = fIn.read(3)25 # check if file is in AES Crypt format (also min length check)26 if fdata != bytes("AES", "utf8") or os.stat(path).st_size < 136:27 fail_with_msg("Error: file is corrupted or " +28 "not an AES Crypt (or pyAesCrypt) file.")29 # check if file is in AES Crypt format, version 230 # (the only one compatible with pyAesCrypt)31 fdata = fIn.read(1)32 if len(fdata) < 1:33 fail_with_msg("Error: file is corrupted.")34 if fdata != b"\x02":35 fail_with_msg("Error: pyAesCrypt is only compatible with version 2 of the " +36 "AES Crypt file format.")37 # skip reserved byte38 fIn.read(1)39 # skip all the extensions40 while True:41 fdata = fIn.read(2)42 if len(fdata) < 2:43 fail_with_msg("Error: file is corrupted.")44 if fdata == b"\x00\x00":45 break46 fIn.read(int.from_bytes(fdata, byteorder="big"))47 # read external iv48 iv1 = fIn.read(16)49 if len(iv1) < 16:50 fail_with_msg("Error: file is corrupted.")51 # stretch password and iv52 key=pyAesCryptStretch(passw, iv1)53 # read encrypted main iv and key54 c_iv_key = fIn.read(48)55 if len(c_iv_key) < 48:56 fail_with_msg("Error: file is corrupted.")57 # read HMAC-SHA256 of the encrypted iv and key58 hmac1 = fIn.read(32)59 if len(hmac1) < 32:60 fail_with_msg("Error: file is corrupted.")61 # compute actual HMAC-SHA256 of the encrypted iv and key62 hmac1Act = HMAC.new(key, digestmod=SHA256)63 hmac1Act.update(c_iv_key)64 # HMAC check65 if hmac1 != hmac1Act.digest():66 fail_with_msg("Error: wrong password (or file is corrupted).")67 # instantiate AES cipher68 cipher1 = AES.new(key, AES.MODE_CBC, iv1)69 # decrypt main iv and key70 iv_key = cipher1.decrypt(c_iv_key)71 # get internal iv and key72 iv0 = iv_key[:16]73 intKey = iv_key[16:]74 # instantiate another AES cipher75 cipher0 = AES.new(intKey, AES.MODE_CBC, iv0)76 # instantiate actual HMAC-SHA256 of the ciphertext77 hmac0Act = HMAC.new(intKey, digestmod=SHA256)78 # decrypt ciphertext in large pieces first, then smaller pieces79 sizeInputFile = os.stat(path).st_size80 for currentBufferSize in [64*64*AES.block_size, 64*AES.block_size, AES.block_size]:81 assert 0 == currentBufferSize % AES.block_size82 while fIn.tell() < sizeInputFile - 32 - 1 - currentBufferSize:83 # read data84 cText = fIn.read(currentBufferSize)85 # update HMAC86 hmac0Act.update(cText)87 # decrypt data88 if fncallback:89 fncallback(cipher0.decrypt(cText))90 else:91 outbytes += bytearray(cipher0.decrypt(cText))92 # last block reached, remove padding if needed93 # read last block94 if fIn.tell() != os.stat(path).st_size - 32 - 1: # this is for empty files95 cText = fIn.read(AES.block_size)96 if len(cText) < AES.block_size:97 fail_with_msg("Error: file is corrupted.")98 else:99 cText = bytes()100 # update HMAC101 hmac0Act.update(cText)102 # read plaintext file size mod 16 lsb positions103 fs16 = fIn.read(1)104 if len(fs16) < 1:105 fail_with_msg("Error: file is corrupted.")106 # decrypt last block107 pText = cipher0.decrypt(cText)108 # remove padding109 toremove=((16-fs16[0])%16)110 if toremove != 0:111 pText=pText[:-toremove]112 if fncallback:113 fncallback(pText)114 else:115 outbytes += bytearray(pText)116 # read HMAC-SHA256 of the encrypted file117 hmac0 = fIn.read(32)118 if len(hmac0) < 32:119 fail_with_msg("Error: file is corrupted.")120 # HMAC check121 if hmac0 != hmac0Act.digest():122 fail_with_msg("Error: bad HMAC (file is corrupted).")123 return outbytes124def pyAesCryptStretch(passw, iv1):125 # hash the external iv and the password 8192 times126 from Crypto.Hash import SHA256127 digest=iv1+(16*b"\x00")128 for _ in range(8192):129 passHash=SHA256.new()130 passHash.update(digest)131 passHash.update(bytes(passw,"utf_16_le"))132 digest=passHash.digest()133 return digest134def fail_with_msg(s):135 print(s)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!