oid.js 857 B

1234567891011121314151617181920212223242526
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.encodeOIDString = encodeOIDString;
  4. const ANS1_TAG_OID = 0x06;
  5. function encodeOIDString(oid) {
  6. const parts = oid.split('.');
  7. // The first two subidentifiers are encoded into the first byte
  8. const first = parseInt(parts[0], 10) * 40 + parseInt(parts[1], 10);
  9. const rest = [];
  10. parts.slice(2).forEach((part) => {
  11. const bytes = encodeVariableLengthInteger(parseInt(part, 10));
  12. rest.push(...bytes);
  13. });
  14. const der = Buffer.from([first, ...rest]);
  15. return Buffer.from([ANS1_TAG_OID, der.length, ...der]);
  16. }
  17. function encodeVariableLengthInteger(value) {
  18. const bytes = [];
  19. let mask = 0x00;
  20. while (value > 0) {
  21. bytes.unshift((value & 0x7f) | mask);
  22. value >>= 7;
  23. mask = 0x80;
  24. }
  25. return bytes;
  26. }