Ticket #40: compare_with_pynacl.py

File compare_with_pynacl.py, 920 bytes (added by warner, at 2012-02-07T04:52:56Z)

compare against pynacl

Line 
1from pycryptopp.cipher.xsalsa import XSalsa
2from binascii import hexlify, unhexlify
3from nacl import crypto_stream, crypto_stream_xor
4from random import randint
5import unittest
6import os
7
8key = "\x00"*32
9iv = "\x00"*24
10msg = "\x00"*50
11
12class CompareWithNACL(unittest.TestCase):
13    def onetest(self, key, iv, msg):
14        expected = hexlify(crypto_stream_xor(msg, iv, key))
15        x = XSalsa(key, iv)
16        offset = 0
17        got = ""
18        while offset < len(msg):
19            size = randint(1, 50)
20            got += hexlify(x.process(msg[offset:offset+size]))
21            offset += size
22        self.failUnlessEqual(expected, got)
23
24    def test_lots(self):
25        for i in range(10000):
26            key = os.urandom(32)
27            iv = os.urandom(24)
28            size = randint(1, 2000)
29            msg = os.urandom(size)
30            self.onetest(key, iv, msg)
31
32if __name__ == '__main__':
33    unittest.main()