Skip to content

Commit

Permalink
hashTest: New module for getting/checking hashes
Browse files Browse the repository at this point in the history
... in the file explorer
  • Loading branch information
ewerybody committed Jun 13, 2022
1 parent 8d6ae2f commit 5516585
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
39 changes: 39 additions & 0 deletions hashTest/a2module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[
{
"author": "eric",
"date": "2022 6 9",
"description": "Get and compare sha256 hashes from seleceted files in Explorer.",
"display_name": "",
"tags": [
"file",
"code",
"wip"
],
"typ": "nfo",
"url": "",
"version": "0.1"
},
{
"disablable": true,
"enabled": true,
"functionCode": "hashTest()",
"functionMode": 0,
"key": [
"Alt+Shift+H"
],
"keyChange": true,
"label": "Make a hash check with selected file",
"multiple": true,
"name": "hashTest_Hotkey",
"scope": [
"ahk_class CabinetWClass"
],
"scopeChange": true,
"scopeMode": 1,
"typ": "hotkey"
},
{
"file": "hashTest.ahk",
"typ": "include"
}
]
33 changes: 33 additions & 0 deletions hashTest/hashTest.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
; hashTest - hashTest.ahk
; author: eric
; created: 2022 6 9

hashTest() {
selection := explorer_get_selected()
if (selection.Length() != 1) {
msgbox_error("Select 1 File!", "hashTest")
return
}

; get hash value from python hash getter script:
output := string_strip(StrUpper(python_get_output(path_neighbor(A_LineFile, "hashtest.py"), selection[1])))
; check against clipboard
clip_val := string_strip(StrUpper(Clipboard))
if (StrLen(output) != StrLen(clip_val)) {
Clipboard := output
msg := "From the selected file """ path_basename(selection[1]) """`n"
msg .= "I got this sha256 hash and put it to the clipboard:`n`n " output
msgbox_info(msg, "hashTest - got hash")
Return
}
if (output == clip_val) {
msg := "Hash values Match! From the selected file """ path_basename(selection[1]) """ I got this hash and it matches with the one in the clipboard:`n" output
msgbox_info(msg, "hashTest - It's A Match!")
}
else {
msg := "The clipboard contains a string of matching length!`n"
msg .= "But it does NOT match the hash I got from the selected file """ path_basename(selection[1]) """`n"
msg .= output " != " clip_val
msgbox_error(msg, "hashTest - Mismatch!")
}
}
40 changes: 40 additions & 0 deletions hashTest/hashtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
import hashlib


def main():
try:
file_path = sys.argv[1]
except IndexError as error:
raise RuntimeError('I need a file path to get the hash from!') from error
try:
hasher_name = sys.argv[2]
except IndexError:
hasher_name = 'sha256'

try:
hasher = getattr(hashlib, hasher_name.lower())
except AttributeError:
raise RuntimeError('Could not get hash algorithm "%s"! Available are:\n %s' % ', '.join(hashlib.algorithms_available))

binary = True
block_size = 2**16
mode = 'rb' if binary else 'r'

with open(file_path, mode) as file_object:
buffer_object = file_object.read(block_size)
hasherobj = hasher()
len_buf = len(buffer_object)
while len_buf > 0:
if binary:
hasherobj.update(buffer_object)
else:
hasherobj.update(buffer_object.encode('utf8'))
buffer_object = file_object.read(block_size)
len_buf = len(buffer_object)
print(hasherobj.hexdigest())


if __name__ == '__main__':
# sys.argv.append(r'C:\Users\eric\Downloads\AutoHotkey_1.1.34.03_setup(1).exe')
main()

0 comments on commit 5516585

Please sign in to comment.