util.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. function zero2(word) {
  4. if (word.length === 1) {
  5. return '0' + word;
  6. }
  7. else {
  8. return word;
  9. }
  10. }
  11. function toArray(msg, enc) {
  12. if (Array.isArray(msg)) {
  13. return msg.slice();
  14. }
  15. if (!msg) {
  16. return [];
  17. }
  18. const res = [];
  19. if (typeof msg !== 'string') {
  20. for (let i = 0; i < msg.length; i++) {
  21. res[i] = msg[i] | 0;
  22. }
  23. return res;
  24. }
  25. if (enc === 'hex') {
  26. msg = msg.replace(/[^a-z0-9]+/ig, '');
  27. if (msg.length % 2 !== 0) {
  28. msg = '0' + msg;
  29. }
  30. for (let i = 0; i < msg.length; i += 2) {
  31. res.push(parseInt(msg[i] + msg[i + 1], 16));
  32. }
  33. }
  34. else {
  35. for (let i = 0; i < msg.length; i++) {
  36. const c = msg.charCodeAt(i);
  37. const hi = c >> 8;
  38. const lo = c & 0xff;
  39. if (hi) {
  40. res.push(hi, lo);
  41. }
  42. else {
  43. res.push(lo);
  44. }
  45. }
  46. }
  47. return res;
  48. }
  49. exports.toArray = toArray;
  50. function encodeHex(msg) {
  51. let res = '';
  52. for (let i = 0; i < msg.length; i++) {
  53. res += zero2(msg[i].toString(16));
  54. }
  55. return res;
  56. }
  57. exports.encodeHex = encodeHex;
  58. //# sourceMappingURL=util.js.map