-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1b16c26
Operation: Added extract hash feature
MShwed 98edef3
Corrected module type
MShwed de8ed69
Improved description of operation
MShwed a6b774d
Fixed issues with const/let and changed default character length
MShwed 3983e1a
Updated imports
MShwed 61295a9
Lower case 'hash'
a3957273 6b95ba7
Fix regular expresion crash in extract hashes
a3957273 8d4ad6a
Minor changes. Added test cases.
MShwed 077b11e
Fixed op name in test
MShwed 21e5641
Merge branch 'master' into feature/extract-hashes
MShwed d2bd397
Merge branch 'master' into feature/extract-hashes
MShwed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @author mshwed [m@ttshwed.com] | ||
* @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, searchAllHashes, showDisplayTotal] = args; | ||
|
||
// 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.length; | ||
results.push(...searchResults); | ||
} | ||
|
||
let output = ""; | ||
if (showDisplayTotal) { | ||
output = `Total Results: ${hashCount}\n\n`; | ||
} | ||
|
||
output = output + results.join("\n"); | ||
return output; | ||
} | ||
|
||
} | ||
|
||
export default ExtractHashes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* ExtractHashes tests. | ||
* | ||
* @author mshwed [m@ttshwed.com] | ||
* @copyright Crown Copyright 2024 | ||
* @license Apache-2.0 | ||
*/ | ||
import TestRegister from "../../lib/TestRegister.mjs"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "Extract MD5 hash", | ||
input: "The quick brown fox jumps over the lazy dog\n\nMD5: 9e107d9d372bb6826bd81d3542a419d6", | ||
expectedOutput: "9e107d9d372bb6826bd81d3542a419d6", | ||
recipeConfig: [ | ||
{ | ||
"op": "Extract hashes", | ||
"args": [32, false, false] | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "Extract SHA1 hash", | ||
input: "The quick brown fox jumps over the lazy dog\n\nSHA1: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", | ||
expectedOutput: "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", | ||
recipeConfig: [ | ||
{ | ||
"op": "Extract hashes", | ||
"args": [40, false, false] | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "Extract SHA256 hash", | ||
input: "The quick brown fox jumps over the lazy dog\n\nSHA256: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", | ||
expectedOutput: "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", | ||
recipeConfig: [ | ||
{ | ||
"op": "Extract hashes", | ||
"args": [64, false, false] | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "Extract SHA512 hash", | ||
input: "The quick brown fox jumps over the lazy dog\n\nSHA512: 07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6", | ||
expectedOutput: "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6", | ||
recipeConfig: [ | ||
{ | ||
"op": "Extract hashes", | ||
"args": [128, false, false] | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "Extract all hashes", | ||
input: "The quick brown fox jumps over the lazy dog\n\nMD5: 9e107d9d372bb6826bd81d3542a419d6\nSHA1: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12\nSHA256: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", | ||
expectedOutput: "9e107d9d372bb6826bd81d3542a419d6\n2fd4e1c67a2d28fced849ee1bb76e7391b93eb12\nd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", | ||
recipeConfig: [ | ||
{ | ||
"op": "Extract hashes", | ||
"args": [0, true, false] | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "Extract hashes with total count", | ||
input: "The quick brown fox jumps over the lazy dog\n\nMD5: 9e107d9d372bb6826bd81d3542a419d6\nSHA1: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12\nSHA256: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", | ||
expectedOutput: "Total Results: 3\n\n9e107d9d372bb6826bd81d3542a419d6\n2fd4e1c67a2d28fced849ee1bb76e7391b93eb12\nd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", | ||
recipeConfig: [ | ||
{ | ||
"op": "Extract hashes", | ||
"args": [0, true, true] | ||
}, | ||
], | ||
} | ||
]); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.