title: Python M2Crypto alternate to PHP
body: <p>Folks,</p>
  
  <p>I am doing an API integration for fetching some data from my Vendor database. But they have this <code>Python</code> script for doing an integration. I just wanted to have a head's up from you guys to help me out with how I can do the same with <code>PHP</code></p>
  
  <pre><code>import M2Crypto
  import hashlib
  </code></pre>
  
  <p><code>payload_template = """&lt;payload&gt;&lt;apiVersion&gt;2.0&lt;/apiVersion&gt;&lt;vendorId&gt;jinna&lt;/vendorId&gt;&lt;txnId&gt;UTFTVAIAD&lt;/txnId&gt;&lt;emailId&gt;{encrypted_email_id}&lt;/emailId&gt;&lt;destination&gt;fetchData&lt;/destination&gt;&lt;myAmount&gt;100000&lt;/myAmount&gt;&lt;Duration&gt;24&lt;/Duration&gt;&lt;Type&gt;Home&lt;/Type&gt;&lt;returnUrl&gt;https://mydomainname.com&lt;/returnUrl&gt;&lt;/payload&gt;"""
  email_id = "jinna@mywebsite.com"</code></p>
  
  <h1>Read public key. This particular piece of code is to read the public key that these guys have shared with me.</h1>
  
  <pre><code>public_key = M2Crypto.RSA.load_pub_key('/home/jinna/work/insights-client-keys/clients/mywebsite/public_key')
  </code></pre>
  
  <h1>Encrypt email_id using Public Key and encode in Hex (Base16)</h1>
  
  <pre><code>encrypted_email_id = public_key.public_encrypt(email_id, M2Crypto.RSA.pkcs1_padding).encode("hex")
  </code></pre>
  
  <h1>Create payload.</h1>
  
  <pre><code>payload = payload_template.format(encrypted_email_id = encrypted_email_id)
  </code></pre>
  
  <h1>Generate SHA1 of payload and encode in Hex (Base16)</h1>
  
  <pre><code>digest = hashlib.sha1(payload).hexdigest()
  </code></pre>
  
  <h1>Read private key.</h1>
  
  <pre><code>private_key = M2Crypto.RSA.load_key('/home/jinna/work/insights-client-keys/clients/mywebsite/private_key')
  </code></pre>
  
  <h1>Encrypt digest using private key and encode in Hex (Base16)</h1>
  
  <pre><code>signature = private_key.private_encrypt(digest, M2Crypto.RSA.pkcs1_padding).encode("hex")
  </code></pre>
  
  <h1>Print payload and signature.</h1>
  
  <pre><code>print payload, signature
  </code></pre>
  
  <p><strong>Will appreciate your support.</strong></p>