@@ -60,14 +60,10 @@ def construct(key_data, algorithm=None):
6060 if not algorithm :
6161 raise JWKError ('Unable to find a algorithm for key: %s' % key_data )
6262
63- if algorithm in ALGORITHMS .HMAC :
64- return HMACKey (key_data , algorithm )
65-
66- if algorithm in ALGORITHMS .RSA :
67- return RSAKey (key_data , algorithm )
68-
69- if algorithm in ALGORITHMS .EC :
70- return ECKey (key_data , algorithm )
63+ key_class = ALGORITHMS .get_key (algorithm )
64+ if not key_class :
65+ raise JWKError ('Unable to find a algorithm for key: %s' % key_data )
66+ return key_class (key_data , algorithm )
7167
7268
7369def get_algorithm_object (algorithm ):
@@ -112,13 +108,12 @@ class HMACKey(Key):
112108 SHA256 = hashlib .sha256
113109 SHA384 = hashlib .sha384
114110 SHA512 = hashlib .sha512
115- valid_hash_algs = ALGORITHMS .HMAC
116111
117112 prepared_key = None
118113 hash_alg = None
119114
120115 def __init__ (self , key , algorithm ):
121- if algorithm not in self . valid_hash_algs :
116+ if algorithm not in ALGORITHMS . HMAC :
122117 raise JWKError ('hash_alg: %s is not a valid hash algorithm' % algorithm )
123118 self .hash_alg = get_algorithm_object (algorithm )
124119
@@ -174,14 +169,13 @@ class RSAKey(Key):
174169 SHA256 = Crypto .Hash .SHA256
175170 SHA384 = Crypto .Hash .SHA384
176171 SHA512 = Crypto .Hash .SHA512
177- valid_hash_algs = ALGORITHMS .RSA
178172
179173 prepared_key = None
180174 hash_alg = None
181175
182176 def __init__ (self , key , algorithm ):
183177
184- if algorithm not in self . valid_hash_algs :
178+ if algorithm not in ALGORITHMS . RSA :
185179 raise JWKError ('hash_alg: %s is not a valid hash algorithm' % algorithm )
186180 self .hash_alg = get_algorithm_object (algorithm )
187181
@@ -257,7 +251,6 @@ class ECKey(Key):
257251 SHA256 = hashlib .sha256
258252 SHA384 = hashlib .sha384
259253 SHA512 = hashlib .sha512
260- valid_hash_algs = ALGORITHMS .EC
261254
262255 curve_map = {
263256 SHA256 : ecdsa .curves .NIST256p ,
@@ -270,7 +263,7 @@ class ECKey(Key):
270263 curve = None
271264
272265 def __init__ (self , key , algorithm ):
273- if algorithm not in self . valid_hash_algs :
266+ if algorithm not in ALGORITHMS . EC :
274267 raise JWKError ('hash_alg: %s is not a valid hash algorithm' % algorithm )
275268 self .hash_alg = get_algorithm_object (algorithm )
276269
0 commit comments