Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/crypt/crypt.js
2868 views
1
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS-IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/**
16
* @fileoverview Namespace with crypto related helper functions.
17
*/
18
19
goog.provide('goog.crypt');
20
21
goog.require('goog.array');
22
goog.require('goog.asserts');
23
24
25
/**
26
* Turns a string into an array of bytes; a "byte" being a JS number in the
27
* range 0-255.
28
* @param {string} str String value to arrify.
29
* @return {!Array<number>} Array of numbers corresponding to the
30
* UCS character codes of each character in str.
31
*/
32
goog.crypt.stringToByteArray = function(str) {
33
var output = [], p = 0;
34
for (var i = 0; i < str.length; i++) {
35
var c = str.charCodeAt(i);
36
while (c > 0xff) {
37
output[p++] = c & 0xff;
38
c >>= 8;
39
}
40
output[p++] = c;
41
}
42
return output;
43
};
44
45
46
/**
47
* Turns an array of numbers into the string given by the concatenation of the
48
* characters to which the numbers correspond.
49
* @param {!Uint8Array|!Array<number>} bytes Array of numbers representing
50
* characters.
51
* @return {string} Stringification of the array.
52
*/
53
goog.crypt.byteArrayToString = function(bytes) {
54
var CHUNK_SIZE = 8192;
55
56
// Special-case the simple case for speed's sake.
57
if (bytes.length <= CHUNK_SIZE) {
58
return String.fromCharCode.apply(null, bytes);
59
}
60
61
// The remaining logic splits conversion by chunks since
62
// Function#apply() has a maximum parameter count.
63
// See discussion: http://goo.gl/LrWmZ9
64
65
var str = '';
66
for (var i = 0; i < bytes.length; i += CHUNK_SIZE) {
67
var chunk = goog.array.slice(bytes, i, i + CHUNK_SIZE);
68
str += String.fromCharCode.apply(null, chunk);
69
}
70
return str;
71
};
72
73
74
/**
75
* Turns an array of numbers into the hex string given by the concatenation of
76
* the hex values to which the numbers correspond.
77
* @param {Uint8Array|Array<number>} array Array of numbers representing
78
* characters.
79
* @return {string} Hex string.
80
*/
81
goog.crypt.byteArrayToHex = function(array) {
82
return goog.array
83
.map(
84
array,
85
function(numByte) {
86
var hexByte = numByte.toString(16);
87
return hexByte.length > 1 ? hexByte : '0' + hexByte;
88
})
89
.join('');
90
};
91
92
93
/**
94
* Converts a hex string into an integer array.
95
* @param {string} hexString Hex string of 16-bit integers (two characters
96
* per integer).
97
* @return {!Array<number>} Array of {0,255} integers for the given string.
98
*/
99
goog.crypt.hexToByteArray = function(hexString) {
100
goog.asserts.assert(
101
hexString.length % 2 == 0, 'Key string length must be multiple of 2');
102
var arr = [];
103
for (var i = 0; i < hexString.length; i += 2) {
104
arr.push(parseInt(hexString.substring(i, i + 2), 16));
105
}
106
return arr;
107
};
108
109
110
/**
111
* Converts a JS string to a UTF-8 "byte" array.
112
* @param {string} str 16-bit unicode string.
113
* @return {!Array<number>} UTF-8 byte array.
114
*/
115
goog.crypt.stringToUtf8ByteArray = function(str) {
116
// TODO(user): Use native implementations if/when available
117
var out = [], p = 0;
118
for (var i = 0; i < str.length; i++) {
119
var c = str.charCodeAt(i);
120
if (c < 128) {
121
out[p++] = c;
122
} else if (c < 2048) {
123
out[p++] = (c >> 6) | 192;
124
out[p++] = (c & 63) | 128;
125
} else if (
126
((c & 0xFC00) == 0xD800) && (i + 1) < str.length &&
127
((str.charCodeAt(i + 1) & 0xFC00) == 0xDC00)) {
128
// Surrogate Pair
129
c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) & 0x03FF);
130
out[p++] = (c >> 18) | 240;
131
out[p++] = ((c >> 12) & 63) | 128;
132
out[p++] = ((c >> 6) & 63) | 128;
133
out[p++] = (c & 63) | 128;
134
} else {
135
out[p++] = (c >> 12) | 224;
136
out[p++] = ((c >> 6) & 63) | 128;
137
out[p++] = (c & 63) | 128;
138
}
139
}
140
return out;
141
};
142
143
144
/**
145
* Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
146
* @param {Uint8Array|Array<number>} bytes UTF-8 byte array.
147
* @return {string} 16-bit Unicode string.
148
*/
149
goog.crypt.utf8ByteArrayToString = function(bytes) {
150
// TODO(user): Use native implementations if/when available
151
var out = [], pos = 0, c = 0;
152
while (pos < bytes.length) {
153
var c1 = bytes[pos++];
154
if (c1 < 128) {
155
out[c++] = String.fromCharCode(c1);
156
} else if (c1 > 191 && c1 < 224) {
157
var c2 = bytes[pos++];
158
out[c++] = String.fromCharCode((c1 & 31) << 6 | c2 & 63);
159
} else if (c1 > 239 && c1 < 365) {
160
// Surrogate Pair
161
var c2 = bytes[pos++];
162
var c3 = bytes[pos++];
163
var c4 = bytes[pos++];
164
var u = ((c1 & 7) << 18 | (c2 & 63) << 12 | (c3 & 63) << 6 | c4 & 63) -
165
0x10000;
166
out[c++] = String.fromCharCode(0xD800 + (u >> 10));
167
out[c++] = String.fromCharCode(0xDC00 + (u & 1023));
168
} else {
169
var c2 = bytes[pos++];
170
var c3 = bytes[pos++];
171
out[c++] =
172
String.fromCharCode((c1 & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
173
}
174
}
175
return out.join('');
176
};
177
178
179
/**
180
* XOR two byte arrays.
181
* @param {!Uint8Array|!Int8Array|!Array<number>} bytes1 Byte array 1.
182
* @param {!Uint8Array|!Int8Array|!Array<number>} bytes2 Byte array 2.
183
* @return {!Array<number>} Resulting XOR of the two byte arrays.
184
*/
185
goog.crypt.xorByteArray = function(bytes1, bytes2) {
186
goog.asserts.assert(
187
bytes1.length == bytes2.length, 'XOR array lengths must match');
188
189
var result = [];
190
for (var i = 0; i < bytes1.length; i++) {
191
result.push(bytes1[i] ^ bytes2[i]);
192
}
193
return result;
194
};
195
196