Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b2ed9d

Browse files
committed
Python: Align cryptodome tests
1 parent 2c0df8e commit 1b2ed9d

5 files changed

Lines changed: 90 additions & 1 deletion

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
These tests are a copy of the tests in [../cryptodome](../cryptodome) with `Cryptodome` replaced by `Crypto`.
2+
3+
You can run the following command to update the tests:
4+
5+
```sh
6+
rm *.py && cp ../cryptodome/*.py . && sed -i -e 's/Cryptodome/Crypto/' *.py
7+
```
8+
9+
The original [`pycrypto` PyPI package](https://pypi.org/project/pycrypto/) that provided the `Crypto` Python package has not been updated since 2013, so it is reasonable to assume that people will use the replacement [`pycryptodome` PyPI package](https://pypi.org/project/pycryptodome/) that has a (mostly) compatible API.
10+
11+
The pycryptodome functionality is also available in the [`pycryptodomex` PyPI package](https://pypi.org/project/pycryptodomex/) which provides the `Cryptodome` Python package.
12+
13+
To ensure our modeling actually covers _both_ ways of importing the same functionality, we have this convoluted test setup.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# https://pycryptodome.readthedocs.io/en/latest/src/cipher/aes.html
2+
from Crypto.Cipher import AES
3+
4+
import os
5+
6+
key = os.urandom(256//8)
7+
iv = os.urandom(16)
8+
9+
# ------------------------------------------------------------------------------
10+
# encrypt/decrypt
11+
# ------------------------------------------------------------------------------
12+
13+
14+
print("encrypt/decrypt")
15+
16+
secret_message = b"secret message"
17+
18+
padding_len = 16 - (len(secret_message) % 16)
19+
padding = b"\0"*padding_len
20+
21+
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
22+
# using separate .encrypt calls on individual lines does not work
23+
whole_plantext = secret_message + padding
24+
encrypted = cipher.encrypt(whole_plantext)
25+
26+
print("encrypted={}".format(encrypted))
27+
28+
print()
29+
30+
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
31+
decrypted = cipher.decrypt(encrypted)
32+
33+
decrypted = decrypted[:-padding_len]
34+
35+
print("decrypted={}".format(decrypted))
36+
assert decrypted == secret_message
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from Crypto.Hash import MD5
2+
3+
hasher = MD5.new(b"secret message") # $ MISSING: CryptographicOperation CryptographicOperationInput=b"secret message" CryptographicOperationAlgorithm=MD5
4+
print(hasher.hexdigest())
5+
6+
7+
hasher = MD5.new() # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=MD5
8+
hasher.update(b"secret") # $ MISSING: CryptographicOperation CryptographicOperationInput=b"secret" CryptographicOperationAlgorithm=MD5
9+
hasher.update(b" message") # $ MISSING: CryptographicOperation CryptographicOperationInput=b" message" CryptographicOperationAlgorithm=MD5
10+
print(hasher.hexdigest())
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# https://pycryptodome.readthedocs.io/en/latest/src/cipher/arc4.html
2+
from Crypto.Cipher import ARC4
3+
4+
import os
5+
6+
key = os.urandom(256//8)
7+
8+
9+
# ------------------------------------------------------------------------------
10+
# encrypt/decrypt
11+
# ------------------------------------------------------------------------------
12+
13+
14+
15+
print("encrypt/decrypt")
16+
17+
secret_message = b"secret message"
18+
19+
cipher = ARC4.new(key)
20+
encrypted = cipher.encrypt(secret_message)
21+
22+
print("encrypted={}".format(encrypted))
23+
24+
print()
25+
26+
cipher = ARC4.new(key)
27+
decrypted = cipher.decrypt(encrypted)
28+
29+
print("decrypted={}".format(decrypted))
30+
assert decrypted == secret_message

python/ql/test/library-tests/frameworks/crypto/test_rsa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858

5959
print()
6060

61-
6261
verifier = pss.new(public_key)
62+
6363
hasher = SHA256.new(message)
6464
verifier.verify(hasher, signature)
6565
print("Signature verified (as expected)")

0 commit comments

Comments
 (0)