blob: 505f6c8adc6fabc63a017fae94306fa19e536c3f [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 Kehrerd4cb34d2013-10-19 23:05:12 -050016import pytest
17
18import six
19
Paul Kehrerbde6fb52013-10-18 18:08:49 -050020from cryptography.primitives import hashes
21
22from .utils import generate_base_hash_test
23
24
Paul Kehrerd4cb34d2013-10-19 23:05:12 -050025class TestBaseHash(object):
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050026 def test_base_hash_reject_unicode(self, backend):
27 m = hashes.SHA1(backend=backend)
Paul Kehrerd4cb34d2013-10-19 23:05:12 -050028 with pytest.raises(TypeError):
29 m.update(six.u("\u00FC"))
30
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050031 def test_base_hash_hexdigest_string_type(self, backend):
32 m = hashes.SHA1(backend=backend, data=b"")
Paul Kehrer0b2042b2013-10-22 14:28:24 -050033 assert isinstance(m.hexdigest(), str)
34
Paul Kehrerd4cb34d2013-10-19 23:05:12 -050035
Paul Kehrerbde6fb52013-10-18 18:08:49 -050036class TestSHA1(object):
37 test_SHA1 = generate_base_hash_test(
Paul Kehrerbb069c22013-10-18 19:51:01 -050038 hashes.SHA1,
Paul Kehrerbde6fb52013-10-18 18:08:49 -050039 digest_size=20,
40 block_size=64,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050041 only_if=lambda backend: backend.supports_hash(hashes.SHA1),
Paul Kehrerbde6fb52013-10-18 18:08:49 -050042 skip_message="Does not support SHA1",
43 )
Paul Kehrer7e5697c2013-10-18 21:07:36 -050044
45
46class TestSHA224(object):
47 test_SHA224 = generate_base_hash_test(
48 hashes.SHA224,
49 digest_size=28,
50 block_size=64,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050051 only_if=lambda backend: backend.supports_hash(hashes.SHA224),
Paul Kehrer7e5697c2013-10-18 21:07:36 -050052 skip_message="Does not support SHA224",
53 )
54
55
56class TestSHA256(object):
57 test_SHA256 = generate_base_hash_test(
58 hashes.SHA256,
59 digest_size=32,
60 block_size=64,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050061 only_if=lambda backend: backend.supports_hash(hashes.SHA256),
Paul Kehrer7e5697c2013-10-18 21:07:36 -050062 skip_message="Does not support SHA256",
63 )
64
65
66class TestSHA384(object):
67 test_SHA384 = generate_base_hash_test(
68 hashes.SHA384,
69 digest_size=48,
70 block_size=128,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050071 only_if=lambda backend: backend.supports_hash(hashes.SHA384),
Paul Kehrer7e5697c2013-10-18 21:07:36 -050072 skip_message="Does not support SHA384",
73 )
74
75
76class TestSHA512(object):
77 test_SHA512 = generate_base_hash_test(
78 hashes.SHA512,
79 digest_size=64,
80 block_size=128,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050081 only_if=lambda backend: backend.supports_hash(hashes.SHA512),
Paul Kehrer7e5697c2013-10-18 21:07:36 -050082 skip_message="Does not support SHA512",
83 )
Paul Kehrerc1794072013-10-18 21:42:57 -050084
85
86class TestRIPEMD160(object):
87 test_RIPEMD160 = generate_base_hash_test(
88 hashes.RIPEMD160,
89 digest_size=20,
90 block_size=64,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -050091 only_if=lambda backend: backend.supports_hash(hashes.RIPEMD160),
Paul Kehrerc1794072013-10-18 21:42:57 -050092 skip_message="Does not support RIPEMD160",
93 )
Paul Kehrer79ff8bf2013-10-18 22:07:29 -050094
95
96class TestWhirlpool(object):
97 test_Whirlpool = generate_base_hash_test(
98 hashes.Whirlpool,
99 digest_size=64,
100 block_size=64,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500101 only_if=lambda backend: backend.supports_hash(hashes.Whirlpool),
Paul Kehrer79ff8bf2013-10-18 22:07:29 -0500102 skip_message="Does not support Whirlpool",
103 )
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500104
105
106class TestMD5(object):
107 test_MD5 = generate_base_hash_test(
108 hashes.MD5,
109 digest_size=16,
110 block_size=64,
Paul Kehrerdb37d0e2013-10-22 20:13:06 -0500111 only_if=lambda backend: backend.supports_hash(hashes.MD5),
Paul Kehrer36e7d0d2013-10-18 18:54:40 -0500112 skip_message="Does not support MD5",
113 )