title: PHP's openssl_sign Equivalent in Node JS
body: <p>I am (slowly) learning Node JS and trying to use it in place of a PHP script I have. I need to sign a string I have assembled with SSL to pass on to a <code>curl</code> request. </p>
  
  <p>In PHP, this is how I've done it:</p>
  
  <pre><code>$sig = '2018-08-24T17:33:41Z:abcdef:/path/to/api';
  
  $pkeyid = openssl_pkey_get_private("file://YourMomGoesToCollege.pem"); 
  
  // Sign 'sig' with private key
  if(openssl_sign($sig, $signed_signature, $pkeyid, "sha256WithRSAEncryption")) {
  
    openssl_free_key($pkeyid);
  
    //Set curl header options ...
    curl_setopt($ch, CURLOPT_HTTPHEADER,
      [
      "X-Apple-CloudKit-Request-SignatureV1: " . base64_encode($signed_signature),
    ]
  );
  
  }
  </code></pre>
  
  <p>So I'm trying to generate the evuivalent of <code>$signed_signature</code>, but I'm not sure how to proceed. It seems like Node's <a href="https://nodejs.org/api/crypto.html#crypto_class_sign" rel="nofollow noreferrer">Crypto</a> can do something similar, but its parameters seem different. This is my best guess:</p>
  
  <pre><code>const crypto = require('crypto')
  const sign = crypto.createSign('SHA256')
  
  sign.write(sig)
  sign.end()
  
  const privateKey = __dirname + 'YourMomGoesToCollege.pem'
  var signedSignature = sign.sign(privateKey, 'hex')
  
  var readyForCurl = Buffer.from(signedSignature).toString('base64')
  </code></pre>
  
  <p>Am I on the right track?</p>