Ejemplo n.º 1
0
    def test_basics(self):
        sha3 = self.new()
        self.assertEqual(sha3.name, self.name)
        self.assertEqual(sha3.digest_size, self.digest_size)
        self.assertEqual(sha3._capacity_bits + sha3._rate_bits, 1600)
        self.assertEqual(sha3._rate_bits, self.rate_bits)
        self.assertEqual(sha3._capacity_bits, self.capacity_bits)
        if HMAC_SUPPORT:
            if self.block_size is not None:
                self.assertEqual(sha3.block_size, self.block_size)
            else:
                self.assertRaises(TypeError, getattr, sha3, "block_size")
        else:
            self.assertEqual(sha3.block_size, NotImplemented)
        self.assertEqual(len(sha3.digest()), self.digest_size)
        self.assertEqual(len(sha3.hexdigest()), self.digest_size * 2)

        self.assertRaises(AttributeError, setattr, sha3, "digest", 3)
        self.assertRaises(AttributeError, setattr, sha3, "name", "egg")

        self.new(b"data")
        self.new(string=b"data")
        self.assertRaises(TypeError, self.new, None)
        self.assertRaises(TypeError, sha3.update, None)
        self.assertRaises(TypeError, self.new, asunicode("text"))
        self.assertRaises(TypeError, sha3.update, asunicode("text"))

        sha3type = type(sha3)
        self.assertEqual(sha3type.__name__, "SHA3")
        self.assertEqual(sha3type.__module__, "_sha3")
        self.assertRaises(TypeError, sha3type)
        self.assertRaises(TypeError, type, sha3type, "subclass", {})
Ejemplo n.º 2
0
def print_hashes(fname):
    if hashcnt == 1:
        if args.md5:
            print(md5.hexdigest() + '\t' + fname)
        elif args.sha1:
            print(sha1.hexdigest() + '\t' + fname)
        elif args.sha256:
            print(sha256.hexdigest() + '\t' + fname)
        elif args.sha3_224:
            print(sha3_224.hexdigest() + '\t' + fname)
        elif args.sha3:
            print(sha3.hexdigest() + '\t' + fname)
        elif args.sha512:
            print(
                codecs.decode(base64.b64encode(sha512.digest())) + '\t' +
                fname)
    elif args.psv:
        if args.md5 or args.all:
            sys.stdout.write(md5.hexdigest() + '|')
        if args.sha1 or args.all:
            sys.stdout.write(sha1.hexdigest() + '|')
        if args.sha256 or args.all:
            sys.stdout.write(sha256.hexdigest() + '|')
        if args.sha512 or args.all:
            sys.stdout.write(
                codecs.decode(base64.b64encode(sha512.digest())) + '|')
        if args.sha3_224 or args.all:
            sys.stdout.write(sha3_224.hexdigest() + '|')
        if args.sha3 or args.all:
            sys.stdout.write(sha3.hexdigest() + '|')
        print(fname)
    else:
        print(fname + ":")
        if args.md5 or args.all:
            print('  MD5:  ' + md5.hexdigest())
        if args.sha1 or args.all:
            print('  SHA1: ' + sha1.hexdigest())
        if args.sha256 or args.all:
            print('  SHA256: ' + sha256.hexdigest())
        if args.sha512 or args.all:
            print('  SHA512: ' +
                  codecs.decode(base64.b64encode(sha512.digest())))
        if args.sha3_224 or args.all:
            print('  SHA3-224: ' + sha3_224.hexdigest())
        if args.sha3 or args.all:
            print('  SHA3-384: ' + sha3.hexdigest())
