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
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"message = 'hello_world'\n",
"\n",
"from math import floor\n",
"from IPython.display import clear_output\n",
"import time \n",
"\n",
"def rotate(value, rotations, width=32):\n",
" # thx to https://stackoverflow.com/a/59005609/2360229\n",
" if int(rotations) != abs(int(rotations)):\n",
" rotations = width + int(rotations)\n",
" return (int(value) << (width - (rotations%width)) | (int(value) >> (rotations % width))) & ((1 << width) - 1)\n",
" \n",
"def sigma0(word):\n",
" part1 = bin(rotate(int(word, 2), 7, 32))\n",
" part2 = bin(rotate(int(word, 2), 18, 32))\n",
" part3 = bin(int(word, 2) >> 3)\n",
" return bin(int(part1, 2) ^ int(part2, 2) ^ int(part3, 2))[2:].zfill(32)\n",
"\n",
"def sigma1(word):\n",
" part1 = bin(rotate(int(word, 2), 17, 32))\n",
" part2 = bin(rotate(int(word, 2), 19, 32))\n",
" part3 = bin(int(word, 2) >> 10)\n",
" return bin(int(part1, 2) ^ int(part2, 2) ^ int(part3, 2))[2:].zfill(32)\n",
"\n",
"def upper_sigma0(word):\n",
" part1 = bin(rotate(int(word, 2), 2, 32))\n",
" part2 = bin(rotate(int(word, 2), 13, 32))\n",
" part3 = bin(rotate(int(word, 2), 22, 32))\n",
" return bin(int(part1, 2) ^ int(part2, 2) ^ int(part3, 2))[2:].zfill(32)\n",
"\n",
"def upper_sigma1(word):\n",
" part1 = bin(rotate(int(word, 2), 6, 32))\n",
" part2 = bin(rotate(int(word, 2), 11, 32))\n",
" part3 = bin(rotate(int(word, 2), 25, 32))\n",
" return bin(int(part1, 2) ^ int(part2, 2) ^ int(part3, 2))[2:].zfill(32)\n",
"\n",
"def choose(word1, word2, word3):\n",
" bin_word1 = (int(word1, 2))\n",
" bin_word2 = (int(word2, 2))\n",
" bin_word3 = (int(word3, 2))\n",
" return bin((bin_word1 & bin_word2) ^ (~bin_word1 & bin_word3))[2:].zfill(32)\n",
"\n",
"def majority(word1, word2, word3):\n",
" bin_word1 = (int(word1, 2))\n",
" bin_word2 = (int(word2, 2))\n",
" bin_word3 = (int(word3, 2))\n",
" return bin((bin_word1 & bin_word2) ^ (bin_word1 & bin_word3) ^ (bin_word2 & bin_word3))[2:].zfill(32)\n",
"\n",
"first_64_prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311]\n",
"\n",
"result_constants = []\n",
"for prime_number in first_64_prime_numbers:\n",
" # get cube root\n",
" cube_root = prime_number ** (1./3.)\n",
" # get fractional part\n",
" frac_part = cube_root - floor(cube_root)\n",
" # multiply with 2^32 (or shift left by 32 bits)\n",
" product = frac_part * (2**32)\n",
" floored_product = floor(product)\n",
" # floor, ready:\n",
" result_constants.append(bin(floored_product)[2:].zfill(32))\n",
"\n",
"first_8_prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19]\n",
"\n",
"compression_constants = []\n",
"for prime_number in first_8_prime_numbers:\n",
" # get square root\n",
" square_root = prime_number ** (1./2.)\n",
" # get fractional part\n",
" frac_part = square_root - floor(square_root)\n",
" # multiply with 2^32 (or shift left by 32 bits)\n",
" product = frac_part * (2**32)\n",
" floored_product = floor(product)\n",
" compression_constants.append(bin(floored_product)[2:].zfill(32))"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"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.
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.
@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