def test_padding_remove(): ''' Assert that the surrounding whitespace is successfully stripped from string ''' teststr = u'Oh Romeo!' padded = padding_add(teststr) assert len(padding_remove(padded)) == len(teststr) assert padding_remove(padded) == teststr
def test_padding_offset(): ''' Assert that the padding offsets for the same string are different each time The test could fail, but with very low probability. ''' padded = [] teststr = u'LOL' padded.append(padding_add(teststr)) padded.append(padding_add(teststr)) padded.append(padding_add(teststr)) same_strings = 0 if padded[0] == padded[1]: same_strings = same_strings + 1 if padded[0] == padded[2]: same_strings = same_strings + 1 if padded[1] == padded[2]: same_strings = same_strings + 1 assert same_strings < 2
def test_padding_length(): ''' Assert that the padded text is always dividedable by `omemo.padding.PAD_LENGTH` ''' assert len(padding_add(u'Oh Romemo!')) == PAD_LENGTH assert len(padding_add(u'Wherefore art thou Romeo?')) == PAD_LENGTH teststr = u""" JULIET O Romeo, Romeo! wherefore art thou Romeo? Deny thy father and refuse thy name; Or, if thou wilt not, be but sworn my love, And I'll no longer be a Capulet. ROMEO [Aside] Shall I hear more, or shall I speak at this? JULIET 'Tis but thy name that is my enemy; Thou art thyself, though not a Montague. What's Montague? it is nor hand, nor foot, Nor arm, nor face, nor any other part Belonging to a man. O, be some other name! What's in a name? that which we call a rose By any other name would smell as sweet; So Romeo would, were he not Romeo call'd, Retain that dear perfection which he owes Without that title. Romeo, doff thy name, And for that name which is no part of thee Take all myself.""" result = len(padding_add(teststr)) % PAD_LENGTH assert result == 0
def encrypt(plaintext): key = os.urandom(16) iv = os.urandom(16) encoded_plaintext = padding_add(plaintext).encode() return key, iv, aes_encrypt(key, iv, encoded_plaintext)
def encrypt(plaintext): key = os.urandom(16) iv = os.urandom(16) encoded_plaintext = padding_add(plaintext).encode('utf-8') return key, iv, aes_encrypt(key, iv, encoded_plaintext)