title: Using public key to verify signature in Node.JS crypto
body: <p>Is there a good way to validate signatures in Node.JS (v0.4+) with public keys?</p>
  
  <p>Current crypto module allows this with certificates but not with public keys. For example:</p>
  
  <pre><code>var crypto = require("crypto");
  
  verifier = crypto.createVerifier("sha1");
  verifier.update("signed data");
  verifier.verify(CERT, signature);
  </code></pre>
  
  <p>Variable <code>CERT</code> needs to be signed certificate (I guess the public key is pulled from that) but all I have is the public key and not a certificate.</p>
  
  <p>Only solid way to achieve this seems to be dumping the contents of the data, public key and signature into files and execute <code>openssl dgst</code></p>
  
  <pre><code>fs.writeFileSync("public.key", pubkey);
  fs.writeFileSync("sig.sha1", signature);
  fs.writeFileSync("data.txt", data);
  exec("openssl dgst -sha1 -verify public.key -signature sig.sha1 data.txt", ...)
  </code></pre>
  
  <p>But creating (and deleting) files every time I need to verify a signature seems like a total waste.</p>
  
  <p>Any good ideas how to do it better?</p>
  
  <p><strong>UPDATE 2011-08-03</strong></p>
  
  <p>Crypto module in Node.js v0.5 allows verifying <a href="http://nodejs.org/docs/v0.5.3/api/crypto.html#verifier.verify" rel="nofollow noreferrer">both with certificates and public keys</a>  (RSA or X.509)</p>