Note - Double Click to Copy Code Contact Us!

Signature Verification

Tech Doubility
Signature Verification




Python
  1. from cryptography.hazmat.primitives import hashes
  2. from cryptography.hazmat.primitives.asymmetric import padding
  3. from cryptography.hazmat.primitives.serialization import load_pem_public_key
  4.  
  5. def verify_signature(public_key_path, message, signature):
  6. with open(public_key_path, 'rb') as key_file:
  7. public_key = load_pem_public_key(key_file.read())
  8.  
  9. try:
  10. public_key.verify(
  11. signature,
  12. message,
  13. padding.PSS(
  14. mgf=padding.MGF1(hashes.SHA256()),
  15. salt_length=padding.PSS.MAX_LENGTH
  16. ),
  17. hashes.SHA256()
  18. )
  19. print("Signature is valid.")
  20. return True
  21. except Exception:
  22. print("Signature is invalid.")
  23. return False
  24.  
  25. # Usage example
  26. public_key_path = 'public_key.pem'
  27. message = b'This is the message that was signed.'
  28. signature = b'\x00\x11\x22...' # Replace with the actual signature
  29.  
  30. verify_signature(public_key_path, message, signature)

Here's how you can use the code:

  1. Replace 'public_key.pem' with the path to your public key file. Make sure the key is in PEM format.
  2. Replace message with the message that was signed.
  3. Replace signature with the actual signature you want to verify.

When you run the code, it will load the public key from the specified file, verify the signature against the message, and print whether the signature is valid or invalid.

Make sure you have the cryptography library installed before running the code. You can install it using pip:

pip install cryptography

Note that this code assumes you have a public key and a signature in hand. If you need to generate keys and sign messages as well, let me know, and I can provide additional code for that.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.