def verify(self, received_mac_tag): """Validate the *binary* MAC tag. The caller invokes this function at the very end. This method checks if the decrypted message is indeed valid (that is, if the key is correct) and it has not been tampered with while in transit. :Parameters: received_mac_tag : byte string This is the *binary* MAC, as received from the sender. :Raises ValueError: if the MAC does not match. The message has been tampered with or the key is incorrect. """ if self.verify not in self._next: raise TypeError("verify() cannot be called" " when encrypting a message") self._next = [self.verify] if self._mac_tag is None: self._mac_tag = self._kdf.derive() # Constant-time comparison if SHA3.new(self._mac_tag).digest() != SHA3.new(received_mac_tag).digest(): raise ValueError("MAC check failed")
def test_update_after_digest(self): msg=b("rrrrttt") # Normally, update() cannot be done after digest() h = SHA3.new(data=msg[:4]) dig1 = h.digest() self.assertRaises(TypeError, h.update, msg[4:]) dig2 = SHA3.new(data=msg).digest() # With the proper flag, it is allowed h = SHA3.new(data=msg[:4], update_after_digest=True) self.assertEquals(h.digest(), dig1) # ... and the subsequent digest applies to the entire message # up to that point h.update(msg[4:]) self.assertEquals(h.digest(), dig2)
def get_sha3_224(self): hash_obj = SHA3_224.new() hash_obj.update(self.value) return { "digest": hash_obj.digest(), "hexdigets": hash_obj.hexdigest() }
def __init__(self, signatureFile, hashAlgorithm, keyLength, privateKeyE): self.signatureFile = open(signatureFile, "w") self.hashAlgorithmName = hashAlgorithm self.keyLength = keyLength self.privateKey = privateKeyE #tuple of (n, e, d) if hashAlgorithm == 'SHA-2-224': self.hashAlgorithm = SHA224.new() elif hashAlgorithm == 'SHA-2-256': self.hashAlgorithm = SHA256.new() elif hashAlgorithm == 'SHA-2-384': self.hashAlgorithm = SHA384.new() elif hashAlgorithm == 'SHA-2-512': self.hashAlgorithm = SHA512.new() elif hashAlgorithm == 'SHA-3-224': self.hashAlgorithm = SHA3_224.new() elif hashAlgorithm == 'SHA-3-256': self.hashAlgorithm = SHA3_256.new() elif hashAlgorithm == 'SHA-2-384': self.hashAlgorithm = SHA3_384.new() elif hashAlgorithm == 'SHA-2-256': self.hashAlgorithm = SHA3_512.new() else: raise Exception("Invalid hash function: " + hashAlgorithm)
def test_update_after_digest(self): msg = b("rrrrttt") # Normally, update() cannot be done after digest() h = SHA3.new(data=msg[:4]) dig1 = h.digest() self.assertRaises(TypeError, h.update, msg[4:]) dig2 = SHA3.new(data=msg).digest() # With the proper flag, it is allowed h = SHA3.new(data=msg[:4], update_after_digest=True) self.assertEqual(h.digest(), dig1) # ... and the subsequent digest applies to the entire message # up to that point h.update(msg[4:]) self.assertEqual(h.digest(), dig2)
def get_sha3_224(data: str) -> dict: """ Returns the SHA3_224 hash value (hex). """ sha3_224_call = SHA3_224.new() sha3_224_call.update(data.encode('utf-8')) return {"SHA3_224_hex": sha3_224_call.hexdigest()}
def switch_dict(size): return { 224: SHA3_224.new(objectToHash.encode('utf-8')), 256: SHA3_256.new(objectToHash.encode('utf-8')), 384: SHA3_384.new(objectToHash.encode('utf-8')), 512: SHA3_512.new(objectToHash.encode('utf-8')) }.get(size, None)
def kex(self, data): if len(data) < 4: return False reqlen, noncelen = struct.unpack_from('<HH', data.peek(4)) if len(data) < 4 + reqlen + noncelen: return False data.drain(4) request = data.read(reqlen) remote_nonce = data.read(noncelen) if self.privkey: response, key = self.encoder.process_kex_request(request, 0) # Add jitter, tinyec is quite horrible time.sleep(random.random()) nonce = get_random_bytes(16) self.downstream.write( struct.pack('<HH', len(response), len(nonce)) + response + nonce) else: key = self.encoder.process_kex_response(request, 0) nonce = self.nonce eh = SHA3_224.new() eh.update(key[0]) eh.update(remote_nonce) ek = eh.digest()[:16] dh = SHA3_224.new() dh.update(key[1]) dh.update(nonce) dk = dh.digest()[:16] self.key = (ek, dk) self.encryptor = AES.new(key=self.key[0], mode=AES.MODE_GCM, nonce=nonce) self.decryptor = AES.new(key=self.key[1], mode=AES.MODE_GCM, nonce=remote_nonce) return True
def update_decryptor(self, vblock): self.decryptor.verify(vblock) h = SHA3_224.new() h.update(self.key[1]) h.update(vblock) self.decryptor = AES.new(key=self.key[1], mode=AES.MODE_GCM, nonce=h.digest())
def testSignature(signatureFile, originalFile, pubFile): pubKeyL, n, e = readPublicKey(pubFile) public_key = RSA.construct((n, e)) verifier = PKCS1_v1_5.new(public_key) fileName, hashed, key, signature = readSignature(signatureFile) (hashing, hashingKeyL) = hashed (enc, encKeyL) = key assert fileName == originalFile assert enc == 'RSA' assert encKeyL == pubKeyL hashAlgorithm = hashing + "-" + str(hashingKeyL) if hashAlgorithm == 'SHA-2-224': hashAlgorithm = SHA224.new() elif hashAlgorithm == 'SHA-2-256': hashAlgorithm = SHA256.new() elif hashAlgorithm == 'SHA-2-384': hashAlgorithm = SHA384.new() elif hashAlgorithm == 'SHA-2-512': hashAlgorithm = SHA512.new() elif hashAlgorithm == 'SHA-3-224': hashAlgorithm = SHA3_224.new() elif hashAlgorithm == 'SHA-3-256': hashAlgorithm = SHA3_256.new() elif hashAlgorithm == 'SHA-2-384': hashAlgorithm = SHA3_384.new() elif hashAlgorithm == 'SHA-2-256': hashAlgorithm = SHA3_512.new() else: raise Exception("Invalid hash function: " + hashAlgorithm) data = open(originalFile, "rb").read() hashAlgorithm.update(data) assert verifier.verify(hashAlgorithm, signature) print("SIGNATURE TEST SUCCESSFUL!!")
def get_hash(msg, hash_size): if hash_size == "224": hash = SHA3_224.new(msg) if hash_size == "256": hash = SHA3_256.new(msg) if hash_size == "384": hash = SHA3_384.new(msg) if hash_size == "512": hash = SHA3_512.new(msg) return hash
def testSeal_verification(signatureFile, envelopeFile, pubFile): pubKeyL, n, e = readPublicKey(pubFile) public_key = RSA.construct((n, e)) verifier = PKCS1_v1_5.new(public_key) fileName, hashed, key, signature = readSignature(signatureFile) (hashing, hashingKeyL) = hashed (enc, encKeyL) = key assert enc == 'RSA' assert encKeyL == pubKeyL hashAlgorithm = hashing + "-" + str(hashingKeyL) if hashAlgorithm == 'SHA-2-224': hashAlgorithm = SHA224.new() elif hashAlgorithm == 'SHA-2-256': hashAlgorithm = SHA256.new() elif hashAlgorithm == 'SHA-2-384': hashAlgorithm = SHA384.new() elif hashAlgorithm == 'SHA-2-512': hashAlgorithm = SHA512.new() elif hashAlgorithm == 'SHA-3-224': hashAlgorithm = SHA3_224.new() elif hashAlgorithm == 'SHA-3-256': hashAlgorithm = SHA3_256.new() elif hashAlgorithm == 'SHA-2-384': hashAlgorithm = SHA3_384.new() elif hashAlgorithm == 'SHA-2-256': hashAlgorithm = SHA3_512.new() else: raise Exception("Invalid hash function: " + hashAlgorithm) fileName, simConf, rsaConf, data, cryptKey, mode, iv = readEnvelope( envelopeFile) hashAlgorithm.update(data + cryptKey) assert verifier.verify(hashAlgorithm, signature) print("SEAL SIGNATURE TEST SUCCESSFUL!!")
def update_encryptor(self): vblock = self.encryptor.digest() h = SHA3_224.new() h.update(self.key[0]) h.update(vblock) self.encryptor = AES.new(key=self.key[0], mode=AES.MODE_GCM, nonce=h.digest()) return vblock
from Crypto.Hash import SHA3_224 import math h_obj = SHA3_224.new() h_obj.update(b'4933601832480809353473354884041175248572605852782963066896748056885475641656749621629491905191014868618662270686970232166446509470324736864650682101529030248099045013028061692922691724625514706329230172429768068340125863618218559912413117007754845075429408372888507551698514494498492001013849289727206925716097505112821640035328796287477288832274302228451772267834762720729040136679341742771582480873438819854901251010698577442809438919694733749250049741106149694027735988626092669792535204681665271762659664614542674529824442558625688825977054069126171946431554677605767056236181410658731273842816008767266250489001' ) print (h_obj.hexdigest()) p=168199388701209853920129085113302407023173962717160229197318545484823101018386724351964316301278642143567435810448472465887143222934545154943005714265124445244247988777471773193847131514083030740407543233616696550197643519458134465700691569680905568000063025830089599260400096259430726498683087138415465107499 k=8 q=959452661475451209325433595634941112150003865821 g=94389192776327398589845326980349814526433869093412782345430946059206568804005181600855825906142967271872548375877738949875812540433223444968461350789461385043775029963900638123183435133537262152973355498432995364505138912569755859623649866375135353179362670798771770711847430626954864269888988371113567502852 r=((g**k)%p)%q print(r) x=432398415306986194693973996870836079581453988813 inverse_mod=359794748053294203497037598363102917056251449683 u=(int(h_obj.hexdigest(),16)-(x*r))*inverse_mod v=int(h_obj.hexdigest(),16) print(v) s=(u%q) print("the value of s:",s)
import random import math import string from Crypto.Hash import SHA3_224 #nonce = random.getrandbits(128) nonce = 23290426 nonce1 = str(nonce).zfill(32) h_amt = SHA3_224.new() h_amt.update(b'1') amt = (int(h_amt.hexdigest(), 16)) print(amt) m1 = 4933601832480809353473354884041175248572605852782963066896748056885475641656749621629491905191014868618662270686970232166446509470324736864650682101529030248099045013028061692922691724625514706329230172429768068340125863618218559912413117007754845075429408372888507551698514494498492001013849289727206925716097505112821640035328796287477288832274302228451772267834762720729040136679341742771582480873438819854901251010698577442809438919694733749250049741106149694027735988626092669792535204681665271762659664614542674529824442558625688825977054069126171946431554677605767056236181410658731273842816008767266250489001 T1 = str(amt) + str(m1) + str(nonce1) print(T1) h_T1 = SHA3_224.new() h_T1.update(T1.encode('utf-16')) print(h_T1.hexdigest()) while (int(h_T1.hexdigest(), 16) >= 0X000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF): nonce += 1 # Increment nonce (ie change your guess) nonce1 = str(nonce).zfill(31) T1 = str(amt) + str(m1) + (nonce1) print(T1) h_T1 = SHA3_224.new()
from Crypto.Hash import SHA3_224 hasher = SHA3_224.new( b"secret message" ) # $ CryptographicOperation CryptographicOperationInput=b"secret message" CryptographicOperationAlgorithm=SHA3224 print(hasher.hexdigest()) hasher = SHA3_224.new( ) # $ CryptographicOperation CryptographicOperationAlgorithm=SHA3224 hasher.update( b"secret" ) # $ CryptographicOperation CryptographicOperationInput=b"secret" CryptographicOperationAlgorithm=SHA3224 hasher.update( b" message" ) # $ CryptographicOperation CryptographicOperationInput=b" message" CryptographicOperationAlgorithm=SHA3224 print(hasher.hexdigest())