initial commit

This commit is contained in:
Felix Bargfeldt 2018-10-22 21:15:06 +02:00
parent 5920b38024
commit cf5bf41284
No known key found for this signature in database
GPG key ID: 99184F5FDC589A67
15 changed files with 488 additions and 0 deletions

60
SHA-256/sha256.py Normal file
View file

@ -0,0 +1,60 @@
def rot_r(a: int, n: int) -> int:
return ((a >> n) | (a << (32 - n))) & 0xffffffff
def ch(x: int, y: int, z: int) -> int:
return z ^ (x & (y ^ z))
def maj(x: int, y: int, z: int) -> int:
return (x & y) ^ (x & z) ^ (y & z)
def sigma0(x: int) -> int:
return rot_r(x, 2) ^ rot_r(x, 13) ^ rot_r(x, 22)
def sigma1(x: int) -> int:
return rot_r(x, 6) ^ rot_r(x, 11) ^ rot_r(x, 25)
def gamma0(x: int) -> int:
return rot_r(x, 7) ^ rot_r(x, 18) ^ (x >> 3)
def gamma1(x: int) -> int:
return rot_r(x, 17) ^ rot_r(x, 19) ^ (x >> 10)
K = [
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
]
def sha256(data: bytes) -> bytes:
H = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
]
message = data + b'\x80'
message += b'\x00' * ((56 - len(message) % 64) % 64)
message += (len(data) * 8).to_bytes(8, 'big')
for i_ in range(len(message) // 64):
block = message[i_ * 64:(i_ + 1) * 64]
w = [int.from_bytes(block[i * 4:(i + 1) * 4], 'big') for i in range(16)]
for i in range(48):
w.append((gamma1(w[-2]) + w[-7] + gamma0(w[-15]) + w[-16]) & 0xffffffff)
g = H.copy()
for i in range(64):
t1 = (g[7] + sigma1(g[4]) + ch(g[4], g[5], g[6]) + K[i] + w[i]) & 0xffffffff
t2 = (sigma0(g[0]) + maj(g[0], g[1], g[2])) & 0xffffffff
g = (t1 + t2) & 0xffffffff, g[0], g[1], g[2], (g[3] + t1) & 0xffffffff, g[4], g[5], g[6]
H = [(x + y) & 0xffffffff for x, y in zip(H, g)]
return b''.join([x.to_bytes(4, 'big') for x in H])

69
SHA-256/sha256_lambda.py Normal file
View file

@ -0,0 +1,69 @@
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]

View file

@ -0,0 +1 @@
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]

37
SHA1/sha1.py Normal file
View file

