Skip to content

Instantly share code, notes, and snippets.

@nickyreinert
Last active February 7, 2023 19:49
Show Gist options
  • Save nickyreinert/00d631fe9a90108924b1df6e911c8cd5 to your computer and use it in GitHub Desktop.
Save nickyreinert/00d631fe9a90108924b1df6e911c8cd5 to your computer and use it in GitHub Desktop.
Step-by-Step implementation of the SHA-256 algorithm in Python
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"new_compression_constants = compression_constants.copy()\n",
"\n",
"for i, word in enumerate(message_schedule):\n",
"\n",
" print('Pos 0:' + new_compression_constants[0])\n",
" print('Pos 1:' + new_compression_constants[1])\n",
" print('Pos 2:' + new_compression_constants[2])\n",
" print('Pos 3:' + new_compression_constants[3])\n",
" print('Pos 4:' + new_compression_constants[4])\n",
" print('Pos 5:' + new_compression_constants[5])\n",
" print('Pos 6:' + new_compression_constants[6])\n",
" print('Pos 7:' + new_compression_constants[7])\n",
"\n",
" print()\n",
" print('upper_sigma1([Pos 4]) -> word1 = ' + upper_sigma1(new_compression_constants[4]))\n",
" print('Choose from [Pos 4], [Pos 5], [Pos 6] -> word2 = ' + choose(new_compression_constants[4], new_compression_constants[5], new_compression_constants[6]))\n",
" print('[Pos 7] -> word3 = ' + new_compression_constants[7])\n",
" print('result_constants[i] -> word4 = ' + result_constants[i])\n",
" print('message_schedule[i] -> word5 = ' + word)\n",
" print('AND 11111111111111111111111111111111')\n",
"\n",
" term1 = (int(upper_sigma1(new_compression_constants[4]), 2) + \\\n",
" int(choose(new_compression_constants[4], new_compression_constants[5], new_compression_constants[6]), 2) + \\\n",
" int(new_compression_constants[7], 2) + \\\n",
" int(result_constants[i], 2) + \\\n",
" int(word, 2)) \\\n",
" & int('11111111111111111111111111111111', 2)\n",
"\n",
" print('term1 = ' + bin(term1)[2:].zfill(32))\n",
"\n",
" print()\n",
" print('upper_sigma0([Pos 0]) -> word1 = ' + upper_sigma1(new_compression_constants[0]))\n",
" print('Majority from [Pos 0], [Pos 1], [Pos 2] -> word2 = ' + choose(new_compression_constants[4], new_compression_constants[5], new_compression_constants[6]))\n",
" print('AND 11111111111111111111111111111111')\n",
"\n",
" term2 = (int(upper_sigma0(new_compression_constants[0]), 2) + \\\n",
" int(majority(new_compression_constants[0], new_compression_constants[1], new_compression_constants[2]), 2)) & \\\n",
" int('11111111111111111111111111111111', 2)\n",
"\n",
" print('term2 = ' + bin(term2)[2:].zfill(32))\n",
"\n",
" # move elements in list one index down, means: last one will be dropped\n",
" # and first one is empty now\n",
" new_compression_constants.insert(0, 1)\n",
" new_compression_constants.pop()\n",
"\n",
" new_compression_constants[0] = bin(\n",
" (term1 + term2) & int('11111111111111111111111111111111', 2)\n",
" )[2:].zfill(32)\n",
"\n",
" new_compression_constants[4] = bin(\n",
" (int(new_compression_constants[4], 2) + term1) & int('11111111111111111111111111111111', 2)\n",
" )[2:].zfill(32)\n",
"\n",
" print()\n",
" \n",
" print('[Pos 0] = ' + new_compression_constants[0])\n",
" print('[Pos 4] = ' + new_compression_constants[0])\n",
"\n",
" time.sleep(0.25)\n",
"\n",
" clear_output(wait=True)"
]
}
],
"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
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Tardis4Future
Copy link

Tardis4Future commented Aug 5, 2022

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:

Unbenannt

Can you fix it please? Thanks

@nickyreinert
Copy link
Author

nickyreinert commented Feb 7, 2023

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

@Tardis4Future

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