4 * A collection of general utility functions..
8 * Copyright (c) 2011, 2012, 2014 Ron Garret
9 * Copyright (c) 2007, 2013, 2014 Michele Bini
10 * Copyright (c) 2014 Mega Limited
11 * under the MIT License.
13 * Authors: Guy K. Kloss, Michele Bini, Ron Garret
15 * You should have received a copy of the license along with this program.
18 var core = require('./core');
21 * @exports jodid25519/utils
22 * A collection of general utility functions..
25 * A collection of general utility functions..
29 var _HEXCHARS = "0123456789abcdef";
31 function _hexencode(vector) {
33 for (var i = vector.length - 1; i >= 0; i--) {
34 var value = vector[i];
35 result.push(_HEXCHARS.substr((value >>> 12) & 0x0f, 1));
36 result.push(_HEXCHARS.substr((value >>> 8) & 0x0f, 1));
37 result.push(_HEXCHARS.substr((value >>> 4) & 0x0f, 1));
38 result.push(_HEXCHARS.substr(value & 0x0f, 1));
40 return result.join('');
43 function _hexdecode(vector) {
44 var result = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
45 for (var i = vector.length - 1, l = 0; i >= 0; i -= 4) {
46 result[l] = (_HEXCHARS.indexOf(vector.charAt(i)))
47 | (_HEXCHARS.indexOf(vector.charAt(i - 1)) << 4)
48 | (_HEXCHARS.indexOf(vector.charAt(i - 2)) << 8)
49 | (_HEXCHARS.indexOf(vector.charAt(i - 3)) << 12);
55 var _BASE32CHARS = "abcdefghijklmnopqrstuvwxyz234567";
57 var _BASE32VALUES = (function () {
59 for (var i = 0; i < _BASE32CHARS.length; i++) {
60 result[_BASE32CHARS.charAt(i)] = i;
65 function _base32encode(n) {
68 for (c = 0; c < 255; c += 5) {
69 r = _BASE32CHARS.substr(core.getbit(n, c)
70 + (core.getbit(n, c + 1) << 1)
71 + (core.getbit(n, c + 2) << 2)
72 + (core.getbit(n, c + 3) << 3)
73 + (core.getbit(n, c + 4) << 4), 1)
79 function _base32decode(n) {
83 for (c = 0; (l > 0) && (c < 255); c += 5) {
85 var v = _BASE32VALUES[n.substr(l, 1)];
86 core.setbit(r, c, v & 1);
88 core.setbit(r, c + 1, v & 1);
90 core.setbit(r, c + 2, v & 1);
92 core.setbit(r, c + 3, v & 1);
94 core.setbit(r, c + 4, v & 1);
100 var result = new Array(l.length);
101 for (var i = 0; i < l.length; i++) {
108 return String.fromCharCode(n);
112 return c.charCodeAt(0);
115 function _bytes2string(bytes) {
116 return _map(_chr, bytes).join('');
119 function _string2bytes(s) {
120 return _map(_ord, s);
124 // Expose some functions to the outside through this name space.
127 * Encodes an array of unsigned 8-bit integers to a hex string.
130 * @param vector {array}
131 * Array containing the byte values.
133 * String containing vector in a hexadecimal representation.
135 ns.hexEncode = _hexencode;
139 * Decodes a hex string to an array of unsigned 8-bit integers.
142 * @param vector {string}
143 * String containing vector in a hexadecimal representation.
145 * Array containing the byte values.
147 ns.hexDecode = _hexdecode;
151 * Encodes an array of unsigned 8-bit integers using base32 encoding.
154 * @param vector {array}
155 * Array containing the byte values.
157 * String containing vector in a hexadecimal representation.
159 ns.base32encode = _base32encode;
163 * Decodes a base32 encoded string to an array of unsigned 8-bit integers.
166 * @param vector {string}
167 * String containing vector in a hexadecimal representation.
169 * Array containing the byte values.
171 ns.base32decode = _base32decode;
175 * Converts an unsigned 8-bit integer array representation to a byte string.
178 * @param vector {array}
179 * Array containing the byte values.
181 * Byte string representation of vector.
183 ns.bytes2string = _bytes2string;
187 * Converts a byte string representation to an array of unsigned
191 * @param vector {array}
192 * Array containing the byte values.
194 * Byte string representation of vector.
196 ns.string2bytes = _string2bytes;