Ejemplo n.º 3
0
    def test_basics(self):
        sha3 = self.new()
        self.assertEqual(sha3.name, self.name)
        self.assertEqual(sha3.digest_size, self.digest_size)
        self.assertEqual(sha3._capacity_bits + sha3._rate_bits, 1600)
        self.assertEqual(sha3._rate_bits, self.rate_bits)
        self.assertEqual(sha3._capacity_bits, self.capacity_bits)
        if self.block_size is not None:
            self.assertEqual(sha3.block_size, self.block_size)

        if self.shake:
            self.assertEqual(len(sha3.digest(4)), 4)
            self.assertEqual(len(sha3.hexdigest(4)), 8)
            self.assertEqual(len(sha3.digest(8)), 8)
            self.assertEqual(len(sha3.hexdigest(8)), 16)
            self.assertEqual(len(sha3.digest(97)), 97)
            self.assertEqual(len(sha3.hexdigest(97)), 194)
        else:
            self.assertEqual(len(sha3.digest()), self.digest_size)
            self.assertEqual(len(sha3.hexdigest()), self.digest_size * 2)

        # object is read-only(PyPy behavior is different here)
        if platform.python_implementation() != "PyPy":
            self.assertRaises(AttributeError, setattr, sha3, "attribute", None)
        if (platform.python_implementation() == "PyPy" and
                platform.python_version_tuple()[0] == "2"):
            self.assertRaises(TypeError, setattr, sha3, "digest_size", 3)
            self.assertRaises(TypeError, setattr, sha3, "name", "egg")
        else:
            self.assertRaises(AttributeError, setattr, sha3, "digest_size", 3)
            self.assertRaises(AttributeError, setattr, sha3, "name", "egg")

        self.new(b"data")
        self.new(string=b"data")
        self.assertRaises(TypeError, self.new, None)
        self.assertRaises(TypeError, sha3.update, None)
        self.assertRaises(TypeError, self.new, asunicode("text"))
        self.assertRaises(TypeError, sha3.update, asunicode("text"))

        sha3type = type(sha3)
        self.assertEqual(sha3type.__name__, self.name)
        self.assertEqual(sha3type.__module__, "_pysha3")
        self.assertIsInstance(sha3type(), sha3type)
        self.assertIs(sha3type, self.new)
        self.assertRaises(TypeError, type, sha3type, "subclass", {})
Ejemplo n.º 4
0
    def assertHashDigest(self, hexmsg, hexdigest):
        hexdigest = hexdigest.lower()
        msg = fromhex(hexmsg)
        digest = fromhex(hexdigest)
        self.assertEqual(len(digest), self.digest_size)

        sha3 = self.new(msg)
        self.assertEqual(sha3.hexdigest(), hexdigest)
        self.assertEqual(sha3.digest(), digest)

        sha3 = self.new()
        sha3.update(msg)
        self.assertEqual(sha3.hexdigest(), hexdigest)
        self.assertEqual(sha3.digest(), digest)

        sha3 = self.new()
        for b in msg:
            sha3.update(tobyte(b))
        self.assertEqual(sha3.hexdigest(), hexdigest)
        self.assertEqual(sha3.digest(), digest)
Ejemplo n.º 5
0
    def assertHashDigest(self, hexmsg, hexdigest):
        hexdigest = hexdigest.lower()
        msg = fromhex(hexmsg)
        digest = fromhex(hexdigest)
        self.assertEqual(len(digest), self.digest_size)

        sha3 = self.new(msg)
        self.assertEqual(sha3.hexdigest(), hexdigest)
        self.assertEqual(sha3.digest(), digest)

        sha3 = self.new()
        sha3.update(msg)
        self.assertEqual(sha3.hexdigest(), hexdigest)
        self.assertEqual(sha3.digest(), digest)

        sha3 = self.new()
        for b in msg:
            sha3.update(tobyte(b))
        self.assertEqual(sha3.hexdigest(), hexdigest)
        self.assertEqual(sha3.digest(), digest)
