utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. /**
  3. * @fileOverview
  4. * A collection of general utility functions..
  5. */
  6. /*
  7. * Copyright (c) 2011, 2012, 2014 Ron Garret
  8. * Copyright (c) 2007, 2013, 2014 Michele Bini
  9. * Copyright (c) 2014 Mega Limited
  10. * under the MIT License.
  11. *
  12. * Authors: Guy K. Kloss, Michele Bini, Ron Garret
  13. *
  14. * You should have received a copy of the license along with this program.
  15. */
  16. var core = require('./core');
  17. /**
  18. * @exports jodid25519/utils
  19. * A collection of general utility functions..
  20. *
  21. * @description
  22. * A collection of general utility functions..
  23. */
  24. var ns = {};
  25. var _HEXCHARS = "0123456789abcdef";
  26. function _hexencode(vector) {
  27. var result = [];
  28. for (var i = vector.length - 1; i >= 0; i--) {
  29. var value = vector[i];
  30. result.push(_HEXCHARS.substr((value >>> 12) & 0x0f, 1));
  31. result.push(_HEXCHARS.substr((value >>> 8) & 0x0f, 1));
  32. result.push(_HEXCHARS.substr((value >>> 4) & 0x0f, 1));
  33. result.push(_HEXCHARS.substr(value & 0x0f, 1));
  34. }
  35. return result.join('');
  36. }
  37. function _hexdecode(vector) {
  38. var result = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  39. for (var i = vector.length - 1, l = 0; i >= 0; i -= 4) {
  40. result[l] = (_HEXCHARS.indexOf(vector.charAt(i)))
  41. | (_HEXCHARS.indexOf(vector.charAt(i - 1)) << 4)
  42. | (_HEXCHARS.indexOf(vector.charAt(i - 2)) << 8)
  43. | (_HEXCHARS.indexOf(vector.charAt(i - 3)) << 12);
  44. l++;
  45. }
  46. return result;
  47. }
  48. var _BASE32CHARS = "abcdefghijklmnopqrstuvwxyz234567";
  49. var _BASE32VALUES = (function () {
  50. var result = {};
  51. for (var i = 0; i < _BASE32CHARS.length; i++) {
  52. result[_BASE32CHARS.charAt(i)] = i;
  53. }
  54. return result;
  55. })();
  56. function _base32encode(n) {
  57. var c;
  58. var r = "";
  59. for (c = 0; c < 255; c += 5) {
  60. r = _BASE32CHARS.substr(core.getbit(n, c)
  61. + (core.getbit(n, c + 1) << 1)
  62. + (core.getbit(n, c + 2) << 2)
  63. + (core.getbit(n, c + 3) << 3)
  64. + (core.getbit(n, c + 4) << 4), 1)
  65. + r;
  66. }
  67. return r;
  68. }
  69. function _base32decode(n) {
  70. var c = 0;
  71. var r = core.ZERO();
  72. var l = n.length;
  73. for (c = 0; (l > 0) && (c < 255); c += 5) {
  74. l--;
  75. var v = _BASE32VALUES[n.substr(l, 1)];
  76. core.setbit(r, c, v & 1);
  77. v >>= 1;
  78. core.setbit(r, c + 1, v & 1);
  79. v >>= 1;
  80. core.setbit(r, c + 2, v & 1);
  81. v >>= 1;
  82. core.setbit(r, c + 3, v & 1);
  83. v >>= 1;
  84. core.setbit(r, c + 4, v & 1);
  85. }
  86. return r;
  87. }
  88. function _map(f, l) {
  89. var result = new Array(l.length);
  90. for (var i = 0; i < l.length; i++) {
  91. result[i] = f(l[i]);
  92. }
  93. return result;
  94. }
  95. function _chr(n) {
  96. return String.fromCharCode(n);
  97. }
  98. function _ord(c) {
  99. return c.charCodeAt(0);
  100. }
  101. function _bytes2string(bytes) {
  102. return _map(_chr, bytes).join('');
  103. }
  104. function _string2bytes(s) {
  105. return _map(_ord, s);
  106. }
  107. // Expose some functions to the outside through this name space.
  108. /**
  109. * Encodes an array of unsigned 8-bit integers to a hex string.
  110. *
  111. * @function
  112. * @param vector {array}
  113. * Array containing the byte values.
  114. * @returns {string}
  115. * String containing vector in a hexadecimal representation.
  116. */
  117. ns.hexEncode = _hexencode;
  118. /**
  119. * Decodes a hex string to an array of unsigned 8-bit integers.
  120. *
  121. * @function
  122. * @param vector {string}
  123. * String containing vector in a hexadecimal representation.
  124. * @returns {array}
  125. * Array containing the byte values.
  126. */
  127. ns.hexDecode = _hexdecode;
  128. /**
  129. * Encodes an array of unsigned 8-bit integers using base32 encoding.
  130. *
  131. * @function
  132. * @param vector {array}
  133. * Array containing the byte values.
  134. * @returns {string}
  135. * String containing vector in a hexadecimal representation.
  136. */
  137. ns.base32encode = _base32encode;
  138. /**
  139. * Decodes a base32 encoded string to an array of unsigned 8-bit integers.
  140. *
  141. * @function
  142. * @param vector {string}
  143. * String containing vector in a hexadecimal representation.
  144. * @returns {array}
  145. * Array containing the byte values.
  146. */
  147. ns.base32decode = _base32decode;
  148. /**
  149. * Converts an unsigned 8-bit integer array representation to a byte string.
  150. *
  151. * @function
  152. * @param vector {array}
  153. * Array containing the byte values.
  154. * @returns {string}
  155. * Byte string representation of vector.
  156. */
  157. ns.bytes2string = _bytes2string;
  158. /**
  159. * Converts a byte string representation to an array of unsigned
  160. * 8-bit integers.
  161. *
  162. * @function
  163. * @param vector {array}
  164. * Array containing the byte values.
  165. * @returns {string}
  166. * Byte string representation of vector.
  167. */
  168. ns.string2bytes = _string2bytes;
  169. module.exports = ns;