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 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_384(self): hash_obj = SHA3_384.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 get_sha3_384(data: str) -> dict: """ Returns the SHA3_384 hash value (hex). """ sha3_384_call = SHA3_384.new() sha3_384_call.update(data.encode('utf-8')) return {"SHA3_384_hex": sha3_384_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 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 get_login_token(self, key, user, action=3): try: key = bytes.fromhex(key) key1 = key[0:48] key2 = key[48:] msg = '{{"a":{}, "u":"{}","time":{}}}'.format( action, user, int(time.time())) iv = get_random_bytes(16) # sha h = SHA3_384.new() h.update(key1) hashed_msg = h.digest() + msg.encode() # aes cipher = AES.new(key2, AES.MODE_CBC, iv) msg = cipher.encrypt(pad(hashed_msg, 16)) return base64.b64encode(iv + msg, altchars=b"@$").decode("utf-8") except Exception: return "err"
def doSHA(vectors, length, version): counter = 0 addition = 0 for vector in vectors: counter += 1 data = bytes.fromhex( vector) #vector is converted from hex to a binary object if version == 2: if length == 384: start = timer() #begin time measure h = SHA384.new() h.update(data) #message is hashed end = timer() #end time measure #print(end-start) #total execution time addition += end - start elif length == 512: start = timer() #begin time measure h = SHA512.new() h.update(data) #message is hashed end = timer() #end time measure #print(end-start) #total execution time addition += end - start #print(h.hexdigest()) elif version == 3: if length == 384: start = timer() #begin time measure h = SHA3_384.new() h.update(data) #message is hashed end = timer() #end time measure #print(end-start) #total execution time addition += end - start elif length == 512: start = timer() #begin time measure h = SHA3_512.new() h.update(data) #message is hashed end = timer() #end time measure #print(end-start) #total execution time addition += end - start #print(h.hexdigest()) return addition / counter