Ejemplo n.º 6
0
    def test_basics(self):
        sha3 = self.new()
        self.assertEqual(sha3.name, self.name)
        self.assertEqual(sha3.digest_size, self.digest_size)
        self.assertEqual(sha3._capacity_bits + sha3._rate_bits, 1600)
        self.assertEqual(sha3._rate_bits, self.rate_bits)
        self.assertEqual(sha3._capacity_bits, self.capacity_bits)
        if self.block_size is not None:
            self.assertEqual(sha3.block_size, self.block_size)

        if self.shake:
            self.assertEqual(len(sha3.digest(4)), 4)
            self.assertEqual(len(sha3.hexdigest(4)), 8)
            self.assertEqual(len(sha3.digest(8)), 8)
            self.assertEqual(len(sha3.hexdigest(8)), 16)
            self.assertEqual(len(sha3.digest(97)), 97)
            self.assertEqual(len(sha3.hexdigest(97)), 194)
        else:
            self.assertEqual(len(sha3.digest()), self.digest_size)
            self.assertEqual(len(sha3.hexdigest()), self.digest_size * 2)

        # object is read-only
        self.assertRaises(AttributeError, setattr, sha3, "attribute", None)
        self.assertRaises(AttributeError, setattr, sha3, "digest_size", 3)
        self.assertRaises(AttributeError, setattr, sha3, "name", "egg")

        self.new(b"data")
        self.new(string=b"data")
        self.assertRaises(TypeError, self.new, None)
        self.assertRaises(TypeError, sha3.update, None)
        self.assertRaises(TypeError, self.new, asunicode("text"))
        self.assertRaises(TypeError, sha3.update, asunicode("text"))

        sha3type = type(sha3)
        self.assertEqual(sha3type.__name__, self.name)
        self.assertEqual(sha3type.__module__, "_pysha3")
        self.assertIsInstance(sha3type(), sha3type)
        self.assertIs(sha3type, self.new)
        self.assertRaises(TypeError, type, sha3type, "subclass", {})
Ejemplo n.º 7
0
    def test_vectors_unaligned(self):
        for hexmsg, hexdigest in self.vectors:
            hexdigest = hexdigest.lower()
            msg = fromhex(hexmsg)
            digest = fromhex(hexdigest)
            for i in range(1, 15):
                msg2 = i * b"\x00" + msg
                unaligned = memoryview(msg2)[i:]
                self.assertEqual(unaligned, msg)

                sha3 = self.new(unaligned)
                self.assertEqual(sha3.hexdigest(), hexdigest)
                self.assertEqual(sha3.digest(), digest)
Ejemplo n.º 8
0
    def test_basics(self):
        sha3 = self.new()
        self.assertEqual(sha3.name, self.name)
        self.assertEqual(sha3.digest_size, self.digest_size)
        self.assertEqual(sha3._capacity_bits + sha3._rate_bits, 1600)
        self.assertEqual(sha3._rate_bits, self.rate_bits)
        self.assertEqual(sha3._capacity_bits, self.capacity_bits)
        if self.block_size is not None:
            self.assertEqual(sha3.block_size, self.block_size)

        if self.shake:
            self.assertEqual(len(sha3.digest(4)), 4)
            self.assertEqual(len(sha3.hexdigest(4)), 8)
            self.assertEqual(len(sha3.digest(8)), 8)
            self.assertEqual(len(sha3.hexdigest(8)), 16)
            self.assertEqual(len(sha3.digest(97)), 97)
            self.assertEqual(len(sha3.hexdigest(97)), 194)
        else:
            self.assertEqual(len(sha3.digest()), self.digest_size)
            self.assertEqual(len(sha3.hexdigest()), self.digest_size * 2)

        # object is read-only
        self.assertRaises(AttributeError, setattr, sha3, "attribute", None)
        self.assertRaises(AttributeError, setattr, sha3, "digest_size", 3)
        self.assertRaises(AttributeError, setattr, sha3, "name", "egg")

        self.new(b"data")
        self.new(string=b"data")
        self.assertRaises(TypeError, self.new, None)
        self.assertRaises(TypeError, sha3.update, None)
        self.assertRaises(TypeError, self.new, asunicode("text"))
        self.assertRaises(TypeError, sha3.update, asunicode("text"))

        sha3type = type(sha3)
        self.assertEqual(sha3type.__name__, self.name)
        self.assertEqual(sha3type.__module__, "_pysha3")
        self.assertIsInstance(sha3type(), sha3type)
        self.assertIs(sha3type, self.new)
        self.assertRaises(TypeError, type, sha3type, "subclass", {})
Ejemplo n.º 9
0
    def test_vectors_unaligned(self):
        for hexmsg, hexdigest in self.vectors:
            hexdigest = hexdigest.lower()
            msg = fromhex(hexmsg)
            digest = fromhex(hexdigest)
            for i in range(1, 15):
                msg2 = i * b"\x00" + msg
                unaligned = memoryview(msg2)[i:]
                self.assertEqual(unaligned, msg)

                sha3 = self.new(unaligned)
                self.assertEqual(sha3.hexdigest(), hexdigest)
                self.assertEqual(sha3.digest(), digest)
