- 
      
- 
        Save qs-wang/24d1fb32b2c149072081ffc68da9daa6 to your computer and use it in GitHub Desktop. 
    Sign and Verify using Python pycrypto
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/usr/bin/env bash | |
| # Generate RSA private key | |
| openssl genrsa -out private_key.pem 1024 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | pycrypto==2.6.1 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/usr/bin/env python | |
| from base64 import ( | |
| b64encode, | |
| b64decode, | |
| ) | |
| from Crypto.Hash import SHA256 | |
| from Crypto.Signature import PKCS1_v1_5 | |
| from Crypto.PublicKey import RSA | |
| message = "I want this stream signed" | |
| digest = SHA256.new() | |
| digest.update(message) | |
| # Read shared key from file | |
| private_key = False | |
| with open ("private_key.pem", "r") as myfile: | |
| private_key = RSA.importKey(myfile.read()) | |
| # Load private key and sign message | |
| signer = PKCS1_v1_5.new(private_key) | |
| sig = signer.sign(digest) | |
| # Load public key and verify message | |
| verifier = PKCS1_v1_5.new(private_key.publickey()) | |
| verified = verifier.verify(digest, sig) | |
| assert verified, 'Signature verification failed' | |
| print 'Successfully verified message' | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment