| 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. Multi-byte characters are written as little-endian. |
| 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 |
// NOTE: c <= 0xffff since JavaScript strings are UTF-16. |
| 37 |
if (c > 0xff) { |
| 38 |
output[p++] = c & 0xff; |
| 39 |
c >>= 8; |
| 40 |
} |
| 41 |
output[p++] = c; |
| 42 |
} |
| 43 |
return output; |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Turns an array of numbers into the string given by the concatenation of the |
| 49 |
* characters to which the numbers correspond. |
| 50 |
* @param {!Uint8Array|!Array<number>} bytes Array of numbers representing |
| 51 |
* characters. |
| 52 |
* @return {string} Stringification of the array. |
| 53 |
*/ |
| 54 |
goog.crypt.byteArrayToString = function(bytes) { |
| 55 |
var CHUNK_SIZE = 8192; |
| 56 |
|
| 57 |
// Special-case the simple case for speed's sake. |
| 58 |
if (bytes.length <= CHUNK_SIZE) { |
| 59 |
return String.fromCharCode.apply(null, bytes); |
| 60 |
} |
| 61 |
|
| 62 |
// The remaining logic splits conversion by chunks since |
| 63 |
// Function#apply() has a maximum parameter count. |
| 64 |
// See discussion: http://goo.gl/LrWmZ9 |
| 65 |
|
| 66 |
var str = ''; |
| 67 |
for (var i = 0; i < bytes.length; i += CHUNK_SIZE) { |
| 68 |
var chunk = goog.array.slice(bytes, i, i + CHUNK_SIZE); |
| 69 |
str += String.fromCharCode.apply(null, chunk); |
| 70 |
} |
| 71 |
return str; |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Turns an array of numbers into the hex string given by the concatenation of |
| 77 |
* the hex values to which the numbers correspond. |
| 78 |
* @param {Uint8Array|Array<number>} array Array of numbers representing |
| 79 |
* characters. |
| 80 |
* @return {string} Hex string. |
| 81 |
*/ |
| 82 |
goog.crypt.byteArrayToHex = function(array) { |
| 83 |
return goog.array |
| 84 |
.map( |
| 85 |
array, |
| 86 |
function(numByte) { |
| 87 |
var hexByte = numByte.toString(16); |
| 88 |
return hexByte.length > 1 ? hexByte : '0' + hexByte; |
| 89 |
}) |
| 90 |
.join(''); |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Converts a hex string into an integer array. |
| 96 |
* @param {string} hexString Hex string of 16-bit integers (two characters |
| 97 |
* per integer). |
| 98 |
* @return {!Array<number>} Array of {0,255} integers for the given string. |
| 99 |
*/ |
| 100 |
goog.crypt.hexToByteArray = function(hexString) { |
| 101 |
goog.asserts.assert( |
| 102 |
hexString.length % 2 == 0, 'Key string length must be multiple of 2'); |
| 103 |
var arr = []; |
| 104 |
for (var i = 0; i < hexString.length; i += 2) { |
| 105 |
arr.push(parseInt(hexString.substring(i, i + 2), 16)); |
| 106 |
} |
| 107 |
return arr; |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Converts a JS string to a UTF-8 "byte" array. |
| 113 |
* @param {string} str 16-bit unicode string. |
| 114 |
* @return {!Array<number>} UTF-8 byte array. |
| 115 |
*/ |
| 116 |
goog.crypt.stringToUtf8ByteArray = function(str) { |
| 117 |
// TODO(user): Use native implementations if/when available |
| 118 |
var out = [], p = 0; |
| 119 |
for (var i = 0; i < str.length; i++) { |
| 120 |
var c = str.charCodeAt(i); |
| 121 |
if (c < 128) { |
| 122 |
out[p++] = c; |
| 123 |
} else if (c < 2048) { |
| 124 |
out[p++] = (c >> 6) | 192; |
| 125 |
out[p++] = (c & 63) | 128; |
| 126 |
} else if ( |
| 127 |
((c & 0xFC00) == 0xD800) && (i + 1) < str.length && |
| 128 |
((str.charCodeAt(i + 1) & 0xFC00) == 0xDC00)) { |
| 129 |
// Surrogate Pair |
| 130 |
c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) & 0x03FF); |
| 131 |
out[p++] = (c >> 18) | 240; |
| 132 |
out[p++] = ((c >> 12) & 63) | 128; |
| 133 |
out[p++] = ((c >> 6) & 63) | 128; |
| 134 |
out[p++] = (c & 63) | 128; |
| 135 |
} else { |
| 136 |
out[p++] = (c >> 12) | 224; |
| 137 |
out[p++] = ((c >> 6) & 63) | 128; |
| 138 |
out[p++] = (c & 63) | 128; |
| 139 |
} |
| 140 |
} |
| 141 |
return out; |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
/** |
| 146 |
* Converts a UTF-8 byte array to JavaScript's 16-bit Unicode. |
| 147 |
* @param {Uint8Array|Array<number>} bytes UTF-8 byte array. |
| 148 |
* @return {string} 16-bit Unicode string. |
| 149 |
*/ |
| 150 |
goog.crypt.utf8ByteArrayToString = function(bytes) { |
| 151 |
// TODO(user): Use native implementations if/when available |
| 152 |
var out = [], pos = 0, c = 0; |
| 153 |
while (pos < bytes.length) { |
| 154 |
var c1 = bytes[pos++]; |
| 155 |
if (c1 < 128) { |
| 156 |
out[c++] = String.fromCharCode(c1); |
| 157 |
} else if (c1 > 191 && c1 < 224) { |
| 158 |
var c2 = bytes[pos++]; |
| 159 |
out[c++] = String.fromCharCode((c1 & 31) << 6 | c2 & 63); |
| 160 |
} else if (c1 > 239 && c1 < 365) { |
| 161 |
// Surrogate Pair |
| 162 |
var c2 = bytes[pos++]; |
| 163 |
var c3 = bytes[pos++]; |
| 164 |
var c4 = bytes[pos++]; |
| 165 |
var u = ((c1 & 7) << 18 | (c2 & 63) << 12 | (c3 & 63) << 6 | c4 & 63) - |
| 166 |
0x10000; |
| 167 |
out[c++] = String.fromCharCode(0xD800 + (u >> 10)); |
| 168 |
out[c++] = String.fromCharCode(0xDC00 + (u & 1023)); |
| 169 |
} else { |
| 170 |
var c2 = bytes[pos++]; |
| 171 |
var c3 = bytes[pos++]; |
| 172 |
out[c++] = |
| 173 |
String.fromCharCode((c1 & 15) << 12 | (c2 & 63) << 6 | c3 & 63); |
| 174 |
} |
| 175 |
} |
| 176 |
return out.join(''); |
| 177 |
}; |
| 178 |
|
| 179 |
|
| 180 |
/** |
| 181 |
* XOR two byte arrays. |
| 182 |
* @param {!Uint8Array|!Int8Array|!Array<number>} bytes1 Byte array 1. |
| 183 |
* @param {!Uint8Array|!Int8Array|!Array<number>} bytes2 Byte array 2. |
| 184 |
* @return {!Array<number>} Resulting XOR of the two byte arrays. |
| 185 |
*/ |
| 186 |
goog.crypt.xorByteArray = function(bytes1, bytes2) { |
| 187 |
goog.asserts.assert( |
| 188 |
bytes1.length == bytes2.length, 'XOR array lengths must match'); |
| 189 |
|
| 190 |
var result = []; |
| 191 |
for (var i = 0; i < bytes1.length; i++) { |
| 192 |
result.push(bytes1[i] ^ bytes2[i]); |
| 193 |
} |
| 194 |
return result; |
| 195 |
}; |