diff --git a/Cryptography/MD5/MD4/md4.py b/Cryptography/MD5/MD4/md4.py new file mode 100644 index 0000000..67cbd55 --- /dev/null +++ b/Cryptography/MD5/MD4/md4.py @@ -0,0 +1,95 @@ +# MD4 Python 2 implementation +# Copyright (C) 2013 Filippo Valsorda +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# Originally based, even if no code survives, on a LGPL work by +# Dmitry Rozmanov 2002 +# http://www.geocities.com/rozmanov/python/ + +import struct +import binascii + +lrot = lambda x, n: (x << n) | (x >> (32 - n)) + + +class MD4(): + + A, B, C, D = (0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476) + buf = [0x00] * 64 + + _F = lambda self, x, y, z: ((x & y) | (~x & z)) + _G = lambda self, x, y, z: ((x & y) | (x & z) | (y & z)) + _H = lambda self, x, y, z: (x ^ y ^ z) + + def __init__(self, message): + length = struct.pack(' 64: + self._handle(message[:64]) + message = message[64:] + message += '\x80' + message += '\x00' * ((56 - len(message) % 64) % 64) + message += length + while len(message): + self._handle(message[:64]) + message = message[64:] + + def _handle(self, chunk): + X = list(struct.unpack('<' + 'I' * 16, chunk)) + A, B, C, D = self.A, self.B, self.C, self.D + + for i in range(16): + k = i + if i % 4 == 0: + A = lrot((A + self._F(B, C, D) + X[k]) & 0xffffffff, 3) + elif i % 4 == 1: + D = lrot((D + self._F(A, B, C) + X[k]) & 0xffffffff, 7) + elif i % 4 == 2: + C = lrot((C + self._F(D, A, B) + X[k]) & 0xffffffff, 11) + elif i % 4 == 3: + B = lrot((B + self._F(C, D, A) + X[k]) & 0xffffffff, 19) + + for i in range(16): + k = (i // 4) + (i % 4) * 4 + if i % 4 == 0: + A = lrot((A + self._G(B, C, D) + X[k] + 0x5a827999) & 0xffffffff, 3) + elif i % 4 == 1: + D = lrot((D + self._G(A, B, C) + X[k] + 0x5a827999) & 0xffffffff, 5) + elif i % 4 == 2: + C = lrot((C + self._G(D, A, B) + X[k] + 0x5a827999) & 0xffffffff, 9) + elif i % 4 == 3: + B = lrot((B + self._G(C, D, A) + X[k] + 0x5a827999) & 0xffffffff, 13) + + order = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15] + for i in range(16): + k = order[i] + if i % 4 == 0: + A = lrot((A + self._H(B, C, D) + X[k] + 0x6ed9eba1) & 0xffffffff, 3) + elif i % 4 == 1: + D = lrot((D + self._H(A, B, C) + X[k] + 0x6ed9eba1) & 0xffffffff, 9) + elif i % 4 == 2: + C = lrot((C + self._H(D, A, B) + X[k] + 0x6ed9eba1) & 0xffffffff, 11) + elif i % 4 == 3: + B = lrot((B + self._H(C, D, A) + X[k] + 0x6ed9eba1) & 0xffffffff, 15) + + self.A = (self.A + A) & 0xffffffff + self.B = (self.B + B) & 0xffffffff + self.C = (self.C + C) & 0xffffffff + self.D = (self.D + D) & 0xffffffff + + def digest(self): + return struct.pack('. +# +# Originally based, even if no code survives, on a Python license work +# by Dinu C. Gherman (C) 2001 and Aurelian Coman +# http://starship.python.net/crew/gherman/programs/md5py/md5py.py + +import struct +import binascii +import math + +lrot = lambda x, n: (x << n) | (x >> (32 - n)) + + +class MD5(): + + A, B, C, D = (0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476) + + # r specifies the per-round shift amounts + r = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, + 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, + 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, + 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21] + + # Use binary integer part of the sines of integers (Radians) as constants + k = [int(math.floor(abs(math.sin(i + 1)) * (2 ** 32))) for i in range(64)] + + def __init__(self, message): + length = struct.pack(' 64: + self._handle(message[:64]) + message = message[64:] + message += '\x80' + message += '\x00' * ((56 - len(message) % 64) % 64) + message += length + while len(message): + self._handle(message[:64]) + message = message[64:] + + def _handle(self, chunk): + w = list(struct.unpack('<' + 'I' * 16, chunk)) + + a, b, c, d = self.A, self.B, self.C, self.D + + for i in range(64): + if i < 16: + f = (b & c) | ((~b) & d) + g = i + elif i < 32: + f = (d & b) | ((~d) & c) + g = (5 * i + 1) % 16 + elif i < 48: + f = b ^ c ^ d + g = (3 * i + 5) % 16 + else: + f = c ^ (b | (~d)) + g = (7 * i) % 16 + + x = b + lrot((a + f + self.k[i] + w[g]) & 0xffffffff, self.r[i]) + a, b, c, d = d, x & 0xffffffff, b, c + + self.A = (self.A + a) & 0xffffffff + self.B = (self.B + b) & 0xffffffff + self.C = (self.C + c) & 0xffffffff + self.D = (self.D + d) & 0xffffffff + + def digest(self): + return struct.pack('. +# +# Based on a MIT licensed work by Stefano Palazzo (C) 2011 +# https://github.com/sfstpala/SlowSHA/blob/master/slowsha.py + +import struct +import binascii + +lrot = lambda x, n: (x << n) | (x >> (32 - n)) + + +class SHA1(): + + _h0, _h1, _h2, _h3, _h4, = ( + 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0) + + def __init__(self, message): + length = struct.pack('>Q', len(message) * 8) + while len(message) > 64: + self._handle(message[:64]) + message = message[64:] + message += '\x80' + message += '\x00' * ((56 - len(message) % 64) % 64) + message += length + while len(message): + self._handle(message[:64]) + message = message[64:] + + def _handle(self, chunk): + w = list(struct.unpack('>' + 'I' * 16, chunk)) + + for i in range(16, 80): + w.append(lrot(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1) + & 0xffffffff) + + a = self._h0 + b = self._h1 + c = self._h2 + d = self._h3 + e = self._h4 + + for i in range(80): + + if i <= i <= 19: + f, k = d ^ (b & (c ^ d)), 0x5a827999 + elif 20 <= i <= 39: + f, k = b ^ c ^ d, 0x6ed9eba1 + elif 40 <= i <= 59: + f, k = (b & c) | (d & (b | c)), 0x8f1bbcdc + elif 60 <= i <= 79: + f, k = b ^ c ^ d, 0xca62c1d6 + + temp = lrot(a, 5) + f + e + k + w[i] & 0xffffffff + a, b, c, d, e = temp, a, lrot(b, 30), c, d + + self._h0 = (self._h0 + a) & 0xffffffff + self._h1 = (self._h1 + b) & 0xffffffff + self._h2 = (self._h2 + c) & 0xffffffff + self._h3 = (self._h3 + d) & 0xffffffff + self._h4 = (self._h4 + e) & 0xffffffff + + def digest(self): + return struct.pack('>IIIII', self._h0, self._h1, + self._h2, self._h3, self._h4) + + def hexdigest(self): + return binascii.hexlify(self.digest()).decode() diff --git a/Cryptography/SHA/sha2.py b/Cryptography/SHA/sha2.py new file mode 100644 index 0000000..cb8aebc --- /dev/null +++ b/Cryptography/SHA/sha2.py @@ -0,0 +1,188 @@ +# SHA2 family Python 2 implementation +# Copyright (C) 2013 Filippo Valsorda +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import struct +import binascii +import math + +rrot = lambda x, n: (x >> n) | (x << (32 - n)) + +from itertools import count, islice +primes = lambda n: islice((k for k in count(2) if all(k % d for d in range(2, k))), 0, n) + +class _SHA2_32(): + + # (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311) + _k = [int(math.modf(x ** (1.0/3))[0] * (1 << 32)) for x in primes(64)] + + def __init__(self, message): + length = struct.pack('>Q', len(message) * 8) + while len(message) > 64: + self._handle(message[:64]) + message = message[64:] + message += '\x80' + message += '\x00' * ((56 - len(message) % 64) % 64) + message += length + while len(message): + self._handle(message[:64]) + message = message[64:] + + def _handle(self, chunk): + w = list(struct.unpack('>' + 'I' * 16, chunk)) + + for i in range(16, 64): + s0 = rrot(w[i - 15], 7) ^ rrot(w[i - 15], 18) ^ (w[i - 15] >> 3) + s1 = rrot(w[i - 2], 17) ^ rrot(w[i - 2], 19) ^ (w[i - 2] >> 10) + w.append((w[i - 16] + s0 + w[i - 7] + s1) & 0xffffffff) + + a = self._h0 + b = self._h1 + c = self._h2 + d = self._h3 + e = self._h4 + f = self._h5 + g = self._h6 + h = self._h7 + + for i in range(64): + S1 = rrot(e, 6) ^ rrot(e, 11) ^ rrot(e, 25) + ch = (e & f) ^ ((~e) & g) + temp = h + S1 + ch + self._k[i] + w[i] + d = (d + temp) & 0xffffffff + S0 = rrot(a, 2) ^ rrot(a, 13) ^ rrot(a, 22) + maj = (a & (b ^ c)) ^ (b & c) + temp = (temp + S0 + maj) & 0xffffffff + + h, g, f, e, d, c, b, a = g, f, e, d, c, b, a, temp + + self._h0 = (self._h0 + a) & 0xffffffff + self._h1 = (self._h1 + b) & 0xffffffff + self._h2 = (self._h2 + c) & 0xffffffff + self._h3 = (self._h3 + d) & 0xffffffff + self._h4 = (self._h4 + e) & 0xffffffff + self._h5 = (self._h5 + f) & 0xffffffff + self._h6 = (self._h6 + g) & 0xffffffff + self._h7 = (self._h7 + h) & 0xffffffff + + def hexdigest(self): + return binascii.hexlify(self.digest()).decode() + + +class SHA2_256(_SHA2_32): + + # (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19) + _h0, _h1, _h2, _h3, _h4, _h5, _h6, _h7 = ( + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19) + + def digest(self): + return struct.pack('>IIIIIIII', self._h0, self._h1, self._h2, + self._h3, self._h4, self._h5, self._h6, self._h7) + +class SHA2_224(_SHA2_32): + + # (second 32 bits of the fractional parts of the square roots of the 9..16 primes 23..53) + _h0, _h1, _h2, _h3, _h4, _h5, _h6, _h7 = ( + 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, + 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4) + + def digest(self): + return struct.pack('>IIIIIII', self._h0, self._h1, self._h2, + self._h3, self._h4, self._h5, self._h6) + + +class _SHA2_64(): + + # (first 64 bits of the fractional parts of the cube roots of the first 80 primes) + _k = [int(math.modf(x ** (1.0/3))[0] * (1 << 80)) for x in primes(80)] + + def __init__(self, message): + length = struct.pack('>QQ', (len(message) * 8) >> 32, (len(message) * 8) & 0xffffffff) + while len(message) > 128: + self._handle(message[:128]) + message = message[128:] + message += '\x80' + message += '\x00' * ((112 - len(message) % 128) % 128) + message += length + while len(message): + self._handle(message[:128]) + message = message[128:] + + def _handle(self, chunk): + w = list(struct.unpack('>' + 'Q' * 16, chunk)) + + for i in range(16, 80): + s0 = rrot(w[i - 15], 7) ^ rrot(w[i - 15], 18) ^ (w[i - 15] >> 3) + s1 = rrot(w[i - 2], 17) ^ rrot(w[i - 2], 19) ^ (w[i - 2] >> 10) + w.append((w[i - 16] + s0 + w[i - 7] + s1) & 0xffffffffffffffff) + + a = self._h0 + b = self._h1 + c = self._h2 + d = self._h3 + e = self._h4 + f = self._h5 + g = self._h6 + h = self._h7 + + for i in range(80): + S1 = rrot(e, 6) ^ rrot(e, 11) ^ rrot(e, 25) + ch = (e & f) ^ ((~e) & g) + temp = h + S1 + ch + self._k[i] + w[i] + d = (d + temp) & 0xffffffffffffffff + S0 = rrot(a, 2) ^ rrot(a, 13) ^ rrot(a, 22) + maj = (a & (b ^ c)) ^ (b & c) + temp = (temp + S0 + maj) & 0xffffffffffffffff + + h, g, f, e, d, c, b, a = g, f, e, d, c, b, a, temp + + self._h0 = (self._h0 + a) & 0xffffffffffffffff + self._h1 = (self._h1 + b) & 0xffffffffffffffff + self._h2 = (self._h2 + c) & 0xffffffffffffffff + self._h3 = (self._h3 + d) & 0xffffffffffffffff + self._h4 = (self._h4 + e) & 0xffffffffffffffff + self._h5 = (self._h5 + f) & 0xffffffffffffffff + self._h6 = (self._h6 + g) & 0xffffffffffffffff + self._h7 = (self._h7 + h) & 0xffffffffffffffff + + def hexdigest(self): + return binascii.hexlify(self.digest()).decode() + + +class SHA2_512(_SHA2_64): + + # (first 64 bits of the fractional parts of the square roots of the first 8 primes 2..19) + _h0, _h1, _h2, _h3, _h4, _h5, _h6, _h7 = ( + 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, + 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, + 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179) + + def digest(self): + return struct.pack('>QQQQQQQQ', self._h0, self._h1, self._h2, + self._h3, self._h4, self._h5, self._h6, self._h7) + + +class SHA2_384(_SHA2_64): + + # (second 64 bits of the fractional parts of the square roots of the 9..16 primes 23..53) + _h0, _h1, _h2, _h3, _h4, _h5, _h6, _h7 = ( + 0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, + 0x152fecd8f70e5939, 0x67332667ffc00b31, 0x8eb44a8768581511, + 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4) + + def digest(self): + return struct.pack('>QQQQQQQ', self._h0, self._h1, self._h2, + self._h3, self._h4, self._h5, self._h6)