1 | from pycryptopp.cipher.xsalsa import XSalsa |
---|
2 | from binascii import hexlify, unhexlify |
---|
3 | from nacl import crypto_stream, crypto_stream_xor |
---|
4 | from random import randint |
---|
5 | import unittest |
---|
6 | import os |
---|
7 | |
---|
8 | key = "\x00"*32 |
---|
9 | iv = "\x00"*24 |
---|
10 | msg = "\x00"*50 |
---|
11 | |
---|
12 | class 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 | |
---|
32 | if __name__ == '__main__': |
---|
33 | unittest.main() |
---|