]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - tests/testmac.py
Begin to implement python3 support. Now tests for oid, bio, cipher, digest, mac and...
[oss/ctypescrypto.git] / tests / testmac.py
1 # -*- encoding: utf-8 -*-
2 from ctypescrypto.oid import Oid
3 from base64 import b16decode,b16encode
4 from ctypescrypto.mac import *
5 from ctypescrypto.engine import set_default
6 import unittest
7
8 class TestMac(unittest.TestCase):
9     def test_hmac_default(self):
10         d=MAC('hmac',key=b'1234'*4)
11         d.update(b'The Quick brown fox jumps over the lazy dog\n')
12         self.assertEqual(d.name,'hmac-md5')
13         self.assertEqual(d.hexdigest(),'A9C16D91CDF2A99273B72336D0D16B56')
14     def test_hmac_digestdataa(self):
15         d=MAC('hmac',key=b'1234'*4)
16         h=d.hexdigest(b'The Quick brown fox jumps over the lazy dog\n')
17         self.assertEqual(d.name,'hmac-md5')
18         self.assertEqual(h,'A9C16D91CDF2A99273B72336D0D16B56')
19     def test_hmac_byoid(self):
20         d=MAC(Oid('hmac'),key=b'1234'*4)
21         d.update(b'The Quick brown fox jumps over the lazy dog\n')
22         self.assertEqual(d.name,'hmac-md5')
23         self.assertEqual(d.hexdigest(),'A9C16D91CDF2A99273B72336D0D16B56')
24     def test_mac_wrongtype(self):
25         with self.assertRaises(TypeError):
26             d=MAC(Oid('hmac').nid,key=b'1234'*4)
27     def test_hmac_sha256(self):
28         d=MAC('hmac',key=b'1234'*16,digest='sha256')
29         d.update(b'The Quick brown fox jumps over the lazy dog\n')
30         self.assertEqual(d.name,'hmac-sha256')
31         self.assertEqual(d.hexdigest(),'BEBA086E1C67200664DCDEEC697D99DB1A8DAA72933A36B708FC5FD568173095')
32     def test_gostmac(self):
33         set_default('gost')
34         d=MAC('gost-mac',key=b'1234'*8)
35         d.update(b'The Quick brown fox jumps over the lazy dog\n')
36         self.assertEqual(d.name,'gost-mac')
37         self.assertEqual(d.digest_size,4)
38         self.assertEqual(d.hexdigest(),'76F25AE3')
39         with self.assertRaisesRegexp(DigestError,"invalid mac key length"):
40             d=MAC('gost-mac',key=b'1234'*4)
41
42 if __name__ == "__main__":
43     unittest.main()