Tidied up CRC-8 operation and added it to 'Generate all hashes'

This commit is contained in:
n1474335 2019-07-03 15:07:26 +01:00
parent 3766982734
commit 13b0ab73d0
2 changed files with 29 additions and 26 deletions

View file

@ -7,7 +7,7 @@
import Operation from "../Operation"; import Operation from "../Operation";
import OperationError from "../errors/OperationError"; import OperationError from "../errors/OperationError";
import { toHex } from "../lib/Hex"; import { toHexFast } from "../lib/Hex";
/** /**
* CRC-8 Checksum operation * CRC-8 Checksum operation
@ -101,7 +101,7 @@ class CRC8Checksum extends Operation {
if (xorOut !== 0) crc = crc ^ xorOut; if (xorOut !== 0) crc = crc ^ xorOut;
return toHex(new Uint8Array([crc])); return toHexFast(new Uint8Array([crc]));
} }
/** /**
@ -127,29 +127,30 @@ class CRC8Checksum extends Operation {
run(input, args) { run(input, args) {
const algorithm = args[0]; const algorithm = args[0];
if (algorithm === "CRC-8") { switch (algorithm) {
return this.calculateCRC8(input, 0x7, 0x0, false, false, 0x0); case "CRC-8":
} else if (algorithm === "CRC-8/CDMA2000") { return this.calculateCRC8(input, 0x7, 0x0, false, false, 0x0);
return this.calculateCRC8(input, 0x9B, 0xFF, false, false, 0x0); case "CRC-8/CDMA2000":
} else if (algorithm === "CRC-8/DARC") { return this.calculateCRC8(input, 0x9B, 0xFF, false, false, 0x0);
return this.calculateCRC8(input, 0x39, 0x0, true, true, 0x0); case "CRC-8/DARC":
} else if (algorithm === "CRC-8/DVB-S2") { return this.calculateCRC8(input, 0x39, 0x0, true, true, 0x0);
return this.calculateCRC8(input, 0xD5, 0x0, false, false, 0x0); case "CRC-8/DVB-S2":
} else if (algorithm === "CRC-8/EBU") { return this.calculateCRC8(input, 0xD5, 0x0, false, false, 0x0);
return this.calculateCRC8(input, 0x1D, 0xFF, true, true, 0x0); case "CRC-8/EBU":
} else if (algorithm === "CRC-8/I-CODE") { return this.calculateCRC8(input, 0x1D, 0xFF, true, true, 0x0);
return this.calculateCRC8(input, 0x1D, 0xFD, false, false, 0x0); case "CRC-8/I-CODE":
} else if (algorithm === "CRC-8/ITU") { return this.calculateCRC8(input, 0x1D, 0xFD, false, false, 0x0);
return this.calculateCRC8(input, 0x7, 0x0, false, false, 0x55); case "CRC-8/ITU":
} else if (algorithm === "CRC-8/MAXIM") { return this.calculateCRC8(input, 0x7, 0x0, false, false, 0x55);
return this.calculateCRC8(input, 0x31, 0x0, true, true, 0x0); case "CRC-8/MAXIM":
} else if (algorithm === "CRC-8/ROHC") { return this.calculateCRC8(input, 0x31, 0x0, true, true, 0x0);
return this.calculateCRC8(input, 0x7, 0xFF, true, true, 0x0); case "CRC-8/ROHC":
} else if (algorithm === "CRC-8/WCDMA") { return this.calculateCRC8(input, 0x7, 0xFF, true, true, 0x0);
return this.calculateCRC8(input, 0x9B, 0x0, true, true, 0x0); case "CRC-8/WCDMA":
return this.calculateCRC8(input, 0x9B, 0x0, true, true, 0x0);
default:
throw new OperationError("Unknown checksum algorithm");
} }
throw new OperationError("Unknown checksum algorithm");
} }
} }

View file

@ -26,6 +26,7 @@ import Fletcher16Checksum from "./Fletcher16Checksum";
import Fletcher32Checksum from "./Fletcher32Checksum"; import Fletcher32Checksum from "./Fletcher32Checksum";
import Fletcher64Checksum from "./Fletcher64Checksum"; import Fletcher64Checksum from "./Fletcher64Checksum";
import Adler32Checksum from "./Adler32Checksum"; import Adler32Checksum from "./Adler32Checksum";
import CRC8Checksum from "./CRC8Checksum";
import CRC16Checksum from "./CRC16Checksum"; import CRC16Checksum from "./CRC16Checksum";
import CRC32Checksum from "./CRC32Checksum"; import CRC32Checksum from "./CRC32Checksum";
import BLAKE2b from "./BLAKE2b"; import BLAKE2b from "./BLAKE2b";
@ -104,8 +105,9 @@ class GenerateAllHashes extends Operation {
"\nFletcher-32: " + (new Fletcher32Checksum).run(byteArray, []) + "\nFletcher-32: " + (new Fletcher32Checksum).run(byteArray, []) +
"\nFletcher-64: " + (new Fletcher64Checksum).run(byteArray, []) + "\nFletcher-64: " + (new Fletcher64Checksum).run(byteArray, []) +
"\nAdler-32: " + (new Adler32Checksum).run(byteArray, []) + "\nAdler-32: " + (new Adler32Checksum).run(byteArray, []) +
"\nCRC-16: " + (new CRC16Checksum).run(str, []) + "\nCRC-8: " + (new CRC8Checksum).run(arrayBuffer, ["CRC-8"]) +
"\nCRC-32: " + (new CRC32Checksum).run(str, []); "\nCRC-16: " + (new CRC16Checksum).run(arrayBuffer, []) +
"\nCRC-32: " + (new CRC32Checksum).run(arrayBuffer, []);
return output; return output;
} }