Beispiel #1
0
    def test_get_cipher(self, Blowfish_new_mock):
        cipher = blowfish_cipher.BlowfishCipher(key="wruff wruff meow",
                                                iv="meow wruff wruff")
        cipher.mode = 'CFB'
        self.assertEqual(cipher._get_cipher(), Blowfish_new_mock.return_value)
        Blowfish_new_mock.assert_called_with("wruff wruff meow",
                                             Blowfish.MODE_CFB,
                                             "meow wruff wruff")

        cipher = blowfish_cipher.BlowfishCipher(key="wruff wruff meow",
                                                iv="meow wruff wruff")
        cipher.mode = 'ECB'
        self.assertEqual(cipher._get_cipher(), Blowfish_new_mock.return_value)
        Blowfish_new_mock.assert_called_with("wruff wruff meow",
                                             Blowfish.MODE_ECB)
Beispiel #2
0
 def test_generate_key(self):
     cipher = blowfish_cipher.BlowfishCipher()
     self.assertEqual(len(cipher.generate_key()), 56)  # test default.
     self.assertEqual(len(cipher.generate_key(4)), 4)
     self.assertEqual(len(cipher.generate_key(24)), 24)
     self.assertEqual(len(cipher.generate_key(32)), 32)
     self.assertRaises(AttributeError, cipher.generate_key, 64)
Beispiel #3
0
 def test_encrypt(self, get_cipher_mock, pad_mock, encode_mock):
     blowfish_cipher_mock = get_cipher_mock.return_value
     cipher = blowfish_cipher.BlowfishCipher()
     self.assertEqual(cipher.encrypt("meow"), encode_mock.return_value)
     get_cipher_mock.assert_called_with()
     pad_mock.assert_called_with("meow", Blowfish.block_size)
     blowfish_cipher_mock.encrypt.assert_called_with(pad_mock.return_value)
     encode_mock.assert_called_with(
         blowfish_cipher_mock.encrypt.return_value)
Beispiel #4
0
 def test_decrypt(self, get_cipher_mock, unpad_mock, decode_mock):
     blowfish_cipher_mock = get_cipher_mock.return_value
     cipher = blowfish_cipher.BlowfishCipher()
     self.assertEqual(cipher.decrypt("wruff"), unpad_mock.return_value)
     get_cipher_mock.assert_called_with()
     decode_mock.assert_called_with("wruff")
     blowfish_cipher_mock.decrypt.assert_called_with(
         decode_mock.return_value)
     unpad_mock.assert_called_with(
         blowfish_cipher_mock.decrypt.return_value)
Beispiel #5
0
    def _test_encryption(self, mode):
        """This will actually execute encrypting/decrypting data.
        Key and IV generation is derived entirely from class staticmethods.
        """
        cipher = blowfish_cipher.BlowfishCipher()
        cipher.mode = mode
        cipher.iv = cipher.generate_iv()
        random_device = random.Random.new()

        # Test various key sizes.
        for key_size in (16, 24, 32):
            cipher.key = cipher.generate_key(key_size)
            util.test_cipher_encryption(self, cipher, random_device.read(2000))

        # Test encoders.
        for encoder in (base_encoders.NullEncoder,
                        binary_encoders.Base64Encoder,
                        binary_encoders.URLSafeBase64Encoder):
            cipher.set_encoding(encoder)
            util.test_cipher_encryption(self, cipher, random_device.read(2000))
Beispiel #6
0
 def test_set_encoding(self):
     cipher = blowfish_cipher.BlowfishCipher()
     self._test_set_encoding(cipher)
Beispiel #7
0
 def test_generate_iv(self):
     cipher = blowfish_cipher.BlowfishCipher()
     self.assertEqual(len(cipher.generate_iv()), Blowfish.block_size)
Beispiel #8
0
    def test_mode_property(self):
        cipher = blowfish_cipher.BlowfishCipher()  # Expects Cipher FeedBack.
        self.assertEqual(cipher._mode.mode_id, Blowfish.MODE_ECB)

        cipher.mode = 'CTR'
        self.assertEqual(cipher._mode.mode_id, Blowfish.MODE_CTR)
Beispiel #9
0
 def test_iv_property(self):
     cipher = blowfish_cipher.BlowfishCipher(iv="wruff!!!", mode='CBC')
     self._test_init_iv(cipher, "wruff!!!")
     self._test_iv_setter(cipher, "wruff!!!", invalid_iv="meow")
Beispiel #10
0
 def test_key_property(self):
     cipher = blowfish_cipher.BlowfishCipher(key="wruff wruff meow")
     self._test_init_key(cipher, "wruff wruff meow")
     self._test_key_setter(cipher,
                           "meow wruff wruff",
                           invalid_key="meow" * 50)
Beispiel #11
0
 def test_init(self):
     cipher = blowfish_cipher.BlowfishCipher()
     self._test_init_no_args(cipher)