indutny / asn1.js

ASN.1 Decoder/Encoder/DSL
MIT License
181 stars 64 forks source link

asn1.js does not support PEM output format #44

Closed shea256 closed 9 years ago

shea256 commented 9 years ago

It'd be nice to be able to use the PEM format in addition to DER.

Conversion from PEM to DER should be quite simple, I even wrote a quick DER-to-PEM function:

var base64 = require('base64-js'),
    convertHex = require('convert-hex');

var derToPem = function(derPrivateKey) {
    var byteArrayPrivateKey = convertHex.hexToBytes(derPrivateKey);
    var base64PrivateKey = base64.fromByteArray(byteArrayPrivateKey);

    var pemBeginning = '-----BEGIN PRIVATE KEY-----';
    var pemEnding = '-----END PRIVATE KEY-----';
    var pemMiddle = base64PrivateKey.match(/.{1,64}/g).join('\n');
    var pemPrivateKey = [pemBeginning, pemMiddle, pemEnding].join('\n');

    return pemPrivateKey;
};

Let me know what you think, thanks!

indutny commented 9 years ago

Fixed in 2ac0a3a, sorry for delay!

shea256 commented 9 years ago

Awesome! No worries.