mirror of
https://github.com/gchq/CyberChef.git
synced 2024-11-16 08:58:30 +01:00
53 lines
1.1 KiB
JavaScript
53 lines
1.1 KiB
JavaScript
|
/**
|
||
|
* @author n1474335 [n1474335@gmail.com]
|
||
|
* @copyright Crown Copyright 2016
|
||
|
* @license Apache-2.0
|
||
|
*/
|
||
|
|
||
|
import Operation from "../Operation";
|
||
|
import Utils from "../Utils";
|
||
|
|
||
|
/**
|
||
|
* TCP/IP Checksum operation
|
||
|
*/
|
||
|
class TCPIPChecksum extends Operation {
|
||
|
|
||
|
/**
|
||
|
* TCPIPChecksum constructor
|
||
|
*/
|
||
|
constructor() {
|
||
|
super();
|
||
|
|
||
|
this.name = "TCP/IP Checksum";
|
||
|
this.module = "Hashing";
|
||
|
this.description = "Calculates the checksum for a TCP (Transport Control Protocol) or IP (Internet Protocol) header from an input of raw bytes.";
|
||
|
this.inputType = "byteArray";
|
||
|
this.outputType = "string";
|
||
|
this.args = [];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param {byteArray} input
|
||
|
* @param {Object[]} args
|
||
|
* @returns {string}
|
||
|
*/
|
||
|
run(input, args) {
|
||
|
let csum = 0;
|
||
|
|
||
|
for (let i = 0; i < input.length; i++) {
|
||
|
if (i % 2 === 0) {
|
||
|
csum += (input[i] << 8);
|
||
|
} else {
|
||
|
csum += input[i];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
csum = (csum >> 16) + (csum & 0xffff);
|
||
|
|
||
|
return Utils.hex(0xffff - csum);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
export default TCPIPChecksum;
|