24 ms·
Show HN: SHA-256 Animation
- inersha 6y agoI 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.
- baicunko 6y agoThis is a great idea of showing something that is at first very complex. Could be used in Discreet Mathematics to teach students!
- m4r35n357 6y agoIndiscreet mathematics?
- perl4ever 6y agoSomewhere I had the idea concrete math was a combination of continuous and discrete, but I seem to be mistaken. "When DEK taught Concrete Mathematics at Stanford for the first time, he explained the somewhat strange title by saying that it was his attempt to teach a math course that was hard instead of soft. He announced that, contrary to the expectations of some of his colleagues, he was not going to teach the Theory of Aggregates, nor Stone's Embedding Theorem, nor even the Stone-Cech compactification. (Several students from the civil engineering department got up and quietly left the room.)"
- ImJasonH 6y agoThis is super impressive, thanks for building and sharing it!
- w0utert 6y agoLike it a lot. Not sure how useful it is but just looking at it is mesmerizing, well done!
- lewiscollard 6y agoNice work, and that is a very cool approach to learning new things!
- mabbo 6y ago> I wanted to understand how SHA-256 works, so I... ... taught everyone else how SHA-256 worked... This is an awesome intro. And now I also want to know more about the things you mentioned wanted to learn more about.
- tialaramex 6y agoWithout any doubt I do not properly understand anything I can't explain to someone else. An exercise I go through constantly is figuring out how to explain a thing I think I know to a curious person with no relevant training. Often in the process I discover I need to go do more research or actually test things because I did not understand them as well as I'd maybe thought. Mostly this is just an exercise. But every so often I actually get to use this in anger. A non-technical friend who works in a Computer Science department asked me on Facebook to explain a joke she'd seen which involved localhost addressing, and I was very pleased to be able to provide a concise explanation using analogies that I know hold up to scrutiny. Obviously a joke isn't very funny if you need it explained, and I can't fix that, but I can avoid the discomfort of her not understanding a joke other people are laughing at in her place of work.
- bradjohnson 6y agoI realize it's not what you're saying, but I don't like the idea that if you can't teach something to others, you don't understand it yourself. Teaching is a skill and it's something that I am aware that I struggle with. I can explain something in great detail to a captive audience and understand it myself personally, but teaching is about getting others to engage with the ideas you're presenting and identifying and elaborating on parts that they don't understand. Given the content of knowledge sharing sessions that I sit through and the convoluted nature of some of them, I wish that people recognized that presenting information is not all that is required to teach. You can understand something perfectly, but teach it horribly.
- stevofolife 6y agoIf you can't teach it, then you don't understand something perfectly. Perfect is a word par excellence. Teaching is indeed a skill but don't confuse it with presenting. A wise guy once said, "if you can’t explain something in simple terms, you don’t understand it". I'm sure OP stands by it and that itself is very admirable.
- Zhyl 6y ago1) 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'.
- inersha 6y ago1) Thank you. 2) It's certainly a combination I never anticipated. 3) I look forward to the book.
- mystickphoenix 6y agoYou could call it CIDR Punk if you want to be a bit more on-the-nose ;)
- hinkley 6y agoI feel like this is some sort of James Watt reference but I can't be sure.
- stan_rogers 6y agoThere's nothing particularly special about the constants. They needed a set of n constants to work with and the "cube root of first n primes, truncated" scheme is a "nothing up my sleeve"[0] construction. If they'd used magic numbers with no obvious generation scheme, you'd be left wondering if that was done as a way to put a back door in place. [0] https://en.wikipedia.org/wiki/Nothing-up-my-sleeve_number https://en.wikipedia.org/wiki/Nothing-up-my-sleeve_number
- jmiserez 6y agoThat's also the explanation the author gives in chapter 4. I'm more interested in how they came up with the parameters for the sigma functions. I'm sure it's described somewhere.
- imoverclocked 6y agoCongratulations on the worlds slowest and useful SHA-256 implementation! :)
- nayuki 6y agoThere is a far slower implementation: http://www.righto.com/2014/09/mining-bitcoin-with-pencil-and-paper.html http://www.righto.com/2014/09/mining-bitcoin-with-pencil-and...
- jonathanzufi 6y agoThis is fantastic - and your YouTube video is also impressive. Thank you for doing this.
- inersha 6y agoNo problem at all, thank you.
- crousto 6y agoVery nice! This also has potential for landing in many movies where a hacker at work is involved.
- maallooc 6y agoThis is so interesting to watch. Can you do it for other hash or encryption methods?
- inersha 6y agoI have thought about it. Any particularly interesting ones?
- dylkil 6y agoecdsa would be good
- rottyguy 6y agomd5/sha1 is useful because they're still used in UUID (rfc 4122) generation. obv not from a crypto standpoint but more from a unique (non-exploited) approach.
- tromp 6y agoInteresting candidates are blake2b, sha-3/keccak, and the much simpler siphash-2-4 that's used in many hashtable implementations.
- jagged-chisel 6y agoI 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.
- harrigan 6y agohttps://www.youtube.com/watch?v=gP4PqVGudtg https://www.youtube.com/watch?v=gP4PqVGudtg is a nice animation of AES. Of course, the inputs are fixed unlike the above.
- nayuki 6y agoI 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.edu/~straubin/cs381-05/blockciphers/rijndael_ingles2004.swf https://web.archive.org/web/20051124061027/http://www.cs.bc....
- iBotPeaches 6y agoThis is amazing! Really fun to watch the example in the readme and just shows how much happens behind the scenes nearly instantly.
- 1_player 6y agoHere'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!
- inersha 6y agoMy pleasure, thank you.
- newscracker 6y agoWow, this was just what I asked for in a comment before seeing this comment. Thanks.
- dest 6y agoMake a screensaver out of this!
- ape4 6y agoCould be a in a movie
- jonplackett 6y agoI 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.
- ghoshbishakh 6y agoYou made my day
- tamiral 6y agoi just felt like i was in the matrix for a little bit and that was ok for me :)
- jmiserez 6y agoDid you see the README.md? It explains each of the steps.
- rhacker 6y agoThanks for pointing that out - I kinda missed it until seeing your post. I went through each step - it's still a bit convoluted, but definitely helps me appreciate hashing functions. It's probably very easy for a poorly coded hash function to accidentally wipe important data or accidentally and a bunch of data with zeroes, so it's pretty cool to see a lot of ROT and XOR usage which does more interesting things with the original data. Had no idea about the prime roots and multiplication, that's pretty clever too.
- folli 6y agoYou 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
- dnadler 6y agoThis 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 6y agoThank you for being so kind, it means a lot.
- barbs 6y agoI feel like there are lots of mathematical / programming concepts that could be explained through animation. Anyone got any good examples?
- inersha 6y agoI follow Tamás Görbe on Twitter and he regularly posts cool mathematical animations: https://twitter.com/TamasGorbe/status/1238448040521932801 https://twitter.com/TamasGorbe/status/1238448040521932801
- jmiserez 6y ago3blue1brown 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 6y agoThank 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.
- aasasd 6y agoThere are plenty of animations/videos about algorithms, all the way up to Hungarian folk dancing: https://youtube.com/user/AlgoRythmics/videos https://youtube.com/user/AlgoRythmics/videos
- alex7o 6y agoThis was very useful while learning stats: https://m.youtube.com/channel/UCtYLUTtgS3k1Fg4y5tAhLbw https://m.youtube.com/channel/UCtYLUTtgS3k1Fg4y5tAhLbw
- borplk 6y agoI think this might end up being used in a hacking scene in a movie ... with bright green text of course.
- alharith 6y agoIIRC, hackertyper has been used numerous times.
- jrvidal 6y agoShameless plug: https://jrvidal.github.io/aes-demo/ https://jrvidal.github.io/aes-demo/ I like this one better though.
- newscracker 6y agoLooks 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.
- kdrag0n 6y agoMinor typo: "Rinjdael" -> Rijndael
- gtsnexp 6y agoGreat work! Any idea where we may find an equivalent in Python?
- nayuki 6y agoMaybe this: https://www.nayuki.io/res/cryptographic-primitives-in-plain-python/sha256hash.py https://www.nayuki.io/res/cryptographic-primitives-in-plain-... ; https://www.nayuki.io/page/cryptographic-primitives-in-plain-python https://www.nayuki.io/page/cryptographic-primitives-in-plain...
- gtsnexp 6y agoawesome, thanks
- n_t 6y agoGreat animation! And I still have no idea what happened there :D
- stanlyjh 6y agoThat's some brilliant work. (Y)
- billpg 6y agoI 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.
- newscracker 6y agoThis 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)
- iamgopal 6y agoThere are microprocessor out there that does most of this in single instruction, :O
- Exuma 6y agoI forsee this being in a blockbuster movie featuring a "hacker"
- Rickvst 6y agoI 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 6y agoHehehe, yea, no, there's a good five to ten steps necessary to grok all that.
- warranty 6y agoI subscribed to your YouTube channel. https://youtu.be/f9EbD6iY9zI https://youtu.be/f9EbD6iY9zI
- kleer001 6y agoI get it! Fantastic work. I hope you get lots of requests to include it in teaching materials.
- elwell 6y agoPsh! I never trust a program with my hashing; pen & paper is your friend here, much more secure.
- sizzzzlerz 6y agoLove 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 6y agoIf 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 6y agoThanks! 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!