Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not really a node.js dev, so this is super hacky.. Here's a function that should output a RSA public key PEM from Base64 modulus and exponent. I'm going based on Trevor's comment about node.js <code>verify</code> expecting a PEM.</p> <p>This function composes a ASN.1 DER structure in hex, then hex-decodes it, then base64-encodes it, and then sandwiches it between <code>-----BEGIN PRIVATE KEY-----</code> and <code>-----END PRIVATE KEY-----</code>. After all, that's all a PEM is.</p> <pre><code>function rsaPublicKeyPem(modulus_b64, exponent_b64) { function prepadSigned(hexStr) { msb = hexStr[0] if ( (msb&gt;='8' &amp;&amp; msb&lt;='9') || (msb&gt;='a' &amp;&amp; msb&lt;='f') || (msb&gt;='A'&amp;&amp;msb&lt;='F')) { return '00'+hexStr; } else { return hexStr; } } function toHex(number) { var nstr = number.toString(16) if (nstr.length%2==0) return nstr return '0'+nstr } // encode ASN.1 DER length field // if &lt;=127, short form // if &gt;=128, long form function encodeLengthHex(n) { if (n&lt;=127) return toHex(n) else { n_hex = toHex(n) length_of_length_byte = 128 + n_hex.length/2 // 0x80+numbytes return toHex(length_of_length_byte)+n_hex } } var modulus = new Buffer(modulus_b64,'base64'); var exponent = new Buffer(exponent_b64, 'base64'); var modulus_hex = modulus.toString('hex') var exponent_hex = exponent.toString('hex') modulus_hex = prepadSigned(modulus_hex) exponent_hex = prepadSigned(exponent_hex) var modlen = modulus_hex.length/2 var explen = exponent_hex.length/2 var encoded_modlen = encodeLengthHex(modlen) var encoded_explen = encodeLengthHex(explen) var encoded_pubkey = '30' + encodeLengthHex( modlen + explen + encoded_modlen.length/2 + encoded_explen.length/2 + 2 ) + '02' + encoded_modlen + modulus_hex + '02' + encoded_explen + exponent_hex; var seq2 = '30 0d ' + '06 09 2a 86 48 86 f7 0d 01 01 01' + '05 00 ' + '03' + encodeLengthHex(encoded_pubkey.length/2 + 1) + '00' + encoded_pubkey; seq2 = seq2.replace(/ /g,''); var der_hex = '30' + encodeLengthHex(seq2.length/2) + seq2; der_hex = der_hex.replace(/ /g, ''); var der = new Buffer(der_hex, 'hex'); var der_b64 = der.toString('base64'); var pem = '-----BEGIN PUBLIC KEY-----\n' + der_b64.match(/.{1,64}/g).join('\n') + '\n-----END PUBLIC KEY-----\n'; return pem } </code></pre> <p>Binary manipulation with hex strings? Yuck.. but hey it's a hack.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload