MCPcopy
hub / github.com/vercel/hyper / write

Function write

bin/yarn-standalone.js:32490–32546  ·  view source on GitHub ↗
(key, options, type)

Source from the content-addressed store, hash-verified

32488}
32489
32490function write(key, options, type) {
32491 assert.object(key);
32492
32493 var alg = {
32494 'ecdsa': 'EC',
32495 'rsa': 'RSA',
32496 'dsa': 'DSA',
32497 'ed25519': 'EdDSA'
32498 }[key.type];
32499 var header;
32500
32501 var der = new asn1.BerWriter();
32502
32503 if (PrivateKey.isPrivateKey(key)) {
32504 if (type && type === 'pkcs8') {
32505 header = 'PRIVATE KEY';
32506 pkcs8.writePkcs8(der, key);
32507 } else {
32508 if (type)
32509 assert.strictEqual(type, 'pkcs1');
32510 header = alg + ' PRIVATE KEY';
32511 pkcs1.writePkcs1(der, key);
32512 }
32513
32514 } else if (Key.isKey(key)) {
32515 if (type && type === 'pkcs1') {
32516 header = alg + ' PUBLIC KEY';
32517 pkcs1.writePkcs1(der, key);
32518 } else {
32519 if (type)
32520 assert.strictEqual(type, 'pkcs8');
32521 header = 'PUBLIC KEY';
32522 pkcs8.writePkcs8(der, key);
32523 }
32524
32525 } else {
32526 throw (new Error('key is not a Key or PrivateKey'));
32527 }
32528
32529 var tmp = der.buffer.toString('base64');
32530 var len = tmp.length + (tmp.length / 64) +
32531 18 + 16 + header.length*2 + 10;
32532 var buf = Buffer.alloc(len);
32533 var o = 0;
32534 o += buf.write('-----BEGIN ' + header + '-----\n', o);
32535 for (var i = 0; i < tmp.length; ) {
32536 var limit = i + 64;
32537 if (limit > tmp.length)
32538 limit = tmp.length;
32539 o += buf.write(tmp.slice(i, limit), o);
32540 buf[o++] = 10;
32541 i = limit;
32542 }
32543 o += buf.write('-----END ' + header + '-----\n', o);
32544
32545 return (buf.slice(0, o));
32546}
32547

Callers 1

yarn-standalone.jsFile · 0.85

Calls 15

keyTypeToAlgFunction · 0.85
__webpack_require__Function · 0.85
writeRSAFunction · 0.85
writeECDSAFunction · 0.85
writeTBSCertFunction · 0.85
toBufferFunction · 0.85
getCertTypeFunction · 0.85
concatMethod · 0.80
onceMethod · 0.80
readMethod · 0.80
matchMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected