
Show HN: SHA-256 Animation - inersha
https://github.com/in3rsha/sha256-animation
======
inersha
I wanted to understand how SHA-256 works, so I made a terminal animation that
shows the bitwise operations at each step. I wrote a text guide in the
README.md to explain what's going on. I think my technical terminology is
okay.

I'm new to hash functions though, so I don't yet know why SHA-256 has been
designed in the way it has (e.g. why exact numbers were chosen in the bitwise
rotations).

As far as I understand, the Sigma functions promote diffusion of bits to help
prevent collisions, and Choice/Majority/Addition help to make it a one-way
function, but I'm not entirely sure. I'd be interested in learning more about
the design if anyone has experience in this field.

~~~
Zhyl
1) I loved this video.

2) I did have to take a sip of tea and think about my life when I realised I
was watching a video by a Welshman about mining.

3) Although having said this, 'Welsh Bitcoin Miner' is going to fit seamlessly
into my West Country themed cyberpunk adventure 'Cider Punk'.

~~~
mystickphoenix
You could call it CIDR Punk if you want to be a bit more on-the-nose ;)

------
dnadler
This is phenomenal. The animations at the top were way too quick for me to
understand, but they pulled me in. The step-by-step explanations below
(alongside the relevant parts of the animation) were very clear and
interesting.

I had the big picture from the top of the page, and each section gave me a
little more insight into what was going on. Great use of animation, and also
very clearly written!

I also like the testimonials.

~~~
inersha
Thank you for being so kind, it means a lot.

------
jonplackett
I once heard about a meeting where someone did a presentation for the best
part of an hour to a room full of Japanese people. They all nodded along,
smiling as the presentation progressed. It wasn’t until the end that the
presenter asked them what they thought. They all just kept smiling, but no
answer. Someone at the front turned and talked to the room (in Japanese) then
turned to the presented and said “they do not speak any English, but they are
sure from this presentation that you are very, very intelligent”.

I feel like the Japanese people. I don’t understand what you’re doing here but
I am convinced it is very clever.

