def test_sha1(self): import sha1 stuff = RAND_pseudo_bytes(500)[0] for n in range(500): self.assertEqual( hashlib.sha1(stuff[:n]).hexdigest(), sha1.SHA1(stuff[:n]).hexdigest())
def dsa_priv_key_from_k(m, k, r, s, q): sha_out = sha1.SHA1(m).finish() H_m = int(sha_out.hex(), 16) x_guess = ((((s * k) - H_m)) * invmod(r, q)) % q return (x_guess)
def gen_DSA_sig_given_k(x, m, p, q, g, k): r = pow(g, k, p) % q sha_out = sha1.SHA1(m).finish() sha_int = int(sha_out.hex(), 16) s = (invmod(k, q) * (sha_int + x * r)) % q return (r, s)
def check_DSA_sig(m, y, r, s, p, g, q): w = invmod(s, q) sha_out = sha1.SHA1(m).finish() sha_int = int(sha_out.hex(), 16) u1 = (sha_int * w) % q u2 = (r * w) % q v = ((pow(g, u1, p) * pow(y, u2, p)) % p) % q return (v == r)
def gen_DSA_sig(x, m, p, q, g): k = random.randint(0, q - 1) r = pow(g, k, p) % q sha_out = sha1.SHA1(m).finish() sha_int = int(sha_out.hex(), 16) s = (invmod(k, q) * (sha_int + x * r)) % q return (r, s, k)
def HMAC(data, key): key_len = len(key) if key_len > 64: key = sha1.SHA(key).finish() elif key_len < 64: key = key + b'\x00' * (64 - key_len) o_key_pad = cp.bitwise_xor(key, b'\x5c' * 64) i_key_pad = cp.bitwise_xor(key, b'\x36' * 64) pass_0_out = sha1.SHA1(i_key_pad + data).finish() pass_1_out = sha1.SHA1(o_key_pad + pass_0_out).finish() return (pass_1_out)
def challenge29(message, extension): for keylen in range(1, 101): forged_message = challenge29_pad(message, keylen) + extension digest = challenge28(message) sha1_handle = sha1.SHA1() sha1_handle._h = struct.unpack('>5I', digest) sha1_handle._message_byte_length = keylen + \ len(forged_message) - len(extension) assert sha1_handle._message_byte_length % 64 == 0 forged_digest = sha1_handle.update(extension).digest() if mac(forged_message) == forged_digest: return forged_message, forged_digest raise NoValidKeyLength('No valid key length from 1 to 100')
def mac(message, algorithm='sha1'): if algorithm == 'sha1': return sha1.SHA1().update(constants.key + message).digest() if algorithm == 'md4': return md4.MD4().update(constants.key + message).digest() raise ValueError('Invalid value for parameter algorithm')
def sha1_hexdigest(txt): sha = sha1.SHA1() sha.update(txt) return sha.hexdigest()
import sha1 import uuid import sys # "".join(format(ord(c), "x") for c in s) # char to hex ascii # bytearray.fromhex("7061756c").decode() # hex ascii to string lastbyte = 4 times = 100000 plain_list = [uuid.uuid4().hex for i in range(times)] hash_list = [sha1.SHA1(str.encode(code))[-lastbyte:] for code in plain_list] # duplicate = {plain_list[i]: sha for i, sha in enumerate(hash_list) if hash_list.count(sha) > 1} duplicate = dict() for i, h in enumerate(hash_list): if hash_list.count(h) > 1: if h not in duplicate: duplicate[h] = [plain_list[i]] else: duplicate[h].append(plain_list[i]) for sha, plain in duplicate.items(): print(sha, ":") print(",".join(p for p in plain)) print()