Пример #1
0
    def test_Functionality(self):
        key = base64.urlsafe_b64encode(os.urandom(16))
        lpc = LengthPreservingCipher(key, 10)

        # decrypt(encrypt(msg)) == msg
        for i in xrange(20):
            msg = os.urandom(6)
            assert lpc.decrypt(lpc.encrypt(msg)) == msg
Пример #2
0
 def test_EnsureConsistentLength(self):
     key = base64.urlsafe_b64encode(os.urandom(16))
     lpc = LengthPreservingCipher(key, length = 5)
     for i in xrange(1,20):
         msg = os.urandom(i)
         try: 
             lpc.encrypt(msg)
         # expected to raise AssertionError because the length is inconsistent.
         # if raise, pass the test and return
         except AssertionError:
             pass
             return
         # otherwise fail the test    
         pytest.fail("Data length must be consistent with the length given during the instantiation of LengthPreservingCipher.")
Пример #3
0
 def test_Functionality(self):
     key = base64.urlsafe_b64encode(os.urandom(16))
     lpc = LengthPreservingCipher(key, length=5)
     for i in xrange(20):
         msg = os.urandom(5)
         assert lpc.decrypt(lpc.encrypt(msg)) == msg
Пример #4
0
 def setup(self):
     key = base64.urlsafe_b64encode('1234567890abcdef')        
     self._lpc5 = LengthPreservingCipher(key, 5)
Пример #5
0
class TestLengthPreservingCipher:
    def setup(self):
        key = base64.urlsafe_b64encode('1234567890abcdef')        
        self._lpc5 = LengthPreservingCipher(key, 5)

    def test_basic(self):
        self.setup()
        txt = b'abcde'
        ctx = self._lpc5.encrypt(txt)
        ttxt = self._lpc5.decrypt(ctx)
        assert ttxt == txt

    def test_random_msg(self):
        self.setup()
        for i in xrange(101):
            txt = os.urandom(5)
            ctx = self._lpc5.encrypt(txt)
            ttxt = self._lpc5.decrypt(ctx)
            assert txt == ttxt, "Input and out messages do not match. {:02X} != {!02X}".format(txt, ttxt)

    def test_wrong_length_encrpyt(self):
        self.setup()
        with pytest.raises(AssertionError) as e:
            self._lpc5.encrypt('a')
        with pytest.raises(AssertionError) as e:
            self._lpc5.encrypt('a'*6)

    def test_wrong_length_decrypt(self):
        self.setup()
        msg = 'a'
        with pytest.raises(AssertionError) as e:
            self._lpc5.decrypt('a')
        with pytest.raises(AssertionError) as e:
            self._lpc5.decrypt('a'*6)

    def test_length_of_cipher(self):
        self.setup()
        txt = b'abcde'
        ctx = self._lpc5.encrypt(txt)
        assert len(ctx) == len(txt)