a zip code crypto-currency system good for red ONLY

x509-pem.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2016 Joyent, Inc.
  2. var x509 = require('./x509');
  3. module.exports = {
  4. read: read,
  5. verify: x509.verify,
  6. sign: x509.sign,
  7. write: write
  8. };
  9. var assert = require('assert-plus');
  10. var asn1 = require('asn1');
  11. var Buffer = require('safer-buffer').Buffer;
  12. var algs = require('../algs');
  13. var utils = require('../utils');
  14. var Key = require('../key');
  15. var PrivateKey = require('../private-key');
  16. var pem = require('./pem');
  17. var Identity = require('../identity');
  18. var Signature = require('../signature');
  19. var Certificate = require('../certificate');
  20. function read(buf, options) {
  21. if (typeof (buf) !== 'string') {
  22. assert.buffer(buf, 'buf');
  23. buf = buf.toString('ascii');
  24. }
  25. var lines = buf.trim().split(/[\r\n]+/g);
  26. var m = lines[0].match(/*JSSTYLED*/
  27. /[-]+[ ]*BEGIN CERTIFICATE[ ]*[-]+/);
  28. assert.ok(m, 'invalid PEM header');
  29. var m2 = lines[lines.length - 1].match(/*JSSTYLED*/
  30. /[-]+[ ]*END CERTIFICATE[ ]*[-]+/);
  31. assert.ok(m2, 'invalid PEM footer');
  32. var headers = {};
  33. while (true) {
  34. lines = lines.slice(1);
  35. m = lines[0].match(/*JSSTYLED*/
  36. /^([A-Za-z0-9-]+): (.+)$/);
  37. if (!m)
  38. break;
  39. headers[m[1].toLowerCase()] = m[2];
  40. }
  41. /* Chop off the first and last lines */
  42. lines = lines.slice(0, -1).join('');
  43. buf = Buffer.from(lines, 'base64');
  44. return (x509.read(buf, options));
  45. }
  46. function write(cert, options) {
  47. var dbuf = x509.write(cert, options);
  48. var header = 'CERTIFICATE';
  49. var tmp = dbuf.toString('base64');
  50. var len = tmp.length + (tmp.length / 64) +
  51. 18 + 16 + header.length*2 + 10;
  52. var buf = Buffer.alloc(len);
  53. var o = 0;
  54. o += buf.write('-----BEGIN ' + header + '-----\n', o);
  55. for (var i = 0; i < tmp.length; ) {
  56. var limit = i + 64;
  57. if (limit > tmp.length)
  58. limit = tmp.length;
  59. o += buf.write(tmp.slice(i, limit), o);
  60. buf[o++] = 10;
  61. i = limit;
  62. }
  63. o += buf.write('-----END ' + header + '-----\n', o);
  64. return (buf.slice(0, o));
  65. }