Initial commit
[yaffs-website] / node_modules / jodid25519 / lib / utils.js
1 "use strict";
2 /**
3  * @fileOverview
4  * A collection of general utility functions..
5  */
6
7 /*
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.
12  *
13  * Authors: Guy K. Kloss, Michele Bini, Ron Garret
14  *
15  * You should have received a copy of the license along with this program.
16  */
17
18 var core = require('./core');
19
20     /**
21      * @exports jodid25519/utils
22      * A collection of general utility functions..
23      *
24      * @description
25      * A collection of general utility functions..
26      */
27     var ns = {};
28
29     var _HEXCHARS = "0123456789abcdef";
30
31     function _hexencode(vector) {
32         var result = [];
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));
39         }
40         return result.join('');
41     }
42
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);
50             l++;
51         }
52         return result;
53     }
54
55     var _BASE32CHARS = "abcdefghijklmnopqrstuvwxyz234567";
56
57     var _BASE32VALUES = (function () {
58         var result = {};
59         for (var i = 0; i < _BASE32CHARS.length; i++) {
60             result[_BASE32CHARS.charAt(i)] = i;
61         }
62         return result;
63     })();
64
65     function _base32encode(n) {
66         var c;
67         var r = "";
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)
74                                     + r;
75         }
76         return r;
77     }
78
79     function _base32decode(n) {
80         var c = 0;
81         var r = core.ZERO();
82         var l = n.length;
83         for (c = 0; (l > 0) && (c < 255); c += 5) {
84             l--;
85             var v = _BASE32VALUES[n.substr(l, 1)];
86             core.setbit(r, c, v & 1);
87             v >>= 1;
88             core.setbit(r, c + 1, v & 1);
89             v >>= 1;
90             core.setbit(r, c + 2, v & 1);
91             v >>= 1;
92             core.setbit(r, c + 3, v & 1);
93             v >>= 1;
94             core.setbit(r, c + 4, v & 1);
95            }
96         return r;
97     }
98
99     function _map(f, l) {
100         var result = new Array(l.length);
101         for (var i = 0; i < l.length; i++) {
102             result[i] = f(l[i]);
103         }
104         return result;
105     }
106
107     function _chr(n) {
108         return String.fromCharCode(n);
109     }
110
111     function _ord(c) {
112         return c.charCodeAt(0);
113     }
114
115     function _bytes2string(bytes) {
116         return _map(_chr, bytes).join('');
117     }
118
119     function _string2bytes(s) {
120         return _map(_ord, s);
121     }
122
123
124     // Expose some functions to the outside through this name space.
125
126     /**
127      * Encodes an array of unsigned 8-bit integers to a hex string.
128      *
129      * @function
130      * @param vector {array}
131      *     Array containing the byte values.
132      * @returns {string}
133      *     String containing vector in a hexadecimal representation.
134      */
135     ns.hexEncode = _hexencode;
136
137
138     /**
139      * Decodes a hex 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.hexDecode = _hexdecode;
148
149
150     /**
151      * Encodes an array of unsigned 8-bit integers using base32 encoding.
152      *
153      * @function
154      * @param vector {array}
155      *     Array containing the byte values.
156      * @returns {string}
157      *     String containing vector in a hexadecimal representation.
158      */
159     ns.base32encode = _base32encode;
160
161
162     /**
163      * Decodes a base32 encoded string to an array of unsigned 8-bit integers.
164      *
165      * @function
166      * @param vector {string}
167      *     String containing vector in a hexadecimal representation.
168      * @returns {array}
169      *     Array containing the byte values.
170      */
171     ns.base32decode = _base32decode;
172
173
174     /**
175      * Converts an unsigned 8-bit integer array representation to a byte string.
176      *
177      * @function
178      * @param vector {array}
179      *     Array containing the byte values.
180      * @returns {string}
181      *     Byte string representation of vector.
182      */
183     ns.bytes2string = _bytes2string;
184
185
186     /**
187      * Converts a byte string representation to an array of unsigned
188      * 8-bit integers.
189      *
190      * @function
191      * @param vector {array}
192      *     Array containing the byte values.
193      * @returns {string}
194      *     Byte string representation of vector.
195      */
196     ns.string2bytes = _string2bytes;
197
198 module.exports = ns;