tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

sha3.tentative.https.any.js (5579B)


      1 // META: title=WebCryptoAPI: digest() SHA-3 algorithms
      2 // META: timeout=long
      3 
      4 var subtle = crypto.subtle; // Change to test prefixed implementations
      5 
      6 var sourceData = {
      7  empty: new Uint8Array(0),
      8  short: new Uint8Array([
      9    21, 110, 234, 124, 193, 76, 86, 203, 148, 219, 3, 10, 74, 157, 149, 255,
     10  ]),
     11  medium: new Uint8Array([
     12    182, 200, 249, 223, 100, 140, 208, 136, 183, 15, 56, 231, 65, 151, 177, 140,
     13    184, 30, 30, 67, 80, 213, 11, 204, 184, 251, 90, 115, 121, 200, 123, 178,
     14    227, 214, 237, 84, 97, 237, 30, 159, 54, 243, 64, 163, 150, 42, 68, 107,
     15    129, 91, 121, 75, 75, 212, 58, 68, 3, 80, 32, 119, 178, 37, 108, 200, 7,
     16    131, 127, 58, 172, 209, 24, 235, 75, 156, 43, 174, 184, 151, 6, 134, 37,
     17    171, 172, 161, 147,
     18  ]),
     19 };
     20 
     21 sourceData.long = new Uint8Array(1024 * sourceData.medium.byteLength);
     22 for (var i = 0; i < 1024; i++) {
     23  sourceData.long.set(sourceData.medium, i * sourceData.medium.byteLength);
     24 }
     25 
     26 var digestedData = {
     27  'SHA3-256': {
     28    empty: new Uint8Array([
     29      167, 255, 198, 248, 191, 30, 215, 102, 81, 193, 71, 86, 160, 97, 214, 98,
     30      245, 128, 255, 77, 228, 59, 73, 250, 130, 216, 10, 75, 128, 248, 67, 74,
     31    ]),
     32    short: new Uint8Array([
     33      48, 89, 175, 122, 163, 59, 81, 112, 132, 232, 173, 123, 188, 79, 178, 8,
     34      164, 76, 40, 239, 50, 180, 105, 141, 16, 61, 213, 64, 228, 249, 26, 161,
     35    ]),
     36    medium: new Uint8Array([
     37      31, 167, 205, 29, 167, 76, 216, 4, 100, 23, 80, 140, 131, 20, 231, 74,
     38      154, 74, 157, 56, 249, 241, 142, 108, 178, 21, 184, 200, 145, 160, 168,
     39      14,
     40    ]),
     41    long: new Uint8Array([
     42      178, 207, 198, 30, 3, 134, 205, 174, 245, 225, 10, 43, 225, 137, 137, 31,
     43      94, 245, 42, 118, 36, 191, 205, 142, 220, 137, 58, 204, 100, 254, 198, 0,
     44    ]),
     45  },
     46  'SHA3-384': {
     47    empty: new Uint8Array([
     48      12, 99, 167, 91, 132, 94, 79, 125, 1, 16, 125, 133, 46, 76, 36, 133, 197,
     49      26, 80, 170, 170, 148, 252, 97, 153, 94, 113, 187, 238, 152, 58, 42, 195,
     50      113, 56, 49, 38, 74, 219, 71, 251, 107, 209, 224, 88, 213, 240, 4,
     51    ]),
     52    short: new Uint8Array([
     53      84, 184, 240, 228, 207, 73, 116, 222, 116, 0, 152, 246, 107, 48, 36, 71,
     54      155, 1, 99, 19, 21, 166, 119, 54, 6, 195, 62, 173, 195, 37, 86, 166, 231,
     55      120, 224, 143, 2, 37, 174, 121, 38, 90, 236, 102, 108, 178, 57, 11,
     56    ]),
     57    medium: new Uint8Array([
     58      67, 123, 125, 139, 104, 178, 80, 181, 193, 115, 158, 164, 204, 134, 219,
     59      32, 51, 135, 157, 251, 24, 222, 41, 44, 156, 80, 217, 193, 147, 164, 199,
     60      154, 8, 166, 202, 227, 244, 228, 131, 194, 121, 94, 165, 209, 239, 126,
     61      105, 210,
     62    ]),
     63    long: new Uint8Array([
     64      59, 57, 196, 201, 122, 216, 118, 19, 48, 93, 12, 204, 152, 113, 129, 113,
     65      62, 45, 94, 132, 177, 249, 118, 0, 17, 188, 206, 12, 41, 116, 153, 0, 91,
     66      220, 232, 163, 210, 64, 155, 90, 208, 22, 79, 50, 187, 135, 120, 208,
     67    ]),
     68  },
     69  'SHA3-512': {
     70    empty: new Uint8Array([
     71      166, 159, 115, 204, 162, 58, 154, 197, 200, 181, 103, 220, 24, 90, 117,
     72      110, 151, 201, 130, 22, 79, 226, 88, 89, 224, 209, 220, 193, 71, 92, 128,
     73      166, 21, 178, 18, 58, 241, 245, 249, 76, 17, 227, 233, 64, 44, 58, 197,
     74      88, 245, 0, 25, 157, 149, 182, 211, 227, 1, 117, 133, 134, 40, 29, 205,
     75      38,
     76    ]),
     77    short: new Uint8Array([
     78      45, 210, 224, 122, 98, 230, 173, 4, 152, 186, 132, 243, 19, 196, 212, 2,
     79      76, 180, 96, 1, 247, 143, 117, 219, 51, 107, 13, 77, 139, 210, 169, 236,
     80      21, 44, 74, 210, 8, 120, 115, 93, 130, 186, 8, 114, 236, 245, 150, 8, 239,
     81      60, 237, 43, 42, 134, 105, 66, 126, 125, 163, 30, 54, 35, 51, 216,
     82    ]),
     83    medium: new Uint8Array([
     84      230, 64, 162, 25, 9, 83, 102, 64, 54, 158, 155, 10, 72, 147, 28, 92, 178,
     85      239, 203, 201, 31, 236, 242, 71, 48, 107, 201, 106, 14, 76, 163, 51, 7,
     86      203, 142, 27, 154, 243, 103, 148, 109, 208, 28, 36, 63, 57, 7, 80, 141, 4,
     87      241, 105, 42, 49, 97, 223, 31, 137, 141, 232, 238, 37, 254, 190,
     88    ]),
     89    long: new Uint8Array([
     90      189, 38, 44, 236, 245, 101, 195, 56, 3, 45, 229, 186, 1, 56, 240, 170,
     91      207, 231, 221, 232, 61, 39, 45, 13, 55, 217, 82, 130, 158, 210, 93, 225,
     92      161, 52, 45, 152, 101, 158, 247, 210, 250, 74, 202, 124, 226, 177, 170, 7,
     93      132, 216, 252, 29, 203, 248, 27, 206, 199, 167, 67, 26, 61, 163, 107, 247,
     94    ]),
     95  },
     96 };
     97 
     98 // Test SHA-3 digest algorithms
     99 Object.keys(sourceData).forEach(function (size) {
    100  Object.keys(digestedData).forEach(function (alg) {
    101    promise_test(function (test) {
    102      return crypto.subtle
    103        .digest(alg, sourceData[size])
    104        .then(function (result) {
    105          assert_true(
    106            equalBuffers(result, digestedData[alg][size]),
    107            'digest matches expected'
    108          );
    109        });
    110    }, alg + ' with ' + size + ' source data');
    111 
    112    promise_test(function (test) {
    113      var buffer = new Uint8Array(sourceData[size]);
    114      return crypto.subtle.digest(alg, buffer).then(function (result) {
    115        // Alter the buffer after calling digest
    116        if (buffer.length > 0) {
    117          buffer[0] = ~buffer[0];
    118        }
    119        assert_true(
    120          equalBuffers(result, digestedData[alg][size]),
    121          'digest matches expected'
    122        );
    123      });
    124    }, alg + ' with ' + size + ' source data and altered buffer after call');
    125  });
    126 });
    127 
    128 function equalBuffers(a, b) {
    129  if (a.byteLength !== b.byteLength) {
    130    return false;
    131  }
    132  var aBytes = new Uint8Array(a);
    133  var bBytes = new Uint8Array(b);
    134  for (var i = 0; i < a.byteLength; i++) {
    135    if (aBytes[i] !== bBytes[i]) {
    136      return false;
    137    }
    138  }
    139  return true;
    140 }