blob: 07e2d8e771920745075a0b3e5930d175d318998a [file] [log] [blame]
Paul Kehrerbde6fb52013-10-18 18:08:49 -05001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14from __future__ import absolute_import, division, print_function
15
Paul Kehrer7a2a1912013-10-20 13:45:23 -050016import pretend
17
Paul Kehrerd4cb34d2013-10-19 23:05:12 -050018import pytest
19
20import six
21
Paul Kehrerdaac6d02013-10-20 13:15:28 -050022from cryptography.bindings import _default_api
23
Paul Kehrerbde6fb52013-10-18 18:08:49 -050024from cryptography.primitives import hashes
25
26from .utils import generate_base_hash_test
27
28
Paul Kehrerd4cb34d2013-10-19 23:05:12 -050029class TestBaseHash(object):
30 def test_base_hash_reject_unicode(self, api):
31 m = hashes.SHA1(api=api)
32 with pytest.raises(TypeError):
33 m.update(six.u("\u00FC"))
34
Paul Kehrer0b2042b2013-10-22 14:28:24 -050035 def test_base_hash_hexdigest_string_type(self, api):
36 m = hashes.SHA1(api=api, data=b"")
37 assert isinstance(m.hexdigest(), str)
38
Paul Kehrerd4cb34d2013-10-19 23:05:12 -050039
Paul Kehrer7a2a1912013-10-20 13:45:23 -050040class TestCopyHash(object):
41 def test_copy_api_object(self):
42 pretend_api = pretend.stub(copy_hash_context=lambda a: "copiedctx")
43 pretend_ctx = pretend.stub()
44 h = hashes.SHA1(api=pretend_api, ctx=pretend_ctx)
45 assert h._api is pretend_api
46 assert h.copy()._api is h._api
47
48
Paul Kehrerdaac6d02013-10-20 13:15:28 -050049class TestDefaultAPISHA1(object):
50 def test_default_api_creation(self):
51 """
52 This test assumes the presence of SHA1 in the default API.
53 """
54 h = hashes.SHA1()
55 assert h._api == _default_api
56
57
Paul Kehrerbde6fb52013-10-18 18:08:49 -050058class TestSHA1(object):
59 test_SHA1 = generate_base_hash_test(
Paul Kehrerbb069c22013-10-18 19:51:01 -050060 hashes.SHA1,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050061 digest_size=20,
62 block_size=64,
Paul Kehrerba3b4712013-10-18 20:53:04 -050063 only_if=lambda api: api.supports_hash(hashes.SHA1),
Paul Kehrerbde6fb52013-10-18 18:08:49 -050064 skip_message="Does not support SHA1",
65 )
Paul Kehrer7e5697c2013-10-18 21:07:36 -050066
67
68class TestSHA224(object):
69 test_SHA224 = generate_base_hash_test(
70 hashes.SHA224,
71 digest_size=28,
72 block_size=64,
73 only_if=lambda api: api.supports_hash(hashes.SHA224),
74 skip_message="Does not support SHA224",
75 )
76
77
78class TestSHA256(object):
79 test_SHA256 = generate_base_hash_test(
80 hashes.SHA256,
81 digest_size=32,
82 block_size=64,
83 only_if=lambda api: api.supports_hash(hashes.SHA256),
84 skip_message="Does not support SHA256",
85 )
86
87
88class TestSHA384(object):
89 test_SHA384 = generate_base_hash_test(
90 hashes.SHA384,
91 digest_size=48,
92 block_size=128,
93 only_if=lambda api: api.supports_hash(hashes.SHA384),
94 skip_message="Does not support SHA384",
95 )
96
97
98class TestSHA512(object):
99 test_SHA512 = generate_base_hash_test(
100 hashes.SHA512,
101 digest_size=64,
102 block_size=128,
103 only_if=lambda api: api.supports_hash(hashes.SHA512),
104 skip_message="Does not support SHA512",
105 )
Paul Kehrerc1794072013-10-18 21:42:57 -0500106
107
108class TestRIPEMD160(object):
109 test_RIPEMD160 = generate_base_hash_test(
110 hashes.RIPEMD160,
111 digest_size=20,
112 block_size=64,
113 only_if=lambda api: api.supports_hash(hashes.RIPEMD160),
114 skip_message="Does not support RIPEMD160",
115 )
Paul Kehrer79ff8bf2013-10-18 22:07:29 -0500116
117
118class TestWhirlpool(object):
119 test_Whirlpool = generate_base_hash_test(
120 hashes.Whirlpool,
121 digest_size=64,
122 block_size=64,
123 only_if=lambda api: api.supports_hash(hashes.Whirlpool),
124 skip_message="Does not support Whirlpool",
125 )
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500126
127
128class TestMD5(object):
129 test_MD5 = generate_base_hash_test(
130 hashes.MD5,
131 digest_size=16,
132 block_size=64,
133 only_if=lambda api: api.supports_hash(hashes.MD5),
134 skip_message="Does not support MD5",
135 )