Last active
February 7, 2023 19:49
-
-
Save nickyreinert/00d631fe9a90108924b1df6e911c8cd5 to your computer and use it in GitHub Desktop.
Step-by-Step implementation of the SHA-256 algorithm in Python
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": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"word_length = 32\n", | |
"\n", | |
"message_schedule = [str(full_message[i:i+word_length]) for i in range(0, len(full_message), word_length)]\n", | |
"\n", | |
"for i in range(16, 64):\n", | |
"\n", | |
" time.sleep(2)\n", | |
"\n", | |
" clear_output(wait=True)\n", | |
"\n", | |
" word1 = message_schedule[i - 2]\n", | |
" word2 = message_schedule[i - 7]\n", | |
" word3 = message_schedule[i - 15]\n", | |
" word4 = message_schedule[i - 16]\n", | |
"\n", | |
" print('Loop #%i' % (i - 16))\n", | |
" print('Pos ' + str(i - 16).zfill(2) + ': ' + message_schedule[i - 16] + ' > word4\\t = ' + word4)\n", | |
" print('Pos ' + str(i - 15).zfill(2) + ': ' + message_schedule[i - 15] + ' > sigma0(word3) = ' + bin(int(sigma0(word3), 2))[2:].zfill(32))\n", | |
" print('Pos ' + str(i - 14).zfill(2) + ': ' + message_schedule[i - 14])\n", | |
" print('Pos ' + str(i - 13).zfill(2) + ': ' + message_schedule[i - 13])\n", | |
" print('Pos ' + str(i - 12).zfill(2) + ': ' + message_schedule[i - 12])\n", | |
" print('Pos ' + str(i - 11).zfill(2) + ': ' + message_schedule[i - 11])\n", | |
" print('Pos ' + str(i - 10).zfill(2) + ': ' + message_schedule[i - 10])\n", | |
" print('Pos ' + str(i - 9).zfill(2) + ': ' + message_schedule[i - 9])\n", | |
" print('Pos ' + str(i - 8).zfill(2) + ': ' + message_schedule[i - 8])\n", | |
" print('Pos ' + str(i - 7).zfill(2) + ': ' + message_schedule[i - 7] + ' > word2\\t = ' + word2)\n", | |
" print('Pos ' + str(i - 6).zfill(2) + ': ' + message_schedule[i - 6])\n", | |
" print('Pos ' + str(i - 5).zfill(2) + ': ' + message_schedule[i - 5])\n", | |
" print('Pos ' + str(i - 4).zfill(2) + ': ' + message_schedule[i - 4])\n", | |
" print('Pos ' + str(i - 3).zfill(2) + ': ' + message_schedule[i - 3])\n", | |
" print('Pos ' + str(i - 2).zfill(2) + ': ' + message_schedule[i - 2] + ' > sigma1(word1) = '+ bin(int(sigma1(word1), 2))[2:].zfill(32))\n", | |
" print('Pos ' + str(i - 1).zfill(2) + ': ' + message_schedule[i - 1])\n", | |
"\n", | |
" new_word = ( \\\n", | |
" int(sigma1(word1), 2) + \\\n", | |
" int(word2, 2) + \\\n", | |
" int(sigma0(word3), 2) + \\\n", | |
" int(word4, 2)) & \\\n", | |
" int('11111111111111111111111111111111', 2)\n", | |
"\n", | |
" print()\n", | |
" print('\\t\\t\\t\\t\\t\\t AND 11111111111111111111111111111111')\n", | |
" print()\n", | |
" print('\\t\\t\\t\\t\\t\\tnew word = ' + bin(new_word)[2:].zfill(32))\n", | |
"\n", | |
" message_schedule.append(bin(new_word)[2:].zfill(32))\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.9.13 (main, May 24 2022, 21:28:31) \n[Clang 13.1.6 (clang-1316.0.21.2)]" | |
}, | |
"orig_nbformat": 4, | |
"vscode": { | |
"interpreter": { | |
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Hello, i got message that this notebook is invalid. I can not open it in jupyter. Thanks for your help!
Edit: now I put the lines manually in jupyter but after loop #47 i got following message:
Can you fix it please? Thanks
You are right, I fixed it. I pasted the code instead of the raw text content of the Jupyter file.
Regarding your error: It works when I run it locally. Maybe it helps to run everything in one Jupyter. I splitted it to make it easier to implement in the Medium article. I uploaded the merged version here: https://gist.github.com/nickyreinert/5621d17d63efbbd68584246d20fe4d2f
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, i got message that this notebook is invalid. I can not open it in jupyter.
Thanks for your help!
Edit: now I put the lines manually in jupyter but after loop #47 i got following message:
Can you fix it please? Thanks