Exemple #1
0
    def test_pbkdf2__setup(self):
        pb = PBKDF2('test', 'salt')
        with self.assertRaises(TypeError) as e:
            pb._setup(123, 'salt', 1,
                      pb._pseudorandom(self.u('salt'), self.u('test')))
        self.assertEqual(str(e.exception), "passphrase must be str or unicode")

        with self.assertRaises(TypeError) as e:
            pb._setup('test', 123, 1,
                      pb._pseudorandom(self.u('salt'), self.u('test')))
        self.assertEqual(str(e.exception), "salt must be str or unicode")

        with self.assertRaises(TypeError) as e:
            pb._setup('test', 'salt', '1',
                      pb._pseudorandom(self.u('salt'), self.u('test')))
        self.assertEqual(str(e.exception), "iterations must be an integer")

        with self.assertRaises(ValueError) as e:
            pb._setup('test', 'salt', 0,
                      pb._pseudorandom(self.u('salt'), self.u('test')))
        self.assertEqual(str(e.exception), "iterations must be at least 1")

        with self.assertRaises(TypeError) as e:
            pb._setup('test', 'salt', 1, 'prf')
        self.assertEqual(str(e.exception), "prf must be callable")
Exemple #2
0
 def test_pbkdf2_read(self):
     pb = PBKDF2('test', 'salt')
     pb.closed = True
     with self.assertRaises(ValueError) as e:
         pb.read(8)
     self.assertEqual(str(e.exception), "file-like object is closed")
     pb.closed = False
     self.assertTrue(pb.read(8))
Exemple #3
0
    def test_pbkdf2(self):
        """Module self-test"""
        from binascii import a2b_hex as _a2b_hex

        def a2b_hex(s):
            return _a2b_hex(s.encode('latin-1'))

        #
        # Test vectors from RFC 3962
        #

        # Test 1
        result = PBKDF2("password", "ATHENA.MIT.EDUraeburn", 1, sha1).read(16)
        expected = a2b_hex("cdedb5281bb2f801565a1122b2563515")
        self.assertEqual(expected, result)

        # Test 2
        result = PBKDF2("password", "ATHENA.MIT.EDUraeburn", 1200,
                        sha1).hexread(32)
        expected = ("5c08eb61fdf71e4e4ec3cf6ba1f5512b"
                    "a7e52ddbc5e5142f708a31e2e62b1e13")
        self.assertEqual(expected, result)

        # Test 3
        result = PBKDF2("X" * 64, "pass phrase equals block size", 1200,
                        sha1).hexread(32)
        expected = ("139c30c0966bc32ba55fdbf212530ac9"
                    "c5ec59f1a452f5cc9ad940fea0598ed1")
        self.assertEqual(expected, result)

        # Test 4
        result = PBKDF2("X" * 65, "pass phrase exceeds block size", 1200,
                        sha1).hexread(32)
        expected = ("9ccad6d468770cd51b10e6a68721be61"
                    "1a8b4d282601db3b36be9246915ec82a")
        self.assertEqual(expected, result)

        #
        # Test vectors for PBKDF2 HMAC-SHA1, from RFC 6070
        #

        result = PBKDF2('password', 'salt', 1, sha1).hexread(20)
        expected = '0c60c80f961f0e71f3a9b524af6012062fe037a6'
        self.assertEqual(expected, result)

        #
        # Test vectors for PBKDF2 HMAC-SHA256
        #

        result = PBKDF2('password', 'salt', 1, sha256).hexread(32)
        e = '120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b'
        self.assertEqual(e, result)

        result = PBKDF2('password', 'salt', 2, sha256).hexread(32)
        e = 'ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43'
        self.assertEqual(e, result)

        result = PBKDF2('password', 'salt', 4096, sha256).hexread(32)
        e = 'c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a'
        self.assertEqual(e, result)

        result = PBKDF2('passwordPASSWORDpassword',
                        'saltSALTsaltSALTsaltSALTsaltSALTsalt', 4096,
                        sha256).hexread(40)
        e = '348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd5' + \
            '3e1c635518c7dac47e9'
        self.assertEqual(e, result)

        result = PBKDF2('pass\0word', 'sa\0lt', 4096, sha256).hexread(16)
        expected = '89b69d0516f829893c696226650a8687'
        self.assertEqual(expected, result)

        salt = unhexlify(
            '9290F727ED06C38BA4549EF7DE25CF5642659211B7FC076F2D28FEFD71'
            '784BB8D8F6FB244A8CC5C06240631B97008565A120764C0EE9C2CB0073'
            '994D79080136')
        result = PBKDF2('hello', salt, 10000, sha512).hexread(64)
        expected = (
            '887CFF169EA8335235D8004242AA7D6187A41E3187DF0CE14E256D85ED'
            '97A97357AAA8FF0A3871AB9EEFF458392F462F495487387F685B7472FC'
            '6C29E293F0A0').lower()
        self.assertEqual(expected, result)

        #
        # Other test vectors
        #

        # Chunked read
        f = PBKDF2("kickstart", "workbench", 256, sha1)
        result = f.read(17)
        result += f.read(17)
        result += f.read(1)
        result += f.read(2)
        result += f.read(3)
        expected = PBKDF2("kickstart", "workbench", 256, sha1).read(40)
        self.assertEqual(expected, result)
Exemple #4
0
 def test_pbkdf2___f(self):
     pb = PBKDF2('test', 'salt')
     self.assertTrue(pb._PBKDF2__f(2))
     self.assertRaises(AssertionError, lambda: pb._PBKDF2__f(0))
Exemple #5
0
 def test_pbkdf2_hexread(self):
     pb = PBKDF2('test', 'salt')
     self.assertEqual(pb.hexread(8), 'a7c6a595ae3e3907')
Exemple #6
0
 def test_pbkdf2_close(self):
     pb = PBKDF2('test', 'salt')
     pb.close()
     self.assertTrue(pb.closed)
Exemple #7
0
 def test_pbkdf2_pseudorandom(self):
     pb = PBKDF2('test', 'salt')
     self.assertTrue(pb._pseudorandom(self.u('salt'), self.u('test')))
     pb = PBKDF2('test', 'salt', secret_key='123')
     self.assertTrue(pb._pseudorandom(self.u('salt'), self.u('test')))