title: How to sign a document in python with M2Crypto using particular padding technique?
body: <p>I need to digitally sign some text in python using a private key stored in a .pem file.  It seems like M2Crypto is the preferred way to do that these days, so that's what I'm using.  I think I get most of it, but I'm confused about how to configure padding. To be specific, I need to verify the signature in an iPhone app, using a padding scheme called <code>kSecPaddingPKCS1SHA1</code> and described like this:</p>
  
  <blockquote>
    <p>Data to be signed is a SHA1 hash.
    Standard ASN.1 padding will be done, as well as PKCS1 padding of the underlying RSA operation.</p>
  </blockquote>
  
  <p>Not being a crypto expert, I have only a fuzzy idea what this means.  I've tried to look at some of the RFCs but found them impenetrable.  I see that the encryption/decryption methods of RSA objects take padding types, but I don't see anything similar related to signature verification.</p>
  
  <p>Any help, especially with code, will be appreciated.</p>
  
  <p>(In some sense this is the converse of <a href="https://stackoverflow.com/questions/544433/how-do-you-verify-an-rsa-sha1-signature-in-python">this question</a>.)</p>
  
  <hr>
  
  <p>Ok, the answer given below is correct AFAICT.  The following code generates a signature for <code>text</code> that validates on the iPhone using the <code>kSecPaddingPKCS1SHA1</code> padding scheme.</p>
  
  <pre><code>from M2Crypto import EVP
  privkey = EVP.load_key("privkey.pem")
  privkey.sign_init()
  privkey.sign_update(text)
  signature = privkey.sign_final()
  </code></pre>
  
  <p>(Sorry to editorialize, but can I just say that crypto hackers are some of the lousiest documentation writers in the universe?)</p>