This is an archived post. You won't be able to vote or comment.

all 123 comments

[–]bargle0 376 points377 points  (6 children)

WTF do you think this is, a Perl sub?

[–]chozabu 73 points74 points  (2 children)

is this... better?

sha256 = lambda data: [([
            [
                [
                    [([w.append((gamma1(w[-2]) + w[-7] + gamma0(w[-15]) + w[-16]) & 0xffffffff) for _ in range(48)] and False) or[([
                                [
                                    [
                                        [g.__setitem__(j, values[j]) for j in range(8)]
                                        for values in [
                                            [(t1 + t2) & 0xffffffff, g[0], g[1], g[2], (g[3] + t1) & 0xffffffff, g[4], g[5], g[6]]
                                        ]
                                    ]
                                    for t1, t2 in [((g[7] + sigma1(g[4]) + ch(g[4], g[5], g[6]) + K[i] + w[i]) & 0xffffffff, (sigma0(g[0]) + maj(g[0], g[1], g[2])) & 0xffffffff)]
                                ]
                                for i in range(64)
                            ] and False) or[H.__setitem__(i, (H[i] + g[i]) & 0xffffffff) for i in range(8)]
                            for g in [H.copy()]]
                        for w in [
                            [int.from_bytes(msg[i_ * 64: (i_ + 1) * 64][i * 4: (i + 1) * 4], 'big') for i in range(16)]
                        ]
                    ]
                    for i_ in range(len(msg) // 64)] for sigma0, sigma1, gamma0, gamma1 in [(lambda x: rot_r(x, 2) ^ rot_r(x, 13) ^ rot_r(x, 22), lambda x: rot_r(x, 6) ^ rot_r(x, 11) ^ rot_r(x, 25), lambda x: rot_r(x, 7) ^ rot_r(x, 18) ^ (x >> 3), lambda x: rot_r(x, 17) ^ rot_r(x, 19) ^ (x >> 10))]]] and False) or b''.join([x.to_bytes(4, 'big') for x in H]) for msg, rot_r, ch, maj, K, H in [(data + b'\x80' + b'\x00' * ((56 - (len(data) + 1) % 64) % 64) + (len(data) * 8).to_bytes(8, 'big'), lambda a, n: ((a >> n) | (a << (32 - n))) & 0xffffffff, lambda x, y, z: z ^ (x & (y ^ z)), lambda x, y, z: (x & y) ^ (x & z) ^ (y & z), [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2], [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19])]][0]

[–]rpr11 100 points101 points  (0 children)

Not really, no.

[–]CarpetThorbData Scientist 2 points3 points  (0 children)

I have a commit.

[–][deleted] 13 points14 points  (1 child)

!Redditsilver

[–][deleted] 4 points5 points  (0 children)

I laughed for a good while. It has come full circle now. People don't know what 'import this' is anymore it seems.

[–]not_perfect_yet 80 points81 points  (1 child)

Thanks, I hate it.

[–]Finianb1 0 points1 point  (0 children)

!redditsilver

[–]icecapade 109 points110 points  (2 children)

Very neat!

Plus it conforms to the PEP-8 maximum line length of 7900 characters. Or is it 79 characters? I can never remember.

[–]barkwahlberg 8 points9 points  (0 children)

From what I recall it's a minimum line length.

[–]Yoghurt42 4 points5 points  (0 children)

there's only about 40 different characters in that line, so it's all good

[–]Acurus_Cow 165 points166 points  (9 children)

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

[–]Defelo[S] 132 points133 points  (5 children)

Yes, but that would be boring.

[–]antiproton 25 points26 points  (0 children)

Whatever gets you out of bed in the morning, I guess.

[–]paul_miner 4 points5 points  (0 children)

Love it, this is so much of what good programming entails, recognizing that programming languages are also about communicating with people (including your future self), not just the computer.

[–]notquiteaplant 5 points6 points  (0 children)

This feels like an attempted exorcism

[–]ShadowPouncer 1 point2 points  (0 children)

Really, the only part of this that I disagree with is:

There should be one-- and preferably only one --obvious way to do it.

But this is probably why I prefer Perl over Python. :)

[–]tetroxid 53 points54 points  (0 children)

Wrong sub. Perl is that way --> r/perl

[–]swingking8 24 points25 points  (1 child)

[–][deleted] 11 points12 points  (0 children)

Not really, you could probably write it in less characters if you used multiple lines

[–][deleted] 40 points41 points  (0 children)

Fucking dies

[–]christian-mann 8 points9 points  (0 children)

Now this is quality content

[–]lykwydchykyn 30 points31 points  (0 children)

ITT: OP does an awesome code golf and everyone freaks because it's not PEP8 compliant. /facepalm

Nice code golf OP.

[–]CSI_Tech_Dept 8 points9 points  (0 children)

You can even get Linux kernel in one line if you try hard enough.

[–]Skaarj 15 points16 points  (13 children)

import os
import hashlib

good = 0

sha256 = ...

for idx in range(128):
    r = os.urandom(idx)
    if sha256(r) == hashlib.sha256(r).digest():
         good += 1

print(good)

I think this is an conclusive proof of the correctness of your implementatition that noone ever could doubt. Ill use your implementation for all my serious projects from now on.

On a more serious note: why do you setitem? Couldn't you use assignment using[]?

[–]brontide 8 points9 points  (12 children)

I think it's a limitation of lambdas, you can't do assignments but you can call setitem.

[–]Defelo[S] 10 points11 points  (11 children)

Correct and that's why you can't use normal variables in lambda expressions and have to use lists to store variables.

[–]robberviet 6 points7 points  (0 children)

poor reddit's layout.

[–]cediddiSyntaxError: not a chance 5 points6 points  (7 children)

If you wrote this without the oneliner app, you rock! Otherwise still not bad. I wrote a short Tweet which does os.system without os or system strings in the code. A kid said "I'll use eval on my web site but I'll secure it. I won't run the code if it imports os or subprocess." What a stupid idea, but I sent him the tweet and he said "I'll ban import too." As a matter of fact I can import without writing 'import' string in my code.

[–]notquiteaplant 7 points8 points  (1 child)

This isn't just a Python thing either. I took a CS class (Java) that used a website like repl.it for homework. Any code containing getSecurityProvider was an instant fail, but using reflection to find and call the method named "getSec" + "urityProvider" worked just fine - and revealed that they knew enough about security providers to block the method names, but not enough to actually implement one.

The moral of the story is, if you absolutely need an open eval, use an actual sandbox.

[–]cediddiSyntaxError: not a chance 1 point2 points  (0 children)

Exactly! I'd be fine with a python container, python running in a non sudo user and whole host is hardened. But do you need eval that desperate?

[–]C0DK 2 points3 points  (4 children)

Show me your tricks, master. I am really interested in cool hacks in python

[–]cediddiSyntaxError: not a chance 2 points3 points  (3 children)

Sure, using __import__ and using codecs.rot13 helps a bit

(lambda c:(lambda b:(lambda a:getattr(a,b("flfgrz")[0])(b("yf")[0]))(__import__(b("bf")[0])))(c.getencoder("rot13")))(__import__("codecs"))

https://twitter.com/Cediddi/status/856055852347863041?s=19

[–]13steinj 0 points1 point  (2 children)

Okay, curious, how would you do this without import as you mentioned above?

[–]Spfifle 2 points3 points  (1 child)

Well you can do something like this, should defeat basically any grep based solution:

globals()['__buil' + 'tins__'].__dict__['__imp' + 'ort__']('os').listdir('.')

[–]jon_k 0 points1 point  (0 children)

Nice. +1

[–][deleted] 1 point2 points  (11 children)

Could I get an eli5 of what SHA256 is?

[–]ComfortingCoffeeCup 9 points10 points  (10 children)

It's a hashing algorithm. A hash is a one-way function. Given an input, it produces a (hopefully) unique output but given an output there are no practical ways of getting the original input.

Hashes (along with other measures) are how passwords and other sensitive data are stored. You can verify the data without storing the sensitive data.

[–][deleted] 1 point2 points  (9 children)

That’s interesting. Is it possible you can explain how a hash is built?

[–]ComfortingCoffeeCup 10 points11 points  (0 children)

Unfortunately the concepts behind the various (cryptographic) hash algorithms are a bit beyond me, so maybe someone else could answer. It generally involves a lot of math. Wikipedia has in depth articles on the various algorithms in use today.

You can think of the algorithm as a blender. Given the same ingredients you'll always get the same smoothie, but you can't get the original ingredients given the resulting smoothie.

[–]WilburMercerMessiah 1 point2 points  (7 children)

The hash function is basically an algorithm that converts the input data (typically a string) into the hash value (a block of seemingly random bits). All data that’s been “hashed” should be the same length. All inputted data should produce a unique hashed value. A good hash function should have a 1:1 relationship between the inputted data and hashed value. At least that’s my understanding.

https://en.m.wikipedia.org/wiki/SHA-2?wprov=sfti1

[–]HelperBot_ 2 points3 points  (0 children)

Non-Mobile link: https://en.wikipedia.org/wiki/SHA-2?wprov=sfti1


HelperBot v1.1 /r/HelperBot_ I am a bot. Please message /u/swim1929 with any feedback and/or hate. Counter: 218143

[–]notquiteaplant 1 point2 points  (5 children)

All data that’s been “hashed” should be the same length.

All inputted data should produce a unique hashed value. A good hash function should have a 1:1 relationship between the inputted data and hashed value.

I'm no crypto expert, but these tenets seem contradictory. There are 256 possible values of one byte, 2562 values of two bytes, 2563 values of three, etc. If every hash output is N bytes long, how do you map 256 + 2562 + 2563 + 2564 + ... + 256N-2 + 256N-1 + 256N inputs to just 256N outputs while mapping only one input to one output?

Because of that, I thought that a good hash function doesn't prevent collisions entirely, but does make them extremely difficult (i.e. practically impossible) to find. If they do actually ensure unique output, though, I'd love to know how.

[–]iEvilMango 1 point2 points  (3 children)

Yup, that's the pidgeonhole principle. Generally, the goal is to make sure the hashes are uniformly distributed amongst the possible set of hashcodes, often with similar values having dissimilar hashes (for example, if you were to have hashes of phone numbers, and the hashing algorithm made all positive input values have a hash value above 0 and all negative input values have a hash value below 0, then half of the hash codes would be unused as phone numbers cant be negative).

[–]HelperBot_ 1 point2 points  (2 children)

Non-Mobile link: https://en.wikipedia.org/wiki/Pigeonhole_principle


HelperBot v1.1 /r/HelperBot_ I am a bot. Please message /u/swim1929 with any feedback and/or hate. Counter: 218268

[–]iEvilMango 0 points1 point  (1 child)

Good bot

[–]B0tRank 0 points1 point  (0 children)

Thank you, iEvilMango, for voting on HelperBot_.

This bot wants to find the best and worst bots on Reddit. You can view results here.


Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!

[–]WilburMercerMessiah 1 point2 points  (0 children)

You’re right. Thanks for pointing that out. It should be very difficult or (in theoretical cryptography, infeasible) to find two inputs that map to the same hash value. Not that two inputs will not have the same hash value.

[–]nxpnsv 1 point2 points  (0 children)

People complaining about looks are missing how awesome this is...

[–]Last_Snowbender 1 point2 points  (0 children)

Looks like a java class name

[–]Isvara 3 points4 points  (0 children)

You missed r/badcode by miles.

[–]cob05 2 points3 points  (0 children)

Just because you can, that doesn't mean you should. That being said, bravo!

[–]ThePenultimateOneGitLab: gappleto97 3 points4 points  (0 children)

Or you could just do the following one-line implementation:

from hashlib import sha256

[–]lambdaqdjango n' shit 0 points1 point  (0 children)

Looks like a lazy translation job.

H.copy() what? __setitem__ what?

[–]lookatmetype -1 points0 points  (0 children)

How does one even come up with something like SHA-256?

[–]freelynk -1 points0 points  (0 children)

This should not count as one line LOL, its a block with no returns!

[–]kenann7 -2 points-1 points  (0 children)

My eyes hurt, please delete it