|
| 1 | +# Following examples from https://stuvel.eu/python-rsa-doc/usage.html |
| 2 | +import rsa |
| 3 | + |
| 4 | +# using a rather low keysize, since otherwise it takes quite long to run. |
| 5 | +(public_key, private_key) = rsa.newkeys(512) # $ MISSING: PublicKeyGeneration keySize=2048 |
| 6 | +(public_key, private_key) = rsa.newkeys(nbits=512) # $ MISSING: PublicKeyGeneration keySize=2048 |
| 7 | + |
| 8 | + |
| 9 | +# ------------------------------------------------------------------------------ |
| 10 | +# encrypt/decrypt |
| 11 | +# ------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +# Note: These are using PKCS#1 v1.5 |
| 14 | + |
| 15 | +print("encrypt/decrypt") |
| 16 | + |
| 17 | +secret_message = b"secret message" |
| 18 | + |
| 19 | +encrypted = rsa.encrypt(secret_message, public_key) # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=RSA CryptographicOperationInput=secret_message |
| 20 | +encrypted = rsa.encrypt(message=secret_message, pub_key=public_key) # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=RSA CryptographicOperationInput=secret_message |
| 21 | + |
| 22 | +print("encrypted={}".format(encrypted)) |
| 23 | + |
| 24 | +print() |
| 25 | + |
| 26 | +decrypted = rsa.decrypt(encrypted, private_key) # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=RSA CryptographicOperationInput=encrypted |
| 27 | +decrypted = rsa.decrypt(crypto=encrypted, priv_key=private_key) # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=RSA CryptographicOperationInput=encrypted |
| 28 | + |
| 29 | +print("decrypted={}".format(decrypted)) |
| 30 | +assert decrypted == secret_message |
| 31 | + |
| 32 | +print("\n---\n") |
| 33 | + |
| 34 | +# ------------------------------------------------------------------------------ |
| 35 | +# sign/verify |
| 36 | +# ------------------------------------------------------------------------------ |
| 37 | + |
| 38 | +# Note: These are using PKCS#1 v1.5 |
| 39 | + |
| 40 | +print("sign/verify") |
| 41 | + |
| 42 | +message = b"message" |
| 43 | +other_message = b"other message" |
| 44 | + |
| 45 | +hash = rsa.compute_hash(message, "SHA-256") # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=message |
| 46 | +hash = rsa.compute_hash(message=message, method_name="SHA-256") # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=message |
| 47 | +signature_from_hash = rsa.sign_hash(hash, private_key, "SHA-256") # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=RSA CryptographicOperationInput=hash |
| 48 | +signature_from_hash = rsa.sign_hash(hash_value=hash, priv_key=private_key, hash_method="SHA-256") # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=RSA CryptographicOperationInput=hash |
| 49 | + |
| 50 | +signature = rsa.sign(message, private_key, "SHA-256") # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=RSA CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=message |
| 51 | +signature = rsa.sign(message=message, priv_key=private_key, hash_method="SHA-256") # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=RSA CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=message |
| 52 | + |
| 53 | +assert signature == signature_from_hash |
| 54 | + |
| 55 | +print("signature={}".format(signature)) |
| 56 | + |
| 57 | +print() |
| 58 | + |
| 59 | +rsa.verify(message, signature, public_key) # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=RSA CryptographicOperationInput=message CryptographicOperationInput=signature |
| 60 | +rsa.verify(message=message, signature=signature, pub_key=public_key) # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=RSA CryptographicOperationInput=message CryptographicOperationInput=signature |
| 61 | + |
| 62 | +print("Signature verified (as expected)") |
| 63 | + |
| 64 | +try: |
| 65 | + rsa.verify(other_message, signature, public_key) # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=RSA CryptographicOperationInput=other_message CryptographicOperationInput=signature |
| 66 | + raise Exception("Signature verified (unexpected)") |
| 67 | +except rsa.VerificationError: |
| 68 | + print("Signature mismatch (as expected)") |
0 commit comments