Fix var conversions that ESLint could not do automatically

This mainly involves the `crypto_spec.js` file which declared most
variables before their usage, which is not really consistent with the
rest of the codebase. This also required reformatting some long arrays
in that file because otherwise we would exceed the 80 character line
limit. Overall, this makes the code more readable.
This commit is contained in:
Tim van der Meij 2020-10-25 16:03:11 +01:00
parent 3e2bfb5819
commit fe08ef4e39
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
4 changed files with 276 additions and 250 deletions

View File

@ -36,11 +36,9 @@ describe("crypto", function () {
function hex2binary(s) { function hex2binary(s) {
const digits = "0123456789ABCDEF"; const digits = "0123456789ABCDEF";
s = s.toUpperCase(); s = s.toUpperCase();
let n = s.length >> 1, const n = s.length >> 1;
i,
j;
const result = new Uint8Array(n); const result = new Uint8Array(n);
for (i = 0, j = 0; i < n; ++i) { for (let i = 0, j = 0; i < n; ++i) {
const d1 = s.charAt(j++); const d1 = s.charAt(j++);
const d2 = s.charAt(j++); const d2 = s.charAt(j++);
const value = (digits.indexOf(d1) << 4) | digits.indexOf(d2); const value = (digits.indexOf(d1) << 4) | digits.indexOf(d2);
@ -52,57 +50,50 @@ describe("crypto", function () {
// RFC 1321, A.5 Test suite // RFC 1321, A.5 Test suite
describe("calculateMD5", function () { describe("calculateMD5", function () {
it("should pass RFC 1321 test #1", function () { it("should pass RFC 1321 test #1", function () {
let input, result, expected; const input = stringToBytes("");
input = stringToBytes(""); const result = calculateMD5(input, 0, input.length);
result = calculateMD5(input, 0, input.length); const expected = hex2binary("d41d8cd98f00b204e9800998ecf8427e");
expected = hex2binary("d41d8cd98f00b204e9800998ecf8427e");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass RFC 1321 test #2", function () { it("should pass RFC 1321 test #2", function () {
let input, result, expected; const input = stringToBytes("a");
input = stringToBytes("a"); const result = calculateMD5(input, 0, input.length);
result = calculateMD5(input, 0, input.length); const expected = hex2binary("0cc175b9c0f1b6a831c399e269772661");
expected = hex2binary("0cc175b9c0f1b6a831c399e269772661");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass RFC 1321 test #3", function () { it("should pass RFC 1321 test #3", function () {
let input, result, expected; const input = stringToBytes("abc");
input = stringToBytes("abc"); const result = calculateMD5(input, 0, input.length);
result = calculateMD5(input, 0, input.length); const expected = hex2binary("900150983cd24fb0d6963f7d28e17f72");
expected = hex2binary("900150983cd24fb0d6963f7d28e17f72");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass RFC 1321 test #4", function () { it("should pass RFC 1321 test #4", function () {
let input, result, expected; const input = stringToBytes("message digest");
input = stringToBytes("message digest"); const result = calculateMD5(input, 0, input.length);
result = calculateMD5(input, 0, input.length); const expected = hex2binary("f96b697d7cb7938d525a2f31aaf161d0");
expected = hex2binary("f96b697d7cb7938d525a2f31aaf161d0");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass RFC 1321 test #5", function () { it("should pass RFC 1321 test #5", function () {
let input, result, expected; const input = stringToBytes("abcdefghijklmnopqrstuvwxyz");
input = stringToBytes("abcdefghijklmnopqrstuvwxyz"); const result = calculateMD5(input, 0, input.length);
result = calculateMD5(input, 0, input.length); const expected = hex2binary("c3fcd3d76192e4007dfb496cca67e13b");
expected = hex2binary("c3fcd3d76192e4007dfb496cca67e13b");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass RFC 1321 test #6", function () { it("should pass RFC 1321 test #6", function () {
let input, result, expected; const input = stringToBytes(
input = stringToBytes(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
); );
result = calculateMD5(input, 0, input.length); const result = calculateMD5(input, 0, input.length);
expected = hex2binary("d174ab98d277d9f5a5611c2c9f419d9f"); const expected = hex2binary("d174ab98d277d9f5a5611c2c9f419d9f");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass RFC 1321 test #7", function () { it("should pass RFC 1321 test #7", function () {
let input, result, expected; const input = stringToBytes(
input = stringToBytes(
"123456789012345678901234567890123456789012345678" + "123456789012345678901234567890123456789012345678" +
"90123456789012345678901234567890" "90123456789012345678901234567890"
); );
result = calculateMD5(input, 0, input.length); const result = calculateMD5(input, 0, input.length);
expected = hex2binary("57edf4a22be3c955ac49da2e2107b67a"); const expected = hex2binary("57edf4a22be3c955ac49da2e2107b67a");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
}); });
@ -110,45 +101,40 @@ describe("crypto", function () {
// http://www.freemedialibrary.com/index.php/RC4_test_vectors are used // http://www.freemedialibrary.com/index.php/RC4_test_vectors are used
describe("ARCFourCipher", function () { describe("ARCFourCipher", function () {
it("should pass test #1", function () { it("should pass test #1", function () {
let key, input, result, expected, cipher; const key = hex2binary("0123456789abcdef");
key = hex2binary("0123456789abcdef"); const input = hex2binary("0123456789abcdef");
input = hex2binary("0123456789abcdef"); const cipher = new ARCFourCipher(key);
cipher = new ARCFourCipher(key); const result = cipher.encryptBlock(input);
result = cipher.encryptBlock(input); const expected = hex2binary("75b7878099e0c596");
expected = hex2binary("75b7878099e0c596");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass test #2", function () { it("should pass test #2", function () {
let key, input, result, expected, cipher; const key = hex2binary("0123456789abcdef");
key = hex2binary("0123456789abcdef"); const input = hex2binary("0000000000000000");
input = hex2binary("0000000000000000"); const cipher = new ARCFourCipher(key);
cipher = new ARCFourCipher(key); const result = cipher.encryptBlock(input);
result = cipher.encryptBlock(input); const expected = hex2binary("7494c2e7104b0879");
expected = hex2binary("7494c2e7104b0879");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass test #3", function () { it("should pass test #3", function () {
let key, input, result, expected, cipher; const key = hex2binary("0000000000000000");
key = hex2binary("0000000000000000"); const input = hex2binary("0000000000000000");
input = hex2binary("0000000000000000"); const cipher = new ARCFourCipher(key);
cipher = new ARCFourCipher(key); const result = cipher.encryptBlock(input);
result = cipher.encryptBlock(input); const expected = hex2binary("de188941a3375d3a");
expected = hex2binary("de188941a3375d3a");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass test #4", function () { it("should pass test #4", function () {
let key, input, result, expected, cipher; const key = hex2binary("ef012345");
key = hex2binary("ef012345"); const input = hex2binary("00000000000000000000");
input = hex2binary("00000000000000000000"); const cipher = new ARCFourCipher(key);
cipher = new ARCFourCipher(key); const result = cipher.encryptBlock(input);
result = cipher.encryptBlock(input); const expected = hex2binary("d6a141a7ec3c38dfbd61");
expected = hex2binary("d6a141a7ec3c38dfbd61");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass test #5", function () { it("should pass test #5", function () {
let key, input, result, expected, cipher; const key = hex2binary("0123456789abcdef");
key = hex2binary("0123456789abcdef"); const input = hex2binary(
input = hex2binary(
"010101010101010101010101010101010101010101010101010" + "010101010101010101010101010101010101010101010101010" +
"10101010101010101010101010101010101010101010101010101010101010101010" + "10101010101010101010101010101010101010101010101010101010101010101010" +
"10101010101010101010101010101010101010101010101010101010101010101010" + "10101010101010101010101010101010101010101010101010101010101010101010" +
@ -166,9 +152,9 @@ describe("crypto", function () {
"10101010101010101010101010101010101010101010101010101010101010101010" + "10101010101010101010101010101010101010101010101010101010101010101010" +
"101010101010101010101" "101010101010101010101"
); );
cipher = new ARCFourCipher(key); const cipher = new ARCFourCipher(key);
result = cipher.encryptBlock(input); const result = cipher.encryptBlock(input);
expected = hex2binary( const expected = hex2binary(
"7595c3e6114a09780c4ad452338e1ffd9a1be9498f813d76" + "7595c3e6114a09780c4ad452338e1ffd9a1be9498f813d76" +
"533449b6778dcad8c78a8d2ba9ac66085d0e53d59c26c2d1c490c1ebbe0ce66d1b6b" + "533449b6778dcad8c78a8d2ba9ac66085d0e53d59c26c2d1c490c1ebbe0ce66d1b6b" +
"1b13b6b919b847c25a91447a95e75e4ef16779cde8bf0a95850e32af9689444fd377" + "1b13b6b919b847c25a91447a95e75e4ef16779cde8bf0a95850e32af9689444fd377" +
@ -189,16 +175,15 @@ describe("crypto", function () {
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass test #6", function () { it("should pass test #6", function () {
let key, input, result, expected, cipher; const key = hex2binary("fb029e3031323334");
key = hex2binary("fb029e3031323334"); const input = hex2binary(
input = hex2binary(
"aaaa0300000008004500004e661a00008011be640a0001220af" + "aaaa0300000008004500004e661a00008011be640a0001220af" +
"fffff00890089003a000080a601100001000000000000204543454a4548454346434" + "fffff00890089003a000080a601100001000000000000204543454a4548454346434" +
"550464545494546464343414341434143414341414100002000011bd0b604" "550464545494546464343414341434143414341414100002000011bd0b604"
); );
cipher = new ARCFourCipher(key); const cipher = new ARCFourCipher(key);
result = cipher.encryptBlock(input); const result = cipher.encryptBlock(input);
expected = hex2binary( const expected = hex2binary(
"f69c5806bd6ce84626bcbefb9474650aad1f7909b0f64d5f" + "f69c5806bd6ce84626bcbefb9474650aad1f7909b0f64d5f" +
"58a503a258b7ed22eb0ea64930d3a056a55742fcce141d485f8aa836dea18df42c53" + "58a503a258b7ed22eb0ea64930d3a056a55742fcce141d485f8aa836dea18df42c53" +
"80805ad0c61a5d6f58f41040b24b7d1a693856ed0d4398e7aee3bf0e2a2ca8f7" "80805ad0c61a5d6f58f41040b24b7d1a693856ed0d4398e7aee3bf0e2a2ca8f7"
@ -206,14 +191,13 @@ describe("crypto", function () {
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should pass test #7", function () { it("should pass test #7", function () {
let key, input, result, expected, cipher; const key = hex2binary("0123456789abcdef");
key = hex2binary("0123456789abcdef"); const input = hex2binary(
input = hex2binary(
"123456789abcdef0123456789abcdef0123456789abcdef012345678" "123456789abcdef0123456789abcdef0123456789abcdef012345678"
); );
cipher = new ARCFourCipher(key); const cipher = new ARCFourCipher(key);
result = cipher.encryptBlock(input); const result = cipher.encryptBlock(input);
expected = hex2binary( const expected = hex2binary(
"66a0949f8af7d6891f7f832ba833c00c892ebe30143ce28740011ecf" "66a0949f8af7d6891f7f832ba833c00c892ebe30143ce28740011ecf"
); );
expect(result).toEqual(expected); expect(result).toEqual(expected);
@ -222,21 +206,19 @@ describe("crypto", function () {
describe("calculateSHA256", function () { describe("calculateSHA256", function () {
it("should properly hash abc", function () { it("should properly hash abc", function () {
let input, result, expected; const input = stringToBytes("abc");
input = stringToBytes("abc"); const result = calculateSHA256(input, 0, input.length);
result = calculateSHA256(input, 0, input.length); const expected = hex2binary(
expected = hex2binary(
"BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD" "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD"
); );
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should properly hash a multiblock input", function () { it("should properly hash a multiblock input", function () {
let input, result, expected; const input = stringToBytes(
input = stringToBytes(
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
); );
result = calculateSHA256(input, 0, input.length); const result = calculateSHA256(input, 0, input.length);
expected = hex2binary( const expected = hex2binary(
"248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1" "248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1"
); );
expect(result).toEqual(expected); expect(result).toEqual(expected);
@ -245,24 +227,22 @@ describe("crypto", function () {
describe("calculateSHA384", function () { describe("calculateSHA384", function () {
it("should properly hash abc", function () { it("should properly hash abc", function () {
let input, result, expected; const input = stringToBytes("abc");
input = stringToBytes("abc"); const result = calculateSHA384(input, 0, input.length);
result = calculateSHA384(input, 0, input.length); const expected = hex2binary(
expected = hex2binary(
"CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED163" + "CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED163" +
"1A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7" "1A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7"
); );
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should properly hash a multiblock input", function () { it("should properly hash a multiblock input", function () {
let input, result, expected; const input = stringToBytes(
input = stringToBytes(
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklm" + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklm" +
"ghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrs" + "ghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrs" +
"mnopqrstnopqrstu" "mnopqrstnopqrstu"
); );
result = calculateSHA384(input, 0, input.length); const result = calculateSHA384(input, 0, input.length);
expected = hex2binary( const expected = hex2binary(
"09330C33F71147E83D192FC782CD1B4753111B173B3B05D2" + "09330C33F71147E83D192FC782CD1B4753111B173B3B05D2" +
"2FA08086E3B0F712FCC7C71A557E2DB966C3E9FA91746039" "2FA08086E3B0F712FCC7C71A557E2DB966C3E9FA91746039"
); );
@ -272,10 +252,9 @@ describe("crypto", function () {
describe("calculateSHA512", function () { describe("calculateSHA512", function () {
it("should properly hash abc", function () { it("should properly hash abc", function () {
let input, result, expected; const input = stringToBytes("abc");
input = stringToBytes("abc"); const result = calculateSHA512(input, 0, input.length);
result = calculateSHA512(input, 0, input.length); const expected = hex2binary(
expected = hex2binary(
"DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA2" + "DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA2" +
"0A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD" + "0A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD" +
"454D4423643CE80E2A9AC94FA54CA49F" "454D4423643CE80E2A9AC94FA54CA49F"
@ -283,14 +262,13 @@ describe("crypto", function () {
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should properly hash a multiblock input", function () { it("should properly hash a multiblock input", function () {
let input, result, expected; const input = stringToBytes(
input = stringToBytes(
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklm" + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklm" +
"ghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrs" + "ghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrs" +
"mnopqrstnopqrstu" "mnopqrstnopqrstu"
); );
result = calculateSHA512(input, 0, input.length); const result = calculateSHA512(input, 0, input.length);
expected = hex2binary( const expected = hex2binary(
"8E959B75DAE313DA8CF4F72814FC143F8F7779C6EB9F7FA1" + "8E959B75DAE313DA8CF4F72814FC143F8F7779C6EB9F7FA1" +
"7299AEADB6889018501D289E4900F7E4331B99DEC4B5433A" + "7299AEADB6889018501D289E4900F7E4331B99DEC4B5433A" +
"C7D329EEB6DD26545E96E55B874BE909" "C7D329EEB6DD26545E96E55B874BE909"
@ -302,28 +280,26 @@ describe("crypto", function () {
describe("AES128", function () { describe("AES128", function () {
describe("Encryption", function () { describe("Encryption", function () {
it("should be able to encrypt a block", function () { it("should be able to encrypt a block", function () {
let input, key, result, expected, iv, cipher; const input = hex2binary("00112233445566778899aabbccddeeff");
input = hex2binary("00112233445566778899aabbccddeeff"); const key = hex2binary("000102030405060708090a0b0c0d0e0f");
key = hex2binary("000102030405060708090a0b0c0d0e0f"); const iv = hex2binary("00000000000000000000000000000000");
iv = hex2binary("00000000000000000000000000000000"); const cipher = new AES128Cipher(key);
cipher = new AES128Cipher(key); const result = cipher.encrypt(input, iv);
result = cipher.encrypt(input, iv); const expected = hex2binary("69c4e0d86a7b0430d8cdb78070b4c55a");
expected = hex2binary("69c4e0d86a7b0430d8cdb78070b4c55a");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
}); });
describe("Decryption", function () { describe("Decryption", function () {
it("should be able to decrypt a block with IV in stream", function () { it("should be able to decrypt a block with IV in stream", function () {
let input, key, result, expected, cipher; const input = hex2binary(
input = hex2binary(
"0000000000000000000000000000000069c4e0d86a7b0430d" + "0000000000000000000000000000000069c4e0d86a7b0430d" +
"8cdb78070b4c55a" "8cdb78070b4c55a"
); );
key = hex2binary("000102030405060708090a0b0c0d0e0f"); const key = hex2binary("000102030405060708090a0b0c0d0e0f");
cipher = new AES128Cipher(key); const cipher = new AES128Cipher(key);
result = cipher.decryptBlock(input); const result = cipher.decryptBlock(input);
expected = hex2binary("00112233445566778899aabbccddeeff"); const expected = hex2binary("00112233445566778899aabbccddeeff");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
}); });
@ -332,47 +308,44 @@ describe("crypto", function () {
describe("AES256", function () { describe("AES256", function () {
describe("Encryption", function () { describe("Encryption", function () {
it("should be able to encrypt a block", function () { it("should be able to encrypt a block", function () {
let input, key, result, expected, iv, cipher; const input = hex2binary("00112233445566778899aabbccddeeff");
input = hex2binary("00112233445566778899aabbccddeeff"); const key = hex2binary(
key = hex2binary(
"000102030405060708090a0b0c0d0e0f101112131415161718" + "000102030405060708090a0b0c0d0e0f101112131415161718" +
"191a1b1c1d1e1f" "191a1b1c1d1e1f"
); );
iv = hex2binary("00000000000000000000000000000000"); const iv = hex2binary("00000000000000000000000000000000");
cipher = new AES256Cipher(key); const cipher = new AES256Cipher(key);
result = cipher.encrypt(input, iv); const result = cipher.encrypt(input, iv);
expected = hex2binary("8ea2b7ca516745bfeafc49904b496089"); const expected = hex2binary("8ea2b7ca516745bfeafc49904b496089");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
}); });
describe("Decryption", function () { describe("Decryption", function () {
it("should be able to decrypt a block with specified iv", function () { it("should be able to decrypt a block with specified iv", function () {
let input, key, result, expected, cipher, iv; const input = hex2binary("8ea2b7ca516745bfeafc49904b496089");
input = hex2binary("8ea2b7ca516745bfeafc49904b496089"); const key = hex2binary(
key = hex2binary(
"000102030405060708090a0b0c0d0e0f101112131415161718" + "000102030405060708090a0b0c0d0e0f101112131415161718" +
"191a1b1c1d1e1f" "191a1b1c1d1e1f"
); );
iv = hex2binary("00000000000000000000000000000000"); const iv = hex2binary("00000000000000000000000000000000");
cipher = new AES256Cipher(key); const cipher = new AES256Cipher(key);
result = cipher.decryptBlock(input, false, iv); const result = cipher.decryptBlock(input, false, iv);
expected = hex2binary("00112233445566778899aabbccddeeff"); const expected = hex2binary("00112233445566778899aabbccddeeff");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should be able to decrypt a block with IV in stream", function () { it("should be able to decrypt a block with IV in stream", function () {
let input, key, result, expected, cipher; const input = hex2binary(
input = hex2binary(
"000000000000000000000000000000008ea2b7ca516745bf" + "000000000000000000000000000000008ea2b7ca516745bf" +
"eafc49904b496089" "eafc49904b496089"
); );
key = hex2binary( const key = hex2binary(
"000102030405060708090a0b0c0d0e0f101112131415161718" + "000102030405060708090a0b0c0d0e0f101112131415161718" +
"191a1b1c1d1e1f" "191a1b1c1d1e1f"
); );
cipher = new AES256Cipher(key); const cipher = new AES256Cipher(key);
result = cipher.decryptBlock(input, false); const result = cipher.decryptBlock(input, false);
expected = hex2binary("00112233445566778899aabbccddeeff"); const expected = hex2binary("00112233445566778899aabbccddeeff");
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
}); });
@ -380,36 +353,50 @@ describe("crypto", function () {
describe("PDF17Algorithm", function () { describe("PDF17Algorithm", function () {
it("should correctly check a user key", function () { it("should correctly check a user key", function () {
let password, userValidation, userPassword, alg, result; const alg = new PDF17();
alg = new PDF17(); const password = new Uint8Array([117, 115, 101, 114]);
password = new Uint8Array([117, 115, 101, 114]);
userValidation = new Uint8Array([117, 169, 4, 32, 159, 101, 22, 220]);
// prettier-ignore // prettier-ignore
userPassword = new Uint8Array([131, 242, 143, 160, 87, 2, 138, 134, 79, const userValidation = new Uint8Array([
253, 189, 173, 224, 73, 144, 241, 190, 81, 117, 169, 4, 32, 159, 101, 22, 220
197, 15, 249, 105, 145, 151, 15, 194, 65, ]);
3, 1, 126, 187, 221]); // prettier-ignore
result = alg.checkUserPassword(password, userValidation, userPassword); const userPassword = new Uint8Array([
131, 242, 143, 160, 87, 2, 138, 134, 79,
253, 189, 173, 224, 73, 144, 241, 190, 81,
197, 15, 249, 105, 145, 151, 15, 194, 65,
3, 1, 126, 187, 221
]);
const result = alg.checkUserPassword(
password,
userValidation,
userPassword
);
expect(result).toEqual(true); expect(result).toEqual(true);
}); });
it("should correctly check an owner key", function () { it("should correctly check an owner key", function () {
let password, ownerValidation, ownerPassword, alg, result, uBytes; const alg = new PDF17();
alg = new PDF17(); const password = new Uint8Array([111, 119, 110, 101, 114]);
password = new Uint8Array([111, 119, 110, 101, 114]);
ownerValidation = new Uint8Array([243, 118, 71, 153, 128, 17, 101, 62]);
// prettier-ignore // prettier-ignore
ownerPassword = new Uint8Array([60, 98, 137, 35, 51, 101, 200, 152, 210, const ownerValidation = new Uint8Array([
178, 226, 228, 134, 205, 163, 24, 204, 243, 118, 71, 153, 128, 17, 101, 62
126, 177, 36, 106, 50, 36, 125, 210, 172, ]);
171, 120, 222, 108, 139, 115]);
// prettier-ignore // prettier-ignore
uBytes = new Uint8Array([131, 242, 143, 160, 87, 2, 138, 134, 79, 253, const ownerPassword = new Uint8Array([
189, 173, 224, 73, 144, 241, 190, 81, 197, 15, 60, 98, 137, 35, 51, 101, 200, 152, 210,
249, 105, 145, 151, 15, 194, 65, 3, 1, 126, 187, 178, 226, 228, 134, 205, 163, 24, 204,
221, 117, 169, 4, 32, 159, 101, 22, 220, 168, 126, 177, 36, 106, 50, 36, 125, 210, 172,
94, 215, 192, 100, 38, 188, 40]); 171, 120, 222, 108, 139, 115
result = alg.checkOwnerPassword( ]);
// prettier-ignore
const uBytes = new Uint8Array([
131, 242, 143, 160, 87, 2, 138, 134, 79, 253,
189, 173, 224, 73, 144, 241, 190, 81, 197, 15,
249, 105, 145, 151, 15, 194, 65, 3, 1, 126, 187,
221, 117, 169, 4, 32, 159, 101, 22, 220, 168,
94, 215, 192, 100, 38, 188, 40
]);
const result = alg.checkOwnerPassword(
password, password,
ownerValidation, ownerValidation,
uBytes, uBytes,
@ -419,83 +406,112 @@ describe("crypto", function () {
}); });
it("should generate a file encryption key from the user key", function () { it("should generate a file encryption key from the user key", function () {
let password, userKeySalt, expected, alg, result, userEncryption; const alg = new PDF17();
alg = new PDF17(); const password = new Uint8Array([117, 115, 101, 114]);
password = new Uint8Array([117, 115, 101, 114]); const userKeySalt = new Uint8Array([168, 94, 215, 192, 100, 38, 188, 40]);
userKeySalt = new Uint8Array([168, 94, 215, 192, 100, 38, 188, 40]);
// prettier-ignore // prettier-ignore
userEncryption = new Uint8Array([35, 150, 195, 169, 245, 51, 51, 255, const userEncryption = new Uint8Array([
158, 158, 33, 242, 231, 75, 125, 190, 35, 150, 195, 169, 245, 51, 51, 255,
25, 126, 172, 114, 195, 244, 137, 245, 158, 158, 33, 242, 231, 75, 125, 190,
234, 165, 42, 74, 60, 38, 17, 17]); 25, 126, 172, 114, 195, 244, 137, 245,
result = alg.getUserKey(password, userKeySalt, userEncryption); 234, 165, 42, 74, 60, 38, 17, 17
]);
const result = alg.getUserKey(password, userKeySalt, userEncryption);
// prettier-ignore // prettier-ignore
expected = new Uint8Array([63, 114, 136, 209, 87, 61, 12, 30, 249, 1, const expected = new Uint8Array([
186, 144, 254, 248, 163, 153, 151, 51, 133, 63, 114, 136, 209, 87, 61, 12, 30, 249, 1,
10, 80, 152, 206, 15, 72, 187, 231, 33, 224, 186, 144, 254, 248, 163, 153, 151, 51, 133,
239, 13, 213]); 10, 80, 152, 206, 15, 72, 187, 231, 33, 224,
239, 13, 213
]);
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should generate a file encryption key from the owner key", function () { it("should generate a file encryption key from the owner key", function () {
let password, ownerKeySalt, expected, alg, result, ownerEncryption; const alg = new PDF17();
let uBytes; const password = new Uint8Array([111, 119, 110, 101, 114]);
alg = new PDF17();
password = new Uint8Array([111, 119, 110, 101, 114]);
ownerKeySalt = new Uint8Array([200, 245, 242, 12, 218, 123, 24, 120]);
// prettier-ignore // prettier-ignore
ownerEncryption = new Uint8Array([213, 202, 14, 189, 110, 76, 70, 191, 6, const ownerKeySalt = new Uint8Array([
195, 10, 190, 157, 100, 144, 85, 8, 62, 200, 245, 242, 12, 218, 123, 24, 120
123, 178, 156, 229, 50, 40, 229, 216, ]);
54, 222, 34, 38, 106, 223]);
// prettier-ignore // prettier-ignore
uBytes = new Uint8Array([131, 242, 143, 160, 87, 2, 138, 134, 79, 253, const ownerEncryption = new Uint8Array([
189, 173, 224, 73, 144, 241, 190, 81, 197, 15, 213, 202, 14, 189, 110, 76, 70, 191, 6,
249, 105, 145, 151, 15, 194, 65, 3, 1, 126, 187, 195, 10, 190, 157, 100, 144, 85, 8, 62,
221, 117, 169, 4, 32, 159, 101, 22, 220, 168, 123, 178, 156, 229, 50, 40, 229, 216,
94, 215, 192, 100, 38, 188, 40]); 54, 222, 34, 38, 106, 223
result = alg.getOwnerKey(password, ownerKeySalt, uBytes, ownerEncryption); ]);
// prettier-ignore // prettier-ignore
expected = new Uint8Array([63, 114, 136, 209, 87, 61, 12, 30, 249, 1, const uBytes = new Uint8Array([
186, 144, 254, 248, 163, 153, 151, 51, 133, 131, 242, 143, 160, 87, 2, 138, 134, 79, 253,
10, 80, 152, 206, 15, 72, 187, 231, 33, 224, 189, 173, 224, 73, 144, 241, 190, 81, 197, 15,
239, 13, 213]); 249, 105, 145, 151, 15, 194, 65, 3, 1, 126, 187,
221, 117, 169, 4, 32, 159, 101, 22, 220, 168,
94, 215, 192, 100, 38, 188, 40
]);
const result = alg.getOwnerKey(
password,
ownerKeySalt,
uBytes,
ownerEncryption
);
// prettier-ignore
const expected = new Uint8Array([
63, 114, 136, 209, 87, 61, 12, 30, 249, 1,
186, 144, 254, 248, 163, 153, 151, 51, 133,
10, 80, 152, 206, 15, 72, 187, 231, 33, 224,
239, 13, 213
]);
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
}); });
describe("PDF20Algorithm", function () { describe("PDF20Algorithm", function () {
it("should correctly check a user key", function () { it("should correctly check a user key", function () {
let password, userValidation, userPassword, alg, result; const alg = new PDF20();
alg = new PDF20(); const password = new Uint8Array([117, 115, 101, 114]);
password = new Uint8Array([117, 115, 101, 114]);
userValidation = new Uint8Array([83, 245, 146, 101, 198, 247, 34, 198]);
// prettier-ignore // prettier-ignore
userPassword = new Uint8Array([94, 230, 205, 75, 166, 99, 250, 76, 219, const userValidation = new Uint8Array([
128, 17, 85, 57, 17, 33, 164, 150, 46, 83, 245, 146, 101, 198, 247, 34, 198
103, 176, 160, 156, 187, 233, 166, 223, ]);
163, 253, 147, 235, 95, 184]); // prettier-ignore
result = alg.checkUserPassword(password, userValidation, userPassword); const userPassword = new Uint8Array([
94, 230, 205, 75, 166, 99, 250, 76, 219,
128, 17, 85, 57, 17, 33, 164, 150, 46,
103, 176, 160, 156, 187, 233, 166, 223,
163, 253, 147, 235, 95, 184
]);
const result = alg.checkUserPassword(
password,
userValidation,
userPassword
);
expect(result).toEqual(true); expect(result).toEqual(true);
}); });
it("should correctly check an owner key", function () { it("should correctly check an owner key", function () {
let password, ownerValidation, ownerPassword, alg, result, uBytes; const alg = new PDF20();
alg = new PDF20(); const password = new Uint8Array([111, 119, 110, 101, 114]);
password = new Uint8Array([111, 119, 110, 101, 114]);
ownerValidation = new Uint8Array([142, 232, 169, 208, 202, 214, 5, 185]);
// prettier-ignore // prettier-ignore
ownerPassword = new Uint8Array([88, 232, 62, 54, 245, 26, 245, 209, 137, const ownerValidation = new Uint8Array([
123, 221, 72, 199, 49, 37, 217, 31, 74, 142, 232, 169, 208, 202, 214, 5, 185
115, 167, 127, 158, 176, 77, 45, 163, 87, ]);
47, 39, 90, 217, 141]);
// prettier-ignore // prettier-ignore
uBytes = new Uint8Array([94, 230, 205, 75, 166, 99, 250, 76, 219, 128, const ownerPassword = new Uint8Array([
17, 85, 57, 17, 33, 164, 150, 46, 103, 176, 160, 88, 232, 62, 54, 245, 26, 245, 209, 137,
156, 187, 233, 166, 223, 163, 253, 147, 235, 95, 123, 221, 72, 199, 49, 37, 217, 31, 74,
184, 83, 245, 146, 101, 198, 247, 34, 198, 191, 115, 167, 127, 158, 176, 77, 45, 163, 87,
11, 16, 94, 237, 216, 20, 175]); 47, 39, 90, 217, 141
result = alg.checkOwnerPassword( ]);
// prettier-ignore
const uBytes = new Uint8Array([
94, 230, 205, 75, 166, 99, 250, 76, 219, 128,
17, 85, 57, 17, 33, 164, 150, 46, 103, 176, 160,
156, 187, 233, 166, 223, 163, 253, 147, 235, 95,
184, 83, 245, 146, 101, 198, 247, 34, 198, 191,
11, 16, 94, 237, 216, 20, 175
]);
const result = alg.checkOwnerPassword(
password, password,
ownerValidation, ownerValidation,
uBytes, uBytes,
@ -505,47 +521,59 @@ describe("crypto", function () {
}); });
it("should generate a file encryption key from the user key", function () { it("should generate a file encryption key from the user key", function () {
let password, userKeySalt, expected, alg, result, userEncryption; const alg = new PDF20();
alg = new PDF20(); const password = new Uint8Array([117, 115, 101, 114]);
password = new Uint8Array([117, 115, 101, 114]); const userKeySalt = new Uint8Array([191, 11, 16, 94, 237, 216, 20, 175]);
userKeySalt = new Uint8Array([191, 11, 16, 94, 237, 216, 20, 175]);
// prettier-ignore // prettier-ignore
userEncryption = new Uint8Array([121, 208, 2, 181, 230, 89, 156, 60, 253, const userEncryption = new Uint8Array([
143, 212, 28, 84, 180, 196, 177, 173, 121, 208, 2, 181, 230, 89, 156, 60, 253,
128, 221, 107, 46, 20, 94, 186, 135, 51, 143, 212, 28, 84, 180, 196, 177, 173,
95, 24, 20, 223, 254, 36]); 128, 221, 107, 46, 20, 94, 186, 135, 51,
result = alg.getUserKey(password, userKeySalt, userEncryption); 95, 24, 20, 223, 254, 36
]);
const result = alg.getUserKey(password, userKeySalt, userEncryption);
// prettier-ignore // prettier-ignore
expected = new Uint8Array([42, 218, 213, 39, 73, 91, 72, 79, 67, 38, 248, const expected = new Uint8Array([
133, 18, 189, 61, 34, 107, 79, 29, 56, 59, 42, 218, 213, 39, 73, 91, 72, 79, 67, 38, 248,
181, 213, 118, 113, 34, 65, 210, 87, 174, 22, 133, 18, 189, 61, 34, 107, 79, 29, 56, 59,
239]); 181, 213, 118, 113, 34, 65, 210, 87, 174, 22,
239
]);
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
it("should generate a file encryption key from the owner key", function () { it("should generate a file encryption key from the owner key", function () {
let password, ownerKeySalt, expected, alg, result, ownerEncryption; const alg = new PDF20();
let uBytes; const password = new Uint8Array([111, 119, 110, 101, 114]);
alg = new PDF20(); const ownerKeySalt = new Uint8Array([29, 208, 185, 46, 11, 76, 135, 149]);
password = new Uint8Array([111, 119, 110, 101, 114]);
ownerKeySalt = new Uint8Array([29, 208, 185, 46, 11, 76, 135, 149]);
// prettier-ignore // prettier-ignore
ownerEncryption = new Uint8Array([209, 73, 224, 77, 103, 155, 201, 181, const ownerEncryption = new Uint8Array([
190, 68, 223, 20, 62, 90, 56, 210, 5, 209, 73, 224, 77, 103, 155, 201, 181,
240, 178, 128, 238, 124, 68, 254, 253, 190, 68, 223, 20, 62, 90, 56, 210, 5,
244, 62, 108, 208, 135, 10, 251]); 240, 178, 128, 238, 124, 68, 254, 253,
244, 62, 108, 208, 135, 10, 251
]);
// prettier-ignore // prettier-ignore
uBytes = new Uint8Array([94, 230, 205, 75, 166, 99, 250, 76, 219, 128, const uBytes = new Uint8Array([
17, 85, 57, 17, 33, 164, 150, 46, 103, 176, 160, 94, 230, 205, 75, 166, 99, 250, 76, 219, 128,
156, 187, 233, 166, 223, 163, 253, 147, 235, 95, 17, 85, 57, 17, 33, 164, 150, 46, 103, 176, 160,
184, 83, 245, 146, 101, 198, 247, 34, 198, 191, 156, 187, 233, 166, 223, 163, 253, 147, 235, 95,
11, 16, 94, 237, 216, 20, 175]); 184, 83, 245, 146, 101, 198, 247, 34, 198, 191,
result = alg.getOwnerKey(password, ownerKeySalt, uBytes, ownerEncryption); 11, 16, 94, 237, 216, 20, 175
]);
const result = alg.getOwnerKey(
password,
ownerKeySalt,
uBytes,
ownerEncryption
);
// prettier-ignore // prettier-ignore
expected = new Uint8Array([42, 218, 213, 39, 73, 91, 72, 79, 67, 38, 248, const expected = new Uint8Array([
133, 18, 189, 61, 34, 107, 79, 29, 56, 59, 42, 218, 213, 39, 73, 91, 72, 79, 67, 38, 248,
181, 213, 118, 113, 34, 65, 210, 87, 174, 22, 133, 18, 189, 61, 34, 107, 79, 29, 56, 59,
239]); 181, 213, 118, 113, 34, 65, 210, 87, 174, 22,
239
]);
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
}); });

View File

@ -46,20 +46,18 @@ describe("MurmurHash3_64", function () {
it("changes the hash after update without seed", function () { it("changes the hash after update without seed", function () {
const hash = new MurmurHash3_64(); const hash = new MurmurHash3_64();
let hexdigest1, hexdigest2;
hash.update(sourceText); hash.update(sourceText);
hexdigest1 = hash.hexdigest(); const hexdigest1 = hash.hexdigest();
hash.update(sourceText); hash.update(sourceText);
hexdigest2 = hash.hexdigest(); const hexdigest2 = hash.hexdigest();
expect(hexdigest1).not.toEqual(hexdigest2); expect(hexdigest1).not.toEqual(hexdigest2);
}); });
it("changes the hash after update with seed", function () { it("changes the hash after update with seed", function () {
const hash = new MurmurHash3_64(1); const hash = new MurmurHash3_64(1);
let hexdigest1, hexdigest2;
hash.update(sourceText); hash.update(sourceText);
hexdigest1 = hash.hexdigest(); const hexdigest1 = hash.hexdigest();
hash.update(sourceText); hash.update(sourceText);
hexdigest2 = hash.hexdigest(); const hexdigest2 = hash.hexdigest();
expect(hexdigest1).not.toEqual(hexdigest2); expect(hexdigest1).not.toEqual(hexdigest2);
}); });
}); });

View File

@ -38,7 +38,7 @@ describe("network", function () {
let len = 0, let len = 0,
count = 0; count = 0;
var read = function () { const read = function () {
return fullReader.read().then(function (result) { return fullReader.read().then(function (result) {
if (result.done) { if (result.done) {
return undefined; return undefined;
@ -101,7 +101,7 @@ describe("network", function () {
const result1 = { value: 0 }, const result1 = { value: 0 },
result2 = { value: 0 }; result2 = { value: 0 };
var read = function (reader, lenResult) { const read = function (reader, lenResult) {
return reader.read().then(function (result) { return reader.read().then(function (result) {
if (result.done) { if (result.done) {
return undefined; return undefined;

View File

@ -301,7 +301,7 @@ describe("ui_utils", function () {
eventBus.off("test", listener2); eventBus.off("test", listener2);
count++; count++;
}; };
var listener2 = function () { const listener2 = function () {
eventBus.off("test", listener1); eventBus.off("test", listener1);
count++; count++;
}; };