CyberChef/src/core/operations/ExtractHashes.mjs

85 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-03-11 14:32:44 +01:00
/**
* @author mshwed [m@ttshwed.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
2024-03-31 16:57:03 +02:00
import Operation from "../Operation.mjs";
import { search } from "../lib/Extract.mjs";
2019-03-11 14:32:44 +01:00
/**
* Extract Hash Values operation
*/
class ExtractHashes extends Operation {
/**
* ExtractHashValues constructor
*/
constructor() {
super();
2024-04-01 18:01:48 +02:00
this.name = "Extract hashes";
2019-03-11 14:53:12 +01:00
this.module = "Regex";
2019-03-12 01:02:49 +01:00
this.description = "Extracts potential hashes based on hash character length";
2019-03-11 14:32:44 +01:00
this.infoURL = "https://en.wikipedia.org/wiki/Comparison_of_cryptographic_hash_functions";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
2019-03-12 01:02:49 +01:00
name: "Hash character length",
2019-03-11 14:32:44 +01:00
type: "number",
value: 40
2019-03-11 14:32:44 +01:00
},
{
name: "All hashes",
type: "boolean",
value: false
},
{
name: "Display Total",
type: "boolean",
value: false
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const results = [];
2019-03-11 14:32:44 +01:00
let hashCount = 0;
2024-04-02 04:22:43 +02:00
const [hashLength, searchAllHashes, showDisplayTotal] = args;
2019-03-11 14:32:44 +01:00
2019-03-12 01:02:49 +01:00
// Convert character length to bit length
let hashBitLengths = [(hashLength / 2) * 8];
2019-03-11 14:32:44 +01:00
2019-03-12 01:02:49 +01:00
if (searchAllHashes) hashBitLengths = [4, 8, 16, 32, 64, 128, 160, 192, 224, 256, 320, 384, 512, 1024];
for (const hashBitLength of hashBitLengths) {
2019-03-12 01:02:49 +01:00
// Convert bit length to character length
const hashCharacterLength = (hashBitLength / 8) * 2;
2019-03-12 01:02:49 +01:00
const regex = new RegExp(`(\\b|^)[a-f0-9]{${hashCharacterLength}}(\\b|$)`, "g");
2019-03-11 14:32:44 +01:00
const searchResults = search(input, regex, null, false);
hashCount += searchResults.length;
results.push(...searchResults);
2019-03-11 14:32:44 +01:00
}
let output = "";
if (showDisplayTotal) {
output = `Total Results: ${hashCount}\n\n`;
}
output = output + results.join("\n");
2019-03-11 14:32:44 +01:00
return output;
}
}
export default ExtractHashes;