11import base64
2- import hashlib
32
43from jwt .algorithms import Algorithm , HMACAlgorithm
54
65from .compat import unittest
76from .utils import ensure_bytes , ensure_unicode
87
98try :
10- from cryptography .hazmat .primitives import hashes
119 from jwt .algorithms import RSAAlgorithm , ECAlgorithm
1210
1311 has_crypto = True
@@ -38,7 +36,7 @@ def test_algorithm_should_throw_exception_if_verify_not_impl(self):
3836 algo .verify ('message' , 'key' , 'signature' )
3937
4038 def test_hmac_should_reject_nonstring_key (self ):
41- algo = HMACAlgorithm (hashlib . sha256 () )
39+ algo = HMACAlgorithm (HMACAlgorithm . SHA256 )
4240
4341 with self .assertRaises (TypeError ) as context :
4442 algo .prepare_key (object ())
@@ -47,34 +45,34 @@ def test_hmac_should_reject_nonstring_key(self):
4745 self .assertEqual (str (exception ), 'Expecting a string- or bytes-formatted key.' )
4846
4947 def test_hmac_should_accept_unicode_key (self ):
50- algo = HMACAlgorithm (hashlib . sha256 () )
48+ algo = HMACAlgorithm (HMACAlgorithm . SHA256 )
5149
5250 algo .prepare_key (ensure_unicode ('awesome' ))
5351
5452 @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
5553 def test_rsa_should_parse_pem_public_key (self ):
56- algo = RSAAlgorithm (hashes .SHA256 () )
54+ algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
5755
5856 with open ('tests/keys/testkey2_rsa.pub.pem' , 'r' ) as pem_key :
5957 algo .prepare_key (pem_key .read ())
6058
6159 @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
6260 def test_rsa_should_accept_unicode_key (self ):
63- algo = RSAAlgorithm (hashes .SHA256 () )
61+ algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
6462
6563 with open ('tests/keys/testkey_rsa' , 'r' ) as rsa_key :
6664 algo .prepare_key (ensure_unicode (rsa_key .read ()))
6765
6866 @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
6967 def test_rsa_should_reject_non_string_key (self ):
70- algo = RSAAlgorithm (hashes .SHA256 () )
68+ algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
7169
7270 with self .assertRaises (TypeError ):
7371 algo .prepare_key (None )
7472
7573 @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
7674 def test_rsa_verify_should_return_false_if_signature_invalid (self ):
77- algo = RSAAlgorithm (hashes .SHA256 () )
75+ algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
7876
7977 jwt_message = ensure_bytes ('Hello World!' )
8078
@@ -96,7 +94,7 @@ def test_rsa_verify_should_return_false_if_signature_invalid(self):
9694
9795 @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
9896 def test_rsa_verify_should_return_true_if_signature_valid (self ):
99- algo = RSAAlgorithm (hashes .SHA256 () )
97+ algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
10098
10199 jwt_message = ensure_bytes ('Hello World!' )
102100
@@ -116,21 +114,21 @@ def test_rsa_verify_should_return_true_if_signature_valid(self):
116114
117115 @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
118116 def test_ec_should_reject_non_string_key (self ):
119- algo = ECAlgorithm (hashes .SHA256 () )
117+ algo = ECAlgorithm (ECAlgorithm .SHA256 )
120118
121119 with self .assertRaises (TypeError ):
122120 algo .prepare_key (None )
123121
124122 @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
125123 def test_ec_should_accept_unicode_key (self ):
126- algo = ECAlgorithm (hashes .SHA256 () )
124+ algo = ECAlgorithm (ECAlgorithm .SHA256 )
127125
128126 with open ('tests/keys/testkey_ec' , 'r' ) as ec_key :
129127 algo .prepare_key (ensure_unicode (ec_key .read ()))
130128
131129 @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
132130 def test_ec_verify_should_return_false_if_signature_invalid (self ):
133- algo = ECAlgorithm (hashes .SHA256 () )
131+ algo = ECAlgorithm (ECAlgorithm .SHA256 )
134132
135133 jwt_message = ensure_bytes ('Hello World!' )
136134
@@ -150,7 +148,7 @@ def test_ec_verify_should_return_false_if_signature_invalid(self):
150148
151149 @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
152150 def test_ec_verify_should_return_true_if_signature_valid (self ):
153- algo = ECAlgorithm (hashes .SHA256 () )
151+ algo = ECAlgorithm (ECAlgorithm .SHA256 )
154152
155153 jwt_message = ensure_bytes ('Hello World!' )
156154
0 commit comments