@ -0,0 +1,37 @@
def left_rotate(n: int, b: int) -> int:
return ((n << b) | (n >> (32 - b))) & 0xffffffff
def sha1(data: bytes) -> bytes:
h = [
0x67452301,
0xEFCDAB89,
0x98BADCFE,
0x10325476,
0xC3D2E1F0
]
message = data + b'\x80'
message += b'\x00' * ((56 - len(message) % 64) % 64)
message += (len(data) * 8).to_bytes(8, 'big')
for i_ in range(len(message) // 64):
chunk = message[i_ * 64:(i_ + 1) * 64]
w = [int.from_bytes(chunk[i * 4:(i + 1) * 4], 'big') for i in range(16)]
for i in range(64):
w.append(left_rotate(w[-3] ^ w[-8] ^ w[-14] ^ w[-16], 1))
a, b, c, d, e = h
for i in range(80):
if i < 20:
f = d ^ (b & (c ^ d))
k = 0x5A827999
elif i < 40:
f = b ^ c ^ d
k = 0x6ED9EBA1
elif i < 60:
f = (b & c) | (b & d) | (c & d)
k = 0x8F1BBCDC
else:
f = b ^ c ^ d
k = 0xCA62C1D6
a, b, c, d, e = (left_rotate(a, 5) + f + e + k + w[i]) & 0xffffffff, a, left_rotate(b, 30), c, d
h = [(x + y) & 0xffffffff for x, y in zip(h, [a, b, c, d, e])]
return b''.join([x.to_bytes(4, 'big') for x in h])

54
SHA1/sha1_lambda.py Normal file
View file

@ -0,0 +1,54 @@
sha1 = lambda data: [
([
[
([
w.append(rotate(w[-3] ^ w[-8] ^ w[-14] ^ w[-16], 1))
for _ in range(64)
] and False) or [
([
[
[
g.__setitem__(j, values[j])
for j in range(5)
]
for values in [[
(rotate(g[0], 5) + [
g[3] ^ (g[1] & (g[2] ^ g[3])),
g[1] ^ g[2] ^ g[3],
(g[1] & g[2]) | (g[1] & g[3]) | (g[2] & g[3]),
g[1] ^ g[2] ^ g[3]
][i // 20] + g[4] + [
0x5A827999,
0x6ED9EBA1,
0x8F1BBCDC,
0xCA62C1D6
][i // 20] + w[i]) & 0xffffffff,
g[0],
rotate(g[1], 30),
g[2],
g[3]
]]
]
for i in range(80)
] and False) or [
h.__setitem__(i, (h[i] + g[i]) & 0xffffffff)
for i in range(5)
]
for g in [[_ for _ in h]]
]
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)
] and False) or b''.join([x.to_bytes(4, 'big') for x in h])
for msg, rotate, h in [(
data + b'\x80' + b'\x00' * ((56 - (len(data) + 1) % 64) % 64) + (len(data) * 8).to_bytes(8, 'big'),
lambda n, b: ((n << b) | (n >> (32 - b))) & 0xffffffff,
[
0x67452301,
0xEFCDAB89,
0x98BADCFE,
0x10325476,
0xC3D2E1F0
]
)]
][0]

1
SHA1/sha1_one_line.py Normal file
View file

@ -0,0 +1 @@
sha1 = lambda data: [([[([w.append(rotate(w[-3] ^ w[-8] ^ w[-14] ^ w[-16], 1)) for _ in range(64)] and False) or [([[[g.__setitem__(j, values[j]) for j in range(5)] for values in [[(rotate(g[0], 5) + [g[3] ^ (g[1] & (g[2] ^ g[3])),g[1] ^ g[2] ^ g[3],(g[1] & g[2]) | (g[1] & g[3]) | (g[2] & g[3]),g[1] ^ g[2] ^ g[3]][i // 20] + g[4] + [0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6][i // 20] + w[i]) & 0xffffffff,g[0],rotate(g[1], 30),g[2],g[3]]]] for i in range(80)] and False) or [h.__setitem__(i, (h[i] + g[i]) & 0xffffffff) for i in range(5)] for g in [[_ for _ in h]]] 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)] and False) or b''.join([x.to_bytes(4, 'big') for x in h]) for msg, rotate, h in [(data + b'\x80' + b'\x00' * ((56 - (len(data) + 1) % 64) % 64) + (len(data) * 8).to_bytes(8, 'big'),lambda n, b: ((n << b) | (n >> (32 - b))) & 0xffffffff,[0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0])]][0]

37
TOTP/pybase64.py Normal file
View file

@ -0,0 +1,37 @@
import base64
import os
import string
charset = string.ascii_uppercase + string.ascii_lowercase + string.digits + "+/"
def base64encode(data: bytes) -> str:
bits = "".join([bin(c)[2:].rjust(8, '0') for c in data]) + "0" * ((3 - len(data)) % 3 * 2)
chunks = [bits[i * 6:(i + 1) * 6] for i in range(len(bits) // 6)]
return "".join([charset[int(c, 2)] for c in chunks]) + "=" * ((3 - len(data)) % 3)
def base64decode(data: str) -> bytes:
padding = data.count("=")
data = data.strip("=")
bits = "".join([bin(charset.index(c))[2:].rjust(6, '0') for c in data])
if padding:
bits = bits[:-padding * 2]
return bytes([int(bits[i * 8:(i + 1) * 8], 2) for i in range(len(bits) // 8)])
def test(inp: bytes, enc: callable, enc2: callable, dec: callable):
print("Input:", inp)
my = enc(inp)
print("My Output:", my)
correct = enc2(inp).decode()
print("Correct Output:", correct)
if my == correct:
dec = dec(my)
print("Decoded:", dec)
print(dec == inp)
else:
print(False)
test(os.urandom(385), base64encode, base64.b64encode, base64decode)

7
TOTP/pyhmac.py Normal file
View file

@ -0,0 +1,7 @@
def hmac(key: bytes, msg: bytes, hasher: callable) -> bytes:
if len(key) > 64:
key = hasher(key)
key += bytes(64 - len(key))
opad = bytes(0x5c ^ i for i in range(256))
ipad = bytes(0x36 ^ i for i in range(256))
return hasher(key.translate(opad) + hasher(key.translate(ipad) + msg))

View file

@ -0,0 +1 @@
sha1_hmac = lambda key, msg: [[sha1(key.translate(bytes(0x5c ^ i for i in range(256))) + sha1(key.translate(bytes(0x36 ^ i for i in range(256))) + msg)) for key in [(key if len(key) <= 64 else sha1(key)).ljust(64, b'\x00')]][0] for sha1 in [lambda data: [([[([w.append(rotate(w[-3] ^ w[-8] ^ w[-14] ^ w[-16], 1)) for _ in range(64)] and False) or [([[[g.__setitem__(j, values[j]) for j in range(5)] for values in [[(rotate(g[0], 5) + [g[3] ^ (g[1] & (g[2] ^ g[3])),g[1] ^ g[2] ^ g[3],(g[1] & g[2]) | (g[1] & g[3]) | (g[2] & g[3]),g[1] ^ g[2] ^ g[3]][i // 20] + g[4] + [0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6][i // 20] + w[i]) & 0xffffffff,g[0],rotate(g[1], 30),g[2],g[3]]]] for i in range(80)] and False) or [h.__setitem__(i, (h[i] + g[i]) & 0xffffffff) for i in range(5)] for g in [[_ for _ in h]]] 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)] and False) or b''.join([x.to_bytes(4, 'big') for x in h]) for msg, rotate, h in [(data + b'\x80' + b'\x00' * ((56 - (len(data) + 1) % 64) % 64) + (len(data) * 8).to_bytes(8, 'big'),lambda n, b: ((n << b) | (n >> (32 - b))) & 0xffffffff,[0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0])]][0]]][0]

38
TOTP/totp.py Normal file
View file

@ -0,0 +1,38 @@
import base64
import hashlib
import hmac
import random
import time
class TOTP:
def __init__(self, secret: str):
self.secret = secret
def now(self) -> str:
return self.at(int(time.time() / 30))
def at(self, for_time: int) -> str:
secret = base64.b32decode(self.secret)
hmac_hash = hmac.HMAC(secret, for_time.to_bytes(8, 'big'), hashlib.sha1).digest()
offset = hmac_hash[-1] & 0xf
code = ((hmac_hash[offset] & 0x7f) << 24 |
(hmac_hash[offset + 1] & 0xff) << 16 |
(hmac_hash[offset + 2] & 0xff) << 8 |
(hmac_hash[offset + 3] & 0xff))
out = str(code % 10 ** 6)
return "0" * (6 - len(out)) + out
def verify(self, code: str, valid_window: int = 0) -> bool:
for_time = int(time.time() / 30)
for i in range(-valid_window, valid_window + 1):
if self.at(for_time + i) == code.replace(" ", ""):
return True
return False
def generate_uri(self, user: str, issuer: str) -> str:
return "otpauth://totp/%s?secret=%s&issuer=%s" % (user, self.secret.lower(), issuer)
@staticmethod
def generate_random():
return TOTP("".join([random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567") for _ in range(16)]))

17
TOTP/totp_lambda.py Normal file
View file

@ -0,0 +1,17 @@
totp = lambda secret: [
[
[
str(((hmac_hash[offset] & 0x7f) << 24 |
(hmac_hash[offset + 1] & 0xff) << 16 |
(hmac_hash[offset + 2] & 0xff) << 8 |
(hmac_hash[offset + 3] & 0xff)) % 10 ** 6).rjust(6, '0')
for offset in [hmac_hash[-1] & 0xf]
][0]
for hmac_hash in [sha1_hmac(base32decode(secret.replace(" ", "").upper()), int(time / 30).to_bytes(8, 'big'))]
][0]
for base32decode, sha1_hmac, time in [(
lambda data: [bytes([int(bits[i * 8:(i + 1) * 8], 2) for i in range(len(bits) // 8)]) for bits in [("".join([bin("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".index(c))[2:].rjust(5, '0') for c in data.strip("=")]))]][0],
lambda key, msg: [[sha1(key.translate(bytes(0x5c ^ i for i in range(256))) + sha1(key.translate(bytes(0x36 ^ i for i in range(256))) + msg)) for key in [(key if len(key) <= 64 else sha1(key)).ljust(64, b'\x00')]][0] for sha1 in [lambda data: [([[([w.append(rotate(w[-3] ^ w[-8] ^ w[-14] ^ w[-16], 1)) for _ in range(64)] and False) or [([[[g.__setitem__(j, values[j]) for j in range(5)] for values in [[(rotate(g[0], 5) + [g[3] ^ (g[1] & (g[2] ^ g[3])),g[1] ^ g[2] ^ g[3],(g[1] & g[2]) | (g[1] & g[3]) | (g[2] & g[3]),g[1] ^ g[2] ^ g[3]][i // 20] + g[4] + [0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6][i // 20] + w[i]) & 0xffffffff,g[0],rotate(g[1], 30),g[2],g[3]]]] for i in range(80)] and False) or [h.__setitem__(i, (h[i] + g[i]) & 0xffffffff) for i in range(5)] for g in [[_ for _ in h]]] 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)] and False) or b''.join([x.to_bytes(4, 'big') for x in h]) for msg, rotate, h in [(data + b'\x80' + b'\x00' * ((56 - (len(data) + 1) % 64) % 64) + (len(data) * 8).to_bytes(8, 'big'),lambda n, b: ((n << b) | (n >> (32 - b))) & 0xffffffff,[0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0])]][0]]][0],
__import__("time").time()
)]
][0]

1
TOTP/totp_one_line.py Normal file
View file

@ -0,0 +1 @@
totp = lambda secret: [[[str(((hmac_hash[offset] & 0x7f) << 24 | (hmac_hash[offset + 1] & 0xff) << 16 | (hmac_hash[offset + 2] & 0xff) << 8 | (hmac_hash[offset + 3] & 0xff)) % 10 ** 6).rjust(6, '0') for offset in [hmac_hash[-1] & 0xf]][0] for hmac_hash in [sha1_hmac(base32decode(secret.replace(" ", "").upper()), int(time / 30).to_bytes(8, 'big'))]][0] for base32decode, sha1_hmac, time in [(lambda data: [bytes([int(bits[i * 8:(i + 1) * 8], 2) for i in range(len(bits) // 8)]) for bits in [("".join([bin("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".index(c))[2:].rjust(5, '0') for c in data.strip("=")]))]][0], lambda key, msg: [[sha1(key.translate(bytes(0x5c ^ i for i in range(256))) + sha1(key.translate(bytes(0x36 ^ i for i in range(256))) + msg)) for key in [(key if len(key) <= 64 else sha1(key)).ljust(64, b'\x00')]][0] for sha1 in [lambda data: [([[([w.append(rotate(w[-3] ^ w[-8] ^ w[-14] ^ w[-16], 1)) for _ in range(64)] and False) or [([[[g.__setitem__(j, values[j]) for j in range(5)] for values in [[(rotate(g[0], 5) + [g[3] ^ (g[1] & (g[2] ^ g[3])),g[1] ^ g[2] ^ g[3],(g[1] & g[2]) | (g[1] & g[3]) | (g[2] & g[3]),g[1] ^ g[2] ^ g[3]][i // 20] + g[4] + [0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6][i // 20] + w[i]) & 0xffffffff,g[0],rotate(g[1], 30),g[2],g[3]]]] for i in range(80)] and False) or [h.__setitem__(i, (h[i] + g[i]) & 0xffffffff) for i in range(5)] for g in [[_ for _ in h]]] 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)] and False) or b''.join([x.to_bytes(4, 'big') for x in h]) for msg, rotate, h in [(data + b'\x80' + b'\x00' * ((56 - (len(data) + 1) % 64) % 64) + (len(data) * 8).to_bytes(8, 'big'),lambda n, b: ((n << b) | (n >> (32 - b))) & 0xffffffff,[0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0])]][0]]][0], __import__("time").time())]][0]

62
TicTacToe/ctictactoe.py Normal file
View file

@ -0,0 +1,62 @@
state = [[0 for _ in range(3)] for _ in range(3)]
def num_to_cell(num: int) -> (int, int):
return (num - 1) % 3, 2 - (num - 1) // 3
def cell_to_num(x: int, y: int) -> int:
return (2 - y) * 3 + x + 1
def display(numbers: bool):
print("\033c", end='')
for i in range(3):
if i > 0:
print("---+---+---")
print("|".join([" " + ("\033[36m" + str(cell_to_num(j, i)) + "\033[0m" if numbers and x == 0 else " XO"[x]) + " " for j, x in
enumerate(state[i])]))
def userinput():
valid = [str(cell_to_num(j, i)) for i in range(3) for j in range(3) if state[i][j] == 0]
x = ""
while x not in valid:
display(True)
x = input(">> ")
x, y = num_to_cell(int(x))
state[y][x] = 1
def aimove():
for y in range(3):
for x in range(3):
if state[y][x] == 0:
state[y][x] = 2
return
def check_state() -> int:
for row in state:
if row == [1] * 3 or row == [2] * 3:
return row[0]
for col in zip(*state):
if col == (1,) * 3 or col == (2,) * 3:
return col[0]
if state[0][0] == state[1][1] == state[2][2] != 0:
return state[0][0]
if state[2][0] == state[1][1] == state[0][2] != 0:
return state[2][0]
for x in range(3):
for y in range(3):
if state[y][x] == 0:
return -1
return 0
while check_state() == -1:
userinput()
if check_state() == -1:
aimove()
display(False)
print(["Draw!", "You won!", "Computer won!"][check_state()])

View file

@ -0,0 +1,102 @@
# state = [[0 for _ in range(3)] for _ in range(3)]
# num_to_cell = lambda num: ((num - 1) % 3, 2 - (num - 1) // 3)
# cell_to_num = lambda x, y: (2 - y) * 3 + x + 1
# display = lambda numbers: print("\033c", end='') or ([print(("---+---+---\n" if i > 0 else "") + "|".join(
# [" " + ("\033[36m" + str(cell_to_num(j, i)) + "\033[0m" if numbers and x == 0 else " XO"[x]) + " " for j, x in
# enumerate(state[i])])) for i in range(3)] and None)
# userinput = lambda: [
# (list(iter(lambda: (display(True) or xin.__setitem__(0, input(">> ")) or True) and xin[0] in valid,
# True)) and False) or [state[y].__setitem__(x, 1) for x, y in [num_to_cell(int(xin[0]))]]
# for xin, valid in [([""], [str(cell_to_num(j, i)) for i in range(3) for j in range(3) if state[i][j] == 0])]
# ]
# check_state = lambda: \
# ([row[0] for row in state if row == [1] * 3 or row == [2] * 3] or
# [col[0] for col in zip(*state) if col == (1,) * 3 or col == (2,) * 3] or
# ([state[0][0]] if state[0][0] == state[1][1] == state[2][2] != 0 else []) or
# ([state[2][0]] if state[2][0] == state[1][1] == state[0][2] != 0 else []) or
# [-1 if any([state[y][x] == 0 for x in range(3) for y in range(3)]) else 0])[0]
# old_aimove = lambda: [
# list(iter(lambda: (
# value[0] is not None and state[value[0][1]][value[0][0]] == 0 or
# value.__setitem__(0, (random(), random())) and False), True))
# and False or value[0]
# for value, random in [([None], lambda: __import__("random").randint(0, 2))]
# ][0]
# aimove = lambda: ([
# (k, j)
# for i in [2, 1] for j in range(3) for k in range(3) for l in range(4)
# if (l == 0 and state[j][k] == 0 and state[j][(k+1)%3] == i and state[j][(k+2)%3] == i) or
# (l == 1 and state[j][k] == 0 and state[(j+1)%3][k] == i and state[(j+2)%3][k] == i) or
# (l == 2 and j == k and state[j][k] == 0 and state[(j+1)%3][(k+1)%3] == i and state[(j+2)%3][(k+2)%3] == i) or
# (l == 3 and j + k == 2 and state[j][k] == 0 and state[(j+1)%3][(k+2)%3] == i and state[(j+2)%3][(k+1)%3] == i)
# ] + ([(1, 1)] if True and state[1][1] == 0 else []) + [
# list(iter(lambda: (
# value[0] is not None and state[value[0][1]][value[0][0]] == 0 or
# value.__setitem__(0, (random(), random())) and False), True))
# and False or value[0]
# for value, random in [([None], lambda: __import__("random").randint(0, 2))]
# ]
# )[0]
(lambda: [
[
[
list(iter(lambda: (
(userinput() and False or check_state() == -1 and [state[y].__setitem__(x, 2) for x, y in
[aimove()]]) and False or
check_state() != -1
), True)) and
display(False) or
print(["Draw!", "You won!", "Computer won!"][check_state()])
for userinput, aimove in [[
lambda: [
(list(iter(lambda: (display(True) or xin.__setitem__(0, input(">> ")) or True) and xin[0] in valid,
True)) and False) or [state[y].__setitem__(x, 1) for x, y in [num_to_cell(int(xin[0]))]]
for xin, valid in [([""], [str(cell_to_num(j, i)) for i in range(3) for j in range(3) if state[i][j] == 0])]
],
lambda: ([
(k, j)
for i in [2, 1] for j in range(3) for k in range(3) for l in range(4)
if (l == 0 and state[j][k] == 0 and state[j][(k+1)%3] == i and state[j][(k+2)%3] == i) or
(l == 1 and state[j][k] == 0 and state[(j+1)%3][k] == i and state[(j+2)%3][k] == i) or
(l == 2 and j == k and state[j][k] == 0 and state[(j+1)%3][(k+1)%3] == i and state[(j+2)%3][(k+2)%3] == i) or
(l == 3 and j + k == 2 and state[j][k] == 0 and state[(j+1)%3][(k+2)%3] == i and state[(j+2)%3][(k+1)%3] == i)
] + ([(1, 1)] if True and state[1][1] == 0 else []) + [
list(iter(lambda: (
value[0] is not None and state[value[0][1]][value[0][0]] == 0 or
value.__setitem__(0, (random(), random())) and False), True))
and False or value[0]
for value, random in [([None], lambda: __import__("random").randint(0, 2))]
]
)[0]
]]
]
for display, check_state in [[lambda numbers: print("\033c" if ansi else "", end='') or ([print(("---+---+---\n" if i > 0 else "") + "|".join(
[" " + (("\033[36m" if ansi else "") + str(cell_to_num(j, i)) + ("\033[0m" if ansi else "") if numbers and x == 0 else " XO"[x]) + " " for j, x in
enumerate(state[i])])) for i in range(3)] and None),
lambda: ([row[0] for row in state if row == [1] * 3 or row == [2] * 3] or
[col[0] for col in zip(*state) if col == (1,) * 3 or col == (2,) * 3] or
([state[0][0]] if state[0][0] == state[1][1] == state[2][2] != 0 else []) or
([state[2][0]] if state[2][0] == state[1][1] == state[0][2] != 0 else []) or
[-1 if any([state[y][x] == 0 for x in range(3) for y in range(3)]) else 0])[0]]]
]
for state, num_to_cell, cell_to_num, ansi in [(
[[0 for _ in range(3)] for _ in range(3)],
(lambda num: ((num - 1) % 3, 2 - (num - 1) // 3)),
(lambda x, y: (2 - y) * 3 + x + 1),
True
)]
] and None)()
# list(iter(lambda: (
# (userinput() and False or check_state() == -1 and [state[y].__setitem__(x, 2) for x, y in
# [aimove()]]) and False or
# check_state() != -1
# ), True)) and
# display(False) or
# print(["Draw!", "You won!", "Computer won!"][check_state()])

View file

@ -0,0 +1 @@
(lambda: [[[list(iter(lambda: ((userinput() and False or check_state() == -1 and [state[y].__setitem__(x, 2) for x, y in [aimove()]]) and False or check_state() != -1), True)) and display(False) or print(["Draw!", "You won!", "Computer won!"][check_state()]) for userinput, aimove in [[lambda: [(list(iter(lambda: (display(True) or xin.__setitem__(0, input(">> ")) or True) and xin[0] in valid, True)) and False) or [state[y].__setitem__(x, 1) for x, y in [num_to_cell(int(xin[0]))]] for xin, valid in [([""], [str(cell_to_num(j, i)) for i in range(3) for j in range(3) if state[i][j] == 0])]], lambda: ([(k, j) for i in [2, 1] for j in range(3) for k in range(3) for l in range(4) if (l == 0 and state[j][k] == 0 and state[j][(k+1)%3] == i and state[j][(k+2)%3] == i) or (l == 1 and state[j][k] == 0 and state[(j+1)%3][k] == i and state[(j+2)%3][k] == i) or (l == 2 and j == k and state[j][k] == 0 and state[(j+1)%3][(k+1)%3] == i and state[(j+2)%3][(k+2)%3] == i) or (l == 3 and j + k == 2 and state[j][k] == 0 and state[(j+1)%3][(k+2)%3] == i and state[(j+2)%3][(k+1)%3] == i)] + ([(1, 1)] if True and state[1][1] == 0 else []) + [list(iter(lambda: (value[0] is not None and state[value[0][1]][value[0][0]] == 0 or value.__setitem__(0, (random(), random())) and False), True)) and False or value[0] for value, random in [([None], lambda: __import__("random").randint(0, 2))]])[0]]]] for display, check_state in [[lambda numbers: print("\033c" if ansi else "", end='') or ([print(("---+---+---\n" if i > 0 else "") + "|".join([" " + (("\033[36m" if ansi else "") + str(cell_to_num(j, i)) + ("\033[0m" if ansi else "") if numbers and x == 0 else " XO"[x]) + " " for j, x in enumerate(state[i])])) for i in range(3)] and None), lambda: ([row[0] for row in state if row == [1] * 3 or row == [2] * 3] or [col[0] for col in zip(*state) if col == (1,) * 3 or col == (2,) * 3] or ([state[0][0]] if state[0][0] == state[1][1] == state[2][2] != 0 else []) or ([state[2][0]] if state[2][0] == state[1][1] == state[0][2] != 0 else []) or [-1 if any([state[y][x] == 0 for x in range(3) for y in range(3)]) else 0])[0]]]] for state, num_to_cell, cell_to_num, ansi in [([[0 for _ in range(3)] for _ in range(3)], (lambda num: ((num - 1) % 3, 2 - (num - 1) // 3)), (lambda x, y: (2 - y) * 3 + x + 1), True)]] and None)()