CyberChef/src/core/operations/MAC.js

91 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-11-28 11:42:58 +01:00
/**
* MAC address operations.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @namespace
*/
const MAC = {
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
OUTPUT_CASE: ["Both", "Upper only", "Lower only"],
/**
* @constant
* @default
*/
NO_DELIM: true,
/**
* @constant
* @default
*/
DASH_DELIM: true,
/**
* @constant
* @default
*/
COLON_DELIM: true,
/**
* @constant
* @default
*/
CISCO_STYLE: false,
/**
* Format MAC addresses operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runFormat: function(input, args) {
2016-11-28 11:42:58 +01:00
if (!input) return "";
2017-02-09 16:09:33 +01:00
var outputCase = args[0],
noDelim = args[1],
dashDelim = args[2],
colonDelim = args[3],
ciscoStyle = args[4],
outputList = [],
2016-11-28 11:42:58 +01:00
macs = input.toLowerCase().split(/[,\s\r\n]+/);
macs.forEach(function(mac) {
2016-12-14 17:39:17 +01:00
var cleanMac = mac.replace(/[:.-]+/g, ""),
macHyphen = cleanMac.replace(/(.{2}(?=.))/g, "$1-"),
macColon = cleanMac.replace(/(.{2}(?=.))/g, "$1:"),
macCisco = cleanMac.replace(/(.{4}(?=.))/g, "$1.");
2017-02-09 16:09:33 +01:00
if (outputCase === "Lower only") {
if (noDelim) outputList.push(cleanMac);
if (dashDelim) outputList.push(macHyphen);
if (colonDelim) outputList.push(macColon);
if (ciscoStyle) outputList.push(macCisco);
} else if (outputCase === "Upper only") {
if (noDelim) outputList.push(cleanMac.toUpperCase());
if (dashDelim) outputList.push(macHyphen.toUpperCase());
if (colonDelim) outputList.push(macColon.toUpperCase());
if (ciscoStyle) outputList.push(macCisco.toUpperCase());
2016-11-28 11:42:58 +01:00
} else {
if (noDelim) outputList.push(cleanMac, cleanMac.toUpperCase());
if (dashDelim) outputList.push(macHyphen, macHyphen.toUpperCase());
if (colonDelim) outputList.push(macColon, macColon.toUpperCase());
if (ciscoStyle) outputList.push(macCisco, macCisco.toUpperCase());
2016-11-28 11:42:58 +01:00
}
2017-02-09 16:09:33 +01:00
outputList.push(
2016-11-28 11:42:58 +01:00
"" // Empty line to delimit groups
);
});
// Return the data as a string
return outputList.join("\n");
2016-11-28 11:42:58 +01:00
},
};
export default MAC;