Last active
April 6, 2025 22:13
-
-
Save wesleyit/a4f18fb356501047e4124adab79b904a to your computer and use it in GitHub Desktop.
My experimental hash algorithm (for learning purposes)
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"id": "5d0af8bb-106f-4081-b5c3-51ab98cb4c45", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The message is: O cavalo morto eh um animal sem vida.\n", | |
"The message bytes are: b'bRRQXlBGQFsaDBRQXlBGQFsaYxRQXlBGQFsaIBRQXlBGQFsaIhRQXlBGQFsaIgNQXlBGQFsaIhRQXlBGQFsaIhlQXlBGQFsaIhpQXlBGQFsaIhofXlBGQFsaIhpSXlBGQFsaIhpQXlBGQFsaIhpNXlBGQFsaIhpNWFBGQFsaIhpNQ1BGQFsaIhpNDFBGQFsaIhpNSVBGQFsaIhpNSV1GQFsaIhpNSRVGQFsaIhpNSUBGQFsaIhpNSVhGQFsaIhpNSVgLQFsaIhpNSVhKQFsaIhpNSVhFQFsaIhpNSVhCQFsaIhpNSVhCRFsaIhpNSVhCSFsaIhpNSVhCRVsaIhpNSQ=='\n" | |
] | |
} | |
], | |
"source": [ | |
"import base64\n", | |
"\n", | |
"\n", | |
"def get_whash(message_bytes: list) -> list:\n", | |
" MSG_SIZE = len(message_bytes)\n", | |
" pairs = []\n", | |
" for i in range(0, MSG_SIZE, 2):\n", | |
" b1 = message_bytes[i]\n", | |
" b2 = message_bytes[i + 1] if i < MSG_SIZE - 1 else 0\n", | |
" pairs.append(b2 ^ b1)\n", | |
" return pairs\n", | |
"\n", | |
"\n", | |
"def whash(message: str):\n", | |
" HASH_SIZE=256\n", | |
" counter = 1\n", | |
" initial_message = message\n", | |
" while len(message) < 4 * HASH_SIZE:\n", | |
" message = message + initial_message[counter:] + initial_message[:counter]\n", | |
" counter+=1\n", | |
" message = message[:HASH_SIZE * 4]\n", | |
" message_bytes = list(message.encode('UTF-8'))\n", | |
" while len(message_bytes) > HASH_SIZE:\n", | |
" message_bytes = get_whash(message_bytes)\n", | |
" return message_bytes\n", | |
"\n", | |
"\n", | |
"message = 'O cavalo morto eh um animal sem vida.'\n", | |
"message_hash = whash(message)\n", | |
"\n", | |
"\n", | |
"print('The message is:', message)\n", | |
"print('The message bytes are:', base64.b64encode(bytes(message_hash)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"id": "b06443fc-ac72-47aa-8917-d17828803a6f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"848516" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import lipsum\n", | |
"text = lipsum.generate_paragraphs(1000)\n", | |
"len(text)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"id": "be9d3dde-ff63-46dc-b632-745eb3b2f3c3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The message is: Restat locus huic disputationi vel maxime necessarius de amicitia, quam, si voluptas summum sit bonu\n", | |
"The message bytes are: b'MFkKVE4CE1FEHU4GD1JFFxxVGgAWS1VOFgAbEUgMZkgeD0V8EVcFXE4RBUoPCFNDUAwSQhZLEwBDT0BvEF8XBAZbCxEEXxYISA5EXBxZWFJUVhxLUEROXBZvABoTD0VBEU5MUAJKEEgOHBFKDVhUTglGE1BNbxIZUAIAVV5QCEoKVg4aC1F4D0cUQh4RNRsAR3gPflpDQRFVHUIVHFkFTQ4AQ0gRChxJFhZOClwIShcXD3FYQlhmDw0YQVpTHAxCA0QHT0gAQnoMXRpGTAdFBAZIQA5cDhQWDVtXVRBORUBFXxMQU2YYQkZEXEFYCBseCFIUTQYOCExaHFYLG0YORg=='\n" | |
] | |
} | |
], | |
"source": [ | |
"message = text\n", | |
"message_hash = whash(message)\n", | |
"\n", | |
"\n", | |
"print('The message is:', message[:100])\n", | |
"print('The message bytes are:', base64.b64encode(bytes(message_hash)))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.15" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment