OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** @fileoverview Various string utility functions */ | 5 /** @fileoverview Various string utility functions */ |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * Converts a string to an array of bytes. | 9 * Converts a string to an array of bytes. |
10 * @param {string} s The string to convert. | 10 * @param {string} s The string to convert. |
11 * @param {(Array|Uint8Array)=} bytes The Array-like object into which to store | 11 * @param {(Array|Uint8Array)=} bytes The Array-like object into which to store |
12 * the bytes. A new Array will be created if not provided. | 12 * the bytes. A new Array will be created if not provided. |
13 * @return {(Array|Uint8Array)} An array of bytes representing the string. | 13 * @return {(Array|Uint8Array)} An array of bytes representing the string. |
14 */ | 14 */ |
15 function UTIL_StringToBytes(s, bytes) { | 15 function UTIL_StringToBytes(s, bytes) { |
16 bytes = bytes || new Array(s.length); | 16 bytes = bytes || new Array(s.length); |
17 for (var i = 0; i < s.length; ++i) | 17 for (var i = 0; i < s.length; ++i) |
18 bytes[i] = s.charCodeAt(i); | 18 bytes[i] = s.charCodeAt(i); |
19 return bytes; | 19 return bytes; |
20 } | 20 } |
21 | 21 |
22 /** | 22 /** |
23 * Converts a byte array to a string. | 23 * Converts a byte array to a string. |
24 * @param {(Uint8Array|Array.<number>)} b input byte array. | 24 * @param {(Uint8Array|Array<number>)} b input byte array. |
25 * @return {string} result. | 25 * @return {string} result. |
26 */ | 26 */ |
27 function UTIL_BytesToString(b) { | 27 function UTIL_BytesToString(b) { |
28 return String.fromCharCode.apply(null, b); | 28 return String.fromCharCode.apply(null, b); |
29 } | 29 } |
30 | 30 |
31 /** | 31 /** |
32 * Converts a byte array to a hex string. | 32 * Converts a byte array to a hex string. |
33 * @param {(Uint8Array|Array.<number>)} b input byte array. | 33 * @param {(Uint8Array|Array<number>)} b input byte array. |
34 * @return {string} result. | 34 * @return {string} result. |
35 */ | 35 */ |
36 function UTIL_BytesToHex(b) { | 36 function UTIL_BytesToHex(b) { |
37 if (!b) return '(null)'; | 37 if (!b) return '(null)'; |
38 var hexchars = '0123456789ABCDEF'; | 38 var hexchars = '0123456789ABCDEF'; |
39 var hexrep = new Array(b.length * 2); | 39 var hexrep = new Array(b.length * 2); |
40 | 40 |
41 for (var i = 0; i < b.length; ++i) { | 41 for (var i = 0; i < b.length; ++i) { |
42 hexrep[i * 2 + 0] = hexchars.charAt((b[i] >> 4) & 15); | 42 hexrep[i * 2 + 0] = hexchars.charAt((b[i] >> 4) & 15); |
43 hexrep[i * 2 + 1] = hexchars.charAt(b[i] & 15); | 43 hexrep[i * 2 + 1] = hexchars.charAt(b[i] & 15); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 } | 158 } |
159 | 159 |
160 // Type tags used for ASN.1 encoding of ECDSA signatures | 160 // Type tags used for ASN.1 encoding of ECDSA signatures |
161 /** @const */ | 161 /** @const */ |
162 var UTIL_ASN_INT = 0x02; | 162 var UTIL_ASN_INT = 0x02; |
163 /** @const */ | 163 /** @const */ |
164 var UTIL_ASN_SEQUENCE = 0x30; | 164 var UTIL_ASN_SEQUENCE = 0x30; |
165 | 165 |
166 /** | 166 /** |
167 * Parse SEQ(INT, INT) from ASN1 byte array. | 167 * Parse SEQ(INT, INT) from ASN1 byte array. |
168 * @param {(Uint8Array|Array.<number>)} a input to parse from. | 168 * @param {(Uint8Array|Array<number>)} a input to parse from. |
169 * @return {{'r': !Array.<number>, 's': !Array.<number>}|null} | 169 * @return {{'r': !Array<number>, 's': !Array<number>}|null} |
170 */ | 170 */ |
171 function UTIL_Asn1SignatureToJson(a) { | 171 function UTIL_Asn1SignatureToJson(a) { |
172 if (a.length < 6) return null; // Too small to be valid | 172 if (a.length < 6) return null; // Too small to be valid |
173 if (a[0] != UTIL_ASN_SEQUENCE) return null; | 173 if (a[0] != UTIL_ASN_SEQUENCE) return null; |
174 var l = a[1] & 255; | 174 var l = a[1] & 255; |
175 if (l & 0x80) return null; // SEQ.size too large | 175 if (l & 0x80) return null; // SEQ.size too large |
176 if (a.length != 2 + l) return null; // SEQ size does not match input | 176 if (a.length != 2 + l) return null; // SEQ size does not match input |
177 | 177 |
178 function parseInt(off) { | 178 function parseInt(off) { |
179 if (a[off] != UTIL_ASN_INT) return null; | 179 if (a[off] != UTIL_ASN_INT) return null; |
180 var l = a[off + 1] & 255; | 180 var l = a[off + 1] & 255; |
181 if (l & 0x80) return null; // INT.size too large | 181 if (l & 0x80) return null; // INT.size too large |
182 if (off + 2 + l > a.length) return null; // Out of bounds | 182 if (off + 2 + l > a.length) return null; // Out of bounds |
183 return a.slice(off + 2, off + 2 + l); | 183 return a.slice(off + 2, off + 2 + l); |
184 } | 184 } |
185 | 185 |
186 var r = parseInt(2); | 186 var r = parseInt(2); |
187 if (!r) return null; | 187 if (!r) return null; |
188 | 188 |
189 var s = parseInt(2 + 2 + r.length); | 189 var s = parseInt(2 + 2 + r.length); |
190 if (!s) return null; | 190 if (!s) return null; |
191 | 191 |
192 return {'r': r, 's': s}; | 192 return {'r': r, 's': s}; |
193 } | 193 } |
194 | 194 |
195 /** | 195 /** |
196 * Encode a JSON signature {r,s} as an ASN1 SEQ(INT, INT). May modify sig | 196 * Encode a JSON signature {r,s} as an ASN1 SEQ(INT, INT). May modify sig |
197 * @param {{'r': (!Array.<number>|undefined), 's': !Array.<number>}} sig | 197 * @param {{'r': (!Array<number>|undefined), 's': !Array<number>}} sig |
198 * @return {!Uint8Array} | 198 * @return {!Uint8Array} |
199 */ | 199 */ |
200 function UTIL_JsonSignatureToAsn1(sig) { | 200 function UTIL_JsonSignatureToAsn1(sig) { |
201 var rbytes = sig.r; | 201 var rbytes = sig.r; |
202 var sbytes = sig.s; | 202 var sbytes = sig.s; |
203 | 203 |
204 // ASN.1 integers are arbitrary length msb first and signed. | 204 // ASN.1 integers are arbitrary length msb first and signed. |
205 // sig.r and sig.s are 256 bits msb first but _unsigned_, so we must | 205 // sig.r and sig.s are 256 bits msb first but _unsigned_, so we must |
206 // prepend a zero byte in case their high bit is set. | 206 // prepend a zero byte in case their high bit is set. |
207 if (rbytes[0] & 0x80) | 207 if (rbytes[0] & 0x80) |
(...skipping 30 matching lines...) Expand all Loading... |
238 var UTIL_max_events = 500; | 238 var UTIL_max_events = 500; |
239 | 239 |
240 function UTIL_fmt(s) { | 240 function UTIL_fmt(s) { |
241 var line = UTIL_time() + ' ' + s; | 241 var line = UTIL_time() + ' ' + s; |
242 if (UTIL_events.push(line) > UTIL_max_events) { | 242 if (UTIL_events.push(line) > UTIL_max_events) { |
243 // Drop from head. | 243 // Drop from head. |
244 UTIL_events.splice(0, UTIL_events.length - UTIL_max_events); | 244 UTIL_events.splice(0, UTIL_events.length - UTIL_max_events); |
245 } | 245 } |
246 return line; | 246 return line; |
247 } | 247 } |
OLD | NEW |