title: OpenSSL equivalent for Java/BouncyCastle signature
body: <p>I have a piece of software doing a digital signature as follows.</p>
  
  <pre><code>    MessageDigest md = MessageDigest.getInstance("SHA1", "BC");
      Cipher cipher = Cipher.getInstance("RSA/None/NoPadding", "BC");
      cipher.init(Cipher.ENCRYPT_MODE, privateKey);
      byte[] hash = md.digest("message".getBytes());
      byte[] signature = cipher.doFinal(hash);
  </code></pre>
  
  <p>I want to replace it by some lines of batch/openssl, but failed reproducing this signature. Basically it's a combination of a SHA1 and RSA, which isn't that bad. I was wondering why hash and encryption are separated. Both ways doing this with openssl resulting in a different signature (ignoring the format):</p>
  
  <pre><code>openssl dgst -sha1 -binary msg.txt &gt; hash
  openssl rsautl -sign -inkey priv.pem -in hash -hexdump
  </code></pre>
  
  <p>and</p>
  
  <pre><code>openssl dgst -sha1 -sign priv.pem -hex &lt; msg.txt
  </code></pre>
  
  <p>So there's something I am missing...</p>
  
  <p>UPDATE:</p>
  
  <p>Thanks for your comments! Of course this Java code is NOT the way to do signatures properly, but the code that verifies is not in my hand, and my goal is to create a signature that could be verified. I will make suggestions to change the whole thing, though.</p>
  
  <p>But I've finally managed to reproduce the signature. I do not answer the question myself, because I want an proper openssl solution. So here is what I got:</p>
  
  <p>The Java code above <strong>does padding</strong>! The hash code is prepadded with zero bytes to a length of 256!</p>