diff --git a/hashTest/a2module.json b/hashTest/a2module.json new file mode 100644 index 0000000..f2585d4 --- /dev/null +++ b/hashTest/a2module.json @@ -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" + } +] \ No newline at end of file diff --git a/hashTest/hashTest.ahk b/hashTest/hashTest.ahk new file mode 100644 index 0000000..86fa001 --- /dev/null +++ b/hashTest/hashTest.ahk @@ -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!") + } +} diff --git a/hashTest/hashtest.py b/hashTest/hashtest.py new file mode 100644 index 0000000..3d45877 --- /dev/null +++ b/hashTest/hashtest.py @@ -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()