title: Convert PEM file to DER
body: <p>I am currently trying to write a script that allows me to compute the Tor HS address from the hiddens service's private key file.
  In order to do this the file needs to be brought into the DER format.</p>
  
  <p>Using OpenSSL this can be done with:</p>
  
  <pre><code>openssl rsa -in private_key -pubout -outform DER
  </code></pre>
  
  <p>Piping this into python with:</p>
  
  <pre><code>base64.b32encode(hashlib.sha1(sys.stdin.read()[22:]).digest()[:10]).lower()'
  </code></pre>
  
  <p>will return the address correctly.</p>
  
  <p>However I would like to perform the same using only python. My problem  is that using the pycrypto module the DER output is different and the address therefore incorrect.</p>
  
  <pre><code>key = RSA.importKey(keyfile.read()).publickey()
  print(key.exportKey(format='DER'))
  </code></pre>
  
  <p>Will result in a different output than the openssl call.
  Is this just a matter of implementation that allows different results? Or am I making a mistake somewhere?</p>
  
  <p>Any help would be appreciated</p>