~~~
folli
You need to watch his video, very cool, it really helps to understand how this
works:
[https://www.youtube.com/watch?v=f9EbD6iY9zI](https://www.youtube.com/watch?v=f9EbD6iY9zI)

~~~
chias
There's a few things in there that are factually incorrect -- in particular,
the false notion that "every input has a unique output" can be quite dangerous
in some cryptographic settings.

That said, the purpose of this talk is about the mechanics of the function,
and not its properties or how to use it safely. So don't let that detract from
what is, really, an awesome presentation.

~~~
tridentboy
I'm sorry, could you please elaborate? I was always under the assumption that
hash functions have to be deterministic, and thus, that "every input has a
unique output" was a correct statement.

AFAIK the contrary is invalid, so that "not every output is the result of one
and only one input".

~~~
chias
A function being deterministic means that any input will have a single output.
But it is not unique for any hash function, SHA-256 included. The definition
of a hash function is any function which takes an arbitrary length input and
outputs an n-bit output for some fixed value of n. By virtue of the fact that
you have infinite inputs and finite outputs, the outputs cannot be unique.

Generally when people make this claim, what they're actually referring to is
what's called Collision Resistance (CR) and/or Weak Collision Resistance
(WCR), which instead make claims on difficulty of finding such collisions (of
which infinitely many exist).

WCR, necessary for almost any cryptographic use, states that for any given
input it should be difficult to find a different input which hashes to the
same value. CR, generally desirable for cryptographic hash functions, states
that it should be difficult to find two different inputs such that their
hashes are equal. CR implies WCR, but WCR does not imply CR -- for example,
SHA-256 (currently) exhibits CR but SHA-1 only exhibits WCR.

------
crousto
Very nice!

This also has potential for landing in many movies where a hacker at work is
involved.

~~~
aasasd
Indeed, I'm _hoping_ that it ends up in movies so they finally have actually
relevant visuals. Someone send this to Hollywood.

------
1_player
Here's the author doing an in-depth explanation of how SHA-256 works using
this code:
[https://www.youtube.com/watch?v=f9EbD6iY9zI](https://www.youtube.com/watch?v=f9EbD6iY9zI)

I'm halfway through, but looks very well done, thanks!

~~~
newscracker
Wow, this was just what I asked for in a comment before seeing this comment.
Thanks.

------
newscracker
This looks great, though it requires quite sometime to go through it (and
figure out what could possibly be understood by someone with knowledge of
programming and bit wise operators, and what can just be skipped because it’s
something only cryptographers can understand).

If someone were to make a slower video explaining it with all the sections
from this dissection, that’d be even more awesome.

Edit: Seems like the author created a video for this —
[https://www.youtube.com/watch?v=f9EbD6iY9zI](https://www.youtube.com/watch?v=f9EbD6iY9zI)
(thanks to 1_player’s comment at
[https://news.ycombinator.com/item?id=23165906](https://news.ycombinator.com/item?id=23165906))

On a tangent, here’s an animation (by someone else) explaining AES encryption
—
[https://m.youtube.com/watch?v=gP4PqVGudtg](https://m.youtube.com/watch?v=gP4PqVGudtg)
(thanks to harrigan’s comment at
[https://news.ycombinator.com/item?id=23165821](https://news.ycombinator.com/item?id=23165821))

------
jagged-chisel
I would love to build something like this for encryption algorithms that
twiddle bits (DES and friends.) Even for the math-based algorithms, but I have
a had time imagining how to make those interesting.

~~~
nayuki
I have Excel spreadsheets showing every bit of the calculation of AES and DES:
[https://www.nayuki.io/page/aes-cipher-internals-in-
excel](https://www.nayuki.io/page/aes-cipher-internals-in-excel) ;
[https://www.nayuki.io/page/des-cipher-internals-in-
excel](https://www.nayuki.io/page/des-cipher-internals-in-excel)

Also, some people have been sharing a video explanation of AES/Rijndael. It is
actually rendered from someone's Shockwave Flash movie.
[https://web.archive.org/web/20051124061027/http://www.cs.bc....](https://web.archive.org/web/20051124061027/http://www.cs.bc.edu/~straubin/cs381-05/blockciphers/rijndael_ingles2004.swf)

------
sizzzzlerz
Love the animations but they go by too fast. It would be nice if you could add
a single step capability so the user could follow along at human pace.

Also, I wanted to see how you updated the bit strings in place and was
surprised not to find anything special. It looks like you use nothing but puts
statements, which, as I understand it, simply add a carriage return at the end
of a string but how then does the cursor move back up to the first line? Can
some explain the technique being used here?

~~~
inersha
If you add the third optional argument of "enter" when running the sha256.rb
script you can use your keyboard to step through. I haven't set this up for
the individual animations though.

ruby sha256.rb sizzzzlerz enter

I didn't do anything special for the terminal animation. I just work out the
current state of the hash function at each step, clear the terminal, and print
the entire state back to the screen. I know it looks like I'm directly
manipulating each individual bit in your terminal, but really I'm just
redrawing your screen.

I'd like to do something more practical in future, but for this I just did
what I needed to do to get an animation working. Still, every bit you are
seeing is correct.

~~~
sizzzzlerz
Thanks! I detected a few flashes in some lines that weren't active but I
didn't believe you could clear and reprint the entire screen that fast. Very
cool!

------
billpg
I was surprised to see addition in there.

Hash functions use bit-wise operators. This is a world where "integers" are
simply convenient fixed-sized blobs of bits and all bits are equally
significant.

Addition is an arithmetic operation. Bits have significance in this world. The
state of the input bits have more "influence" over the left hand bits of the
result than the right hand bits.

Look at abcd+efgh=ijkl (Ignoring any overflow'd bits per SHA-256.)

To find the value of bit l, you only need to know bits d and h. You need to
know all eight input bits to know the value of bit i.

------
maallooc
This is so interesting to watch. Can you do it for other hash or encryption
methods?

~~~
inersha
I have thought about it. Any particularly interesting ones?

~~~
tromp
Interesting candidates are blake2b, sha-3/keccak, and the much simpler
siphash-2-4 that's used in many hashtable implementations.

------
borplk
I think this might end up being used in a hacking scene in a movie ... with
bright green text of course.

~~~
alharith
IIRC, hackertyper has been used numerous times.

------
jrvidal
Shameless plug: [https://jrvidal.github.io/aes-
demo/](https://jrvidal.github.io/aes-demo/)

I like this one better though.

~~~
newscracker
Looks nice, but an explanation of each step would be more helpful to
understand it (along with a coverage of the overall algorithm). The animation
was a little too quick for me to follow along.

------
barbs
I feel like there are lots of mathematical / programming concepts that could
be explained through animation. Anyone got any good examples?

~~~
jmiserez
3blue1brown is great, e.g. his Fourier transform animation is super intuitive:
[https://youtube.com/watch?v=spUNpyF58BY](https://youtube.com/watch?v=spUNpyF58BY)

He has opensourced his animation engine "manim" used in his videos:
[https://github.com/3b1b/manim](https://github.com/3b1b/manim)

~~~
Sohcahtoa82
Thank you for showing me that Fourier transform video. I've never understood
how it worked because usually they just show the integral and call it good.

------
Rickvst
I remember that in my first year of CS, the professor asked us to implement
this algorithm. We had just learned our first computer language(C by the way).
Result: nobody was able to do it, and everybody got a 10 after the professor
realized it.

~~~
kleer001
Hehehe, yea, no, there's a good five to ten steps necessary to grok all that.

------
n_t
Great animation! And I still have no idea what happened there :D

------
iBotPeaches
This is amazing! Really fun to watch the example in the readme and just shows
how much happens behind the scenes nearly instantly.

------
elwell
Psh! I never trust a program with my hashing; pen & paper is your friend here,
much more secure.

------
iamgopal
There are microprocessor out there that does most of this in single
instruction, :O

------
dest
Make a screensaver out of this!

------
kleer001
I get it! Fantastic work.

I hope you get lots of requests to include it in teaching materials.

------
Exuma
I forsee this being in a blockbuster movie featuring a "hacker"

------
ape4
Could be a in a movie

------
stanlyjh
That's some brilliant work. (Y)

------
warranty
I subscribed to your YouTube channel.

[https://youtu.be/f9EbD6iY9zI](https://youtu.be/f9EbD6iY9zI)

------
gtsnexp
Great work! Any idea where we may find an equivalent in Python?

~~~
nayuki
Maybe this: [https://www.nayuki.io/res/cryptographic-primitives-in-
plain-...](https://www.nayuki.io/res/cryptographic-primitives-in-plain-
python/sha256hash.py) ; [https://www.nayuki.io/page/cryptographic-primitives-
in-plain...](https://www.nayuki.io/page/cryptographic-primitives-in-plain-
python)

~~~
gtsnexp
awesome, thanks

