from cryptography.hazmat.primitives.asymmetric import dsa
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
# Generate a DSA private key
private_key = dsa.generate_private_key(
key_size=2048,
backend=default_backend()
)
# Extract public key
public_key = private_key.public_key()
# Message to sign
message = b"Hello, Digital Signature!"
# Sign the message
signature = private_key.sign(
message,
hashes.SHA256()
)
# Verify the signature
try:
public_key.verify(
signature,
message,
hashes.SHA256()
)
print("The signature is valid.")
except Exception as e:
print("The signature is invalid:", e)
ZnJvbSBjcnlwdG9ncmFwaHkuaGF6bWF0LnByaW1pdGl2ZXMuYXN5bW1ldHJpYyBpbXBvcnQgZHNhCmZyb20gY3J5cHRvZ3JhcGh5Lmhhem1hdC5wcmltaXRpdmVzIGltcG9ydCBoYXNoZXMKZnJvbSBjcnlwdG9ncmFwaHkuaGF6bWF0LmJhY2tlbmRzIGltcG9ydCBkZWZhdWx0X2JhY2tlbmQKZnJvbSBjcnlwdG9ncmFwaHkuaGF6bWF0LnByaW1pdGl2ZXMgaW1wb3J0IHNlcmlhbGl6YXRpb24KCiMgR2VuZXJhdGUgYSBEU0EgcHJpdmF0ZSBrZXkKcHJpdmF0ZV9rZXkgPSBkc2EuZ2VuZXJhdGVfcHJpdmF0ZV9rZXkoCiAgICBrZXlfc2l6ZT0yMDQ4LAogICAgYmFja2VuZD1kZWZhdWx0X2JhY2tlbmQoKQopCgojIEV4dHJhY3QgcHVibGljIGtleQpwdWJsaWNfa2V5ID0gcHJpdmF0ZV9rZXkucHVibGljX2tleSgpCgojIE1lc3NhZ2UgdG8gc2lnbgptZXNzYWdlID0gYiJIZWxsbywgRGlnaXRhbCBTaWduYXR1cmUhIgoKIyBTaWduIHRoZSBtZXNzYWdlCnNpZ25hdHVyZSA9IHByaXZhdGVfa2V5LnNpZ24oCiAgICBtZXNzYWdlLAogICAgaGFzaGVzLlNIQTI1NigpCikKCiMgVmVyaWZ5IHRoZSBzaWduYXR1cmUKdHJ5OgogICAgcHVibGljX2tleS52ZXJpZnkoCiAgICAgICAgc2lnbmF0dXJlLAogICAgICAgIG1lc3NhZ2UsCiAgICAgICAgaGFzaGVzLlNIQTI1NigpCiAgICApCiAgICBwcmludCgiVGhlIHNpZ25hdHVyZSBpcyB2YWxpZC4iKQpleGNlcHQgRXhjZXB0aW9uIGFzIGU6CiAgICBwcmludCgiVGhlIHNpZ25hdHVyZSBpcyBpbnZhbGlkOiIsIGUpCg==