2019-06-30 21:28:00 -04:00
|
|
|
/**
|
|
|
|
* @author mshwed [m@ttshwed.com]
|
|
|
|
* @copyright Crown Copyright 2019
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CRC-8 Checksum operation
|
|
|
|
*/
|
|
|
|
class CRC8Checksum extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CRC8Checksum constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "CRC-8 Checksum";
|
|
|
|
this.module = "Crypto";
|
|
|
|
this.description = "";
|
|
|
|
this.infoURL = "";
|
|
|
|
this.inputType = "ArrayBuffer";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
2019-07-01 19:02:07 -04:00
|
|
|
"name": "Algorithm",
|
|
|
|
"type": "option",
|
|
|
|
"value": [
|
|
|
|
"CRC-8"
|
|
|
|
]
|
2019-06-30 21:28:00 -04:00
|
|
|
}
|
2019-07-01 19:02:07 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
calculateCRC8(algorithmName, polynomial, initializationValue, refIn, refOut, xorOut, check) {
|
|
|
|
let initializationValue = this.reverseBits();
|
|
|
|
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For an 8 bit initialization value reverse the bits.
|
|
|
|
*
|
|
|
|
* @param input
|
|
|
|
*/
|
|
|
|
reverseBits(input) {
|
|
|
|
let reflectedBits = input.toString(2).split('');
|
|
|
|
for (let i = 0; i < hashSize / 2; i++) {
|
|
|
|
let x = reflectedBits[i];
|
|
|
|
reflectedBits[i] = reflectedBits[hashSize - i - 1];
|
|
|
|
reflectedBits[hashSize - i - 1] = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseInt(reflectedBits.join(''));
|
2019-06-30 21:28:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {byteArray} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
2019-07-01 19:02:07 -04:00
|
|
|
const algorithm = args[0];
|
2019-06-30 21:28:00 -04:00
|
|
|
|
2019-07-01 19:02:07 -04:00
|
|
|
if (algorithm === "CRC-8") {
|
|
|
|
return this.calculateCRC8(algorithm, 0x7, 0x0, false, false, 0x0, 0xF4)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2019-06-30 21:28:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-01 19:02:07 -04:00
|
|
|
const hashSize = 8;
|
|
|
|
|
|
|
|
// const CRC8AlgoParameters = {
|
|
|
|
// 'CRC8'
|
|
|
|
// }
|
|
|
|
|
2019-06-30 21:28:00 -04:00
|
|
|
export default CRC8Checksum;
|