Ejemplo n.º 10
0
 def test_kat(self):
     name = self.name.split("_")[1]
     katname = os.path.join("kat", "*%s.txt" % name)
     kats = glob(katname)
     self.assertEqual(len(kats), 2)
     for kat in kats:
         for hexmsg, hexdigest in katparser(kat):
             hexdigest = hexdigest.lower()
             msg = fromhex(hexmsg)
             digest = fromhex(hexdigest)
             self.assertEqual(len(digest), self.digest_size)
             sha3 = self.new(msg)
             self.assertEqual(sha3.hexdigest(), hexdigest)
             self.assertEqual(sha3.digest(), digest)
Ejemplo n.º 11
0
    def test_basics(self):
        sha3 = self.new()
        self.assertEqual(sha3.name, self.name)
        self.assertEqual(sha3.digest_size, self.digest_size)
        self.assertEqual(sha3.block_size, self.block_size)
        self.assertEqual(len(sha3.digest()), self.digest_size)
        self.assertEqual(len(sha3.hexdigest()), self.digest_size * 2)

        self.assertRaises(AttributeError, setattr, sha3, "digest", 3)
        self.assertRaises(AttributeError, setattr, sha3, "name", "egg")

        self.new(b"data")
        self.new(string=b"data")
        self.assertRaises(TypeError, self.new, None)
        self.assertRaises(TypeError, sha3.update, None)
        self.assertRaises(TypeError, self.new, asunicode("text"))
        self.assertRaises(TypeError, sha3.update, asunicode("text"))

        sha3type = type(sha3)
        self.assertEqual(sha3type.__name__, "SHA3")
        self.assertEqual(sha3type.__module__, "_sha3")
        self.assertRaises(TypeError, sha3type)
        self.assertRaises(TypeError, type, sha3type, "subclass", {})
Ejemplo n.º 12
0
    def test_basics(self):
        sha3 = self.new()
        self.assertEqual(sha3.name, self.name)
        self.assertEqual(sha3.digest_size, self.digest_size)
        self.assertEqual(sha3.block_size, self.block_size)
        self.assertEqual(len(sha3.digest()), self.digest_size)
        self.assertEqual(len(sha3.hexdigest()), self.digest_size * 2)

        self.assertRaises(AttributeError, setattr, sha3, "digest", 3)
        self.assertRaises(AttributeError, setattr, sha3, "name", "egg")

        self.new(b"data")
        self.new(string=b"data")
        self.assertRaises(TypeError, self.new, None)
        self.assertRaises(TypeError, sha3.update, None)
        self.assertRaises(TypeError, self.new, asunicode("text"))
        self.assertRaises(TypeError, sha3.update, asunicode("text"))

        sha3type = type(sha3)
        self.assertEqual(sha3type.__name__, "SHA3")
        self.assertEqual(sha3type.__module__, "_sha3")
        self.assertRaises(TypeError, sha3type)
        self.assertRaises(TypeError, type, sha3type, "subclass", {})
Ejemplo n.º 13
0
import sha3

#define the buffer size - how much of a file is read into memory at a time, allows for larger file sizes
BUF_SIZE = 65536

#declaring our hashing algorithms
sha1 = hashlib.sha1()
sha256 = hashlib.sha256()
sha512 = hashlib.sha512()
sha3 = hashlib.sha3_512()

#opens the command line argument as a file
with open(sys.argv[1], 'rb') as file:
    while True:
        #reads the contents of the file to the size of the buffer at a time
        data = file.read(BUF_SIZE)
        #breaks out of the loop if there's no data
        if not data:
            break
        #gets hash of the file
        sha1.update(data)
        sha256.update(data)
        sha512.update(data)
        sha3.update(data)

#prints out the has of the file
print("SHA1: {0}".format(sha1.hexdigest()))
print("SHA-256: {0}".format(sha256.hexdigest()))
print("SHA-512: {0}".format(sha512.hexdigest()))
print("SHA3: {0}".format(sha3.hexdigest()))