OLD | NEW |
1 function importTestKeys() | 1 function importTestKeys() |
2 { | 2 { |
3 var keyFormat = "raw"; | 3 var keyFormat = "raw"; |
4 var data = asciiToArrayBuffer("16 bytes of key!"); | 4 var data = asciiToUint8Array("16 bytes of key!"); |
5 var extractable = true; | 5 var extractable = true; |
6 var keyUsages = ['encrypt', 'decrypt', 'sign', 'verify']; | 6 var keyUsages = ['encrypt', 'decrypt', 'sign', 'verify']; |
7 | 7 |
8 var hmacPromise = crypto.subtle.importKey(keyFormat, data, {name: 'hmac', ha
sh: {name: 'sha-1'}}, extractable, keyUsages); | 8 var hmacPromise = crypto.subtle.importKey(keyFormat, data, {name: 'hmac', ha
sh: {name: 'sha-1'}}, extractable, keyUsages); |
9 var aesCbcPromise = crypto.subtle.importKey(keyFormat, data, {name: 'AES-CBC
'}, extractable, keyUsages); | 9 var aesCbcPromise = crypto.subtle.importKey(keyFormat, data, {name: 'AES-CBC
'}, extractable, keyUsages); |
10 var aesCbcJustDecrypt = crypto.subtle.importKey(keyFormat, data, {name: 'AES
-CBC'}, false, ['decrypt']); | 10 var aesCbcJustDecrypt = crypto.subtle.importKey(keyFormat, data, {name: 'AES
-CBC'}, false, ['decrypt']); |
11 | 11 |
12 return Promise.all([hmacPromise, aesCbcPromise, aesCbcJustDecrypt]).then(fun
ction(results) { | 12 return Promise.all([hmacPromise, aesCbcPromise, aesCbcJustDecrypt]).then(fun
ction(results) { |
13 return { | 13 return { |
14 hmacSha1: results[0], | 14 hmacSha1: results[0], |
15 aesCbc: results[1], | 15 aesCbc: results[1], |
16 aesCbcJustDecrypt: results[2], | 16 aesCbcJustDecrypt: results[2], |
17 }; | 17 }; |
18 }); | 18 }); |
19 } | 19 } |
20 | 20 |
21 // Builds a hex string representation of any array-like input (array or | 21 // Verifies that the given "bytes" holds the same value as "expectedHexString". |
22 // ArrayBufferView). The output looks like this: | 22 // "bytes" can be anything recognized by "bytesToHexString()". |
23 // [ab 03 4c 99] | 23 function bytesShouldMatchHexString(testDescription, expectedHexString, bytes) |
24 function byteArrayToHexString(bytes) | |
25 { | 24 { |
| 25 expectedHexString = "[" + expectedHexString.toLowerCase() + "]"; |
| 26 var actualHexString = "[" + bytesToHexString(bytes) + "]"; |
| 27 |
| 28 if (actualHexString === expectedHexString) { |
| 29 debug("PASS: " + testDescription + " should be " + expectedHexString + "
and was"); |
| 30 } else { |
| 31 debug("FAIL: " + testDescription + " should be " + expectedHexString + "
but was " + actualHexString); |
| 32 } |
| 33 } |
| 34 |
| 35 // Builds a hex string representation for an array-like input. |
| 36 // "bytes" can be an Array of bytes, an ArrayBuffer, or any TypedArray. |
| 37 // The output looks like this: |
| 38 // ab034c99 |
| 39 function bytesToHexString(bytes) |
| 40 { |
| 41 if (!bytes) |
| 42 return null; |
| 43 |
| 44 bytes = new Uint8Array(bytes); |
26 var hexBytes = []; | 45 var hexBytes = []; |
27 | 46 |
28 for (var i = 0; i < bytes.length; ++i) { | 47 for (var i = 0; i < bytes.length; ++i) { |
29 var byteString = bytes[i].toString(16); | 48 var byteString = bytes[i].toString(16); |
30 if (byteString.length < 2) | 49 if (byteString.length < 2) |
31 byteString = "0" + byteString; | 50 byteString = "0" + byteString; |
32 hexBytes.push(byteString); | 51 hexBytes.push(byteString); |
33 } | 52 } |
34 | 53 |
35 return "[" + hexBytes.join(" ") + "]"; | 54 return hexBytes.join(""); |
36 } | 55 } |
37 | 56 |
38 function arrayBufferToHexString(buffer) | 57 function hexStringToUint8Array(hexString) |
39 { | 58 { |
40 return byteArrayToHexString(new Uint8Array(buffer)); | 59 if (hexString.length % 2 != 0) |
| 60 throw "Invalid hexString"; |
| 61 var arrayBuffer = new Uint8Array(hexString.length / 2); |
| 62 |
| 63 for (var i = 0; i < hexString.length; i += 2) { |
| 64 var byteValue = parseInt(hexString.substr(i, 2), 16); |
| 65 if (byteValue == NaN) |
| 66 throw "Invalid hexString"; |
| 67 arrayBuffer[i/2] = byteValue; |
| 68 } |
| 69 |
| 70 return arrayBuffer; |
41 } | 71 } |
42 | 72 |
43 function asciiToArrayBuffer(str) | 73 function asciiToUint8Array(str) |
44 { | 74 { |
45 var chars = []; | 75 var chars = []; |
46 for (var i = 0; i < str.length; ++i) | 76 for (var i = 0; i < str.length; ++i) |
47 chars.push(str.charCodeAt(i)); | 77 chars.push(str.charCodeAt(i)); |
48 return new Uint8Array(chars); | 78 return new Uint8Array(chars); |
49 } | 79 } |
50 | 80 |
51 function failAndFinishJSTest(error) | 81 function failAndFinishJSTest(error) |
52 { | 82 { |
53 if (error) | 83 if (error) |
54 debug(error); | 84 debug(error); |
55 finishJSTest(); | 85 finishJSTest(); |
56 } | 86 } |
OLD | NEW |