-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Operation: Extract Hash Values #512
Changes from 6 commits
1b16c26
98edef3
de8ed69
a6b774d
3983e1a
61295a9
6b95ba7
8d4ad6a
077b11e
21e5641
d2bd397
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* @author mshwed [[email protected]] | ||
* @copyright Crown Copyright 2019 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
import { search } from "../lib/Extract.mjs"; | ||
|
||
/** | ||
* Extract Hash Values operation | ||
*/ | ||
class ExtractHashes extends Operation { | ||
|
||
/** | ||
* ExtractHashValues constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "Extract hashes"; | ||
this.module = "Regex"; | ||
this.description = "Extracts potential hashes based on hash character length"; | ||
this.infoURL = "https://en.wikipedia.org/wiki/Comparison_of_cryptographic_hash_functions"; | ||
this.inputType = "string"; | ||
this.outputType = "string"; | ||
this.args = [ | ||
{ | ||
name: "Hash character length", | ||
type: "number", | ||
value: 40 | ||
}, | ||
{ | ||
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 = []; | ||
let hashCount = 0; | ||
|
||
const hashLength = args[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: This can be shortened to:
|
||
const searchAllHashes = args[1]; | ||
const showDisplayTotal = args[2]; | ||
|
||
// Convert character length to bit length | ||
let hashBitLengths = [(hashLength / 2) * 8]; | ||
|
||
if (searchAllHashes) hashBitLengths = [4, 8, 16, 32, 64, 128, 160, 192, 224, 256, 320, 384, 512, 1024]; | ||
|
||
for (const hashBitLength of hashBitLengths) { | ||
// Convert bit length to character length | ||
const hashCharacterLength = (hashBitLength / 8) * 2; | ||
|
||
const regex = new RegExp(`(\\b|^)[a-f0-9]{${hashCharacterLength}}(\\b|$)`, "g"); | ||
const searchResults = search(input, regex, null, false); | ||
|
||
hashCount += searchResults.split("\n").length - 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This area crashed for me when I tested it out. I've made some changes to hopefully resolve the issues I saw. Could you verify it works for you as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! That works for me as well. |
||
results.push(searchResults); | ||
} | ||
|
||
let output = ""; | ||
if (showDisplayTotal) { | ||
output = `Total Results: ${hashCount}\n\n`; | ||
} | ||
|
||
output = output + results.join(""); | ||
return output; | ||
} | ||
|
||
} | ||
|
||
export default ExtractHashes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please could you add some unit tests. This would help ensure we are extracting the correct hashes in the future!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem! I've added some unit tests.