import sha3
from Crypto.Hash import SHA3_224, SHA3_256, SHA3_384, SHA3_512
import random
import string
import unittest


class TestSha3(unittest.TestCase):

    def test_sha3_224(self):
        print("Testing SHA3-224 for 10 000 random vectors in range 0 - 10 000 bytes")
        for i in range(10000):
            percent = i / 100
            if percent.is_integer() and percent != 0: print("Tested for: " + str(i) + " vectors")
            msg = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(i)])
            hash_own = sha3.sha3_224(msg)
            hash_test = SHA3_224.new()
            hash_test.update(str.encode(msg))
            self.assertEqual(hash_own, hash_test.hexdigest(), "Wrong hash! is: " + str(
                hash_own) + ", should be: " + hash_test.hexdigest() + ", input is: " + msg)

    def test_sha3_256(self):
        print("Testing SHA3-256 for 10 000 random vectors in range 0 - 10 000 bytes")
        for i in range(10000):
            percent = i / 100
            if percent.is_integer() and percent != 0: print("Tested for: " + str(i) + " vectors")
            msg = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(i)])
            hash_own = sha3.sha3_256(msg)
            hash_test = SHA3_256.new()
            hash_test.update(str.encode(msg))
            self.assertEqual(hash_own, hash_test.hexdigest(), "Wrong hash! is: " + str(
                hash_own) + ", should be: " + hash_test.hexdigest() + ", input is: " + msg)

    def test_sha3_384(self):
        print("Testing SHA3-384 for 10 000 random vectors in range 0 - 10 000 bytes")
        for i in range(10000):
            percent = i / 100
            if percent.is_integer() and percent != 0: print("Tested for: " + str(i) + " vectors")
            msg = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(i)])
            hash_own = sha3.sha3_384(msg)
            hash_test = SHA3_384.new()
            hash_test.update(str.encode(msg))
            self.assertEqual(hash_own, hash_test.hexdigest(), "Wrong hash! is: " + str(
                hash_own) + ", should be: " + hash_test.hexdigest() + ", input is: " + msg)

    def test_sha3_512(self):
        print("Testing SHA3-512 for 10 000 random vectors in range 0 - 10 000 bytes")
        for i in range(10000):
            percent = i / 100
            if percent.is_integer() and percent != 0: print("Tested for: " + str(i) + " vectors")
            msg = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(i)])
            hash_own = sha3.sha3_512(msg)
            hash_test = SHA3_512.new()
            hash_test.update(str.encode(msg))
            self.assertEqual(hash_own, hash_test.hexdigest(), "Wrong hash! is: " + str(
                hash_own) + ", should be: " + hash_test.hexdigest() + ", input is: " + msg)

if __name__ == '__main__':
    unittest.main()
