summaryrefslogtreecommitdiff
path: root/jwt/algorithms.py
diff options
context:
space:
mode:
authorMark Adams <mark@markadams.me>2015-03-15 20:20:05 -0500
committerMark Adams <mark@markadams.me>2015-03-15 20:27:35 -0500
commitd9727ca98a9cad1f749f41fb2207b269b6ae5660 (patch)
tree5fd92170f38a2876dd9aeb8364994869f5913f5e /jwt/algorithms.py
parent2d0e8272dbd1372289bff1b8e8eba446bed4befa (diff)
downloadpyjwt-d9727ca98a9cad1f749f41fb2207b269b6ae5660.tar.gz
Made algorithm class dependence on hash functions more direct.
- Algorithms now have SHA256, SHA384, and SHA512 static properties that refer to the callable that instantiates their hash class - All algorithms now expect a class (callable) as their hash_alg now. This behavior was inconsistent before.
Diffstat (limited to 'jwt/algorithms.py')
-rw-r--r--jwt/algorithms.py28
1 files changed, 17 insertions, 11 deletions
diff --git a/jwt/algorithms.py b/jwt/algorithms.py
index d347dcc..d3bc11d 100644
--- a/jwt/algorithms.py
+++ b/jwt/algorithms.py
@@ -23,18 +23,18 @@ def _register_default_algorithms():
Registers the algorithms that are implemented by the library.
"""
register_algorithm('none', NoneAlgorithm())
- register_algorithm('HS256', HMACAlgorithm(hashlib.sha256))
- register_algorithm('HS384', HMACAlgorithm(hashlib.sha384))
- register_algorithm('HS512', HMACAlgorithm(hashlib.sha512))
+ register_algorithm('HS256', HMACAlgorithm(HMACAlgorithm.SHA256))
+ register_algorithm('HS384', HMACAlgorithm(HMACAlgorithm.SHA384))
+ register_algorithm('HS512', HMACAlgorithm(HMACAlgorithm.SHA512))
if has_crypto:
- register_algorithm('RS256', RSAAlgorithm(hashes.SHA256()))
- register_algorithm('RS384', RSAAlgorithm(hashes.SHA384()))
- register_algorithm('RS512', RSAAlgorithm(hashes.SHA512()))
+ register_algorithm('RS256', RSAAlgorithm(RSAAlgorithm.SHA256))
+ register_algorithm('RS384', RSAAlgorithm(RSAAlgorithm.SHA384))
+ register_algorithm('RS512', RSAAlgorithm(RSAAlgorithm.SHA512))
- register_algorithm('ES256', ECAlgorithm(hashes.SHA256()))
- register_algorithm('ES384', ECAlgorithm(hashes.SHA384()))
- register_algorithm('ES512', ECAlgorithm(hashes.SHA512()))
+ register_algorithm('ES256', ECAlgorithm(ECAlgorithm.SHA256))
+ register_algorithm('ES384', ECAlgorithm(ECAlgorithm.SHA384))
+ register_algorithm('ES512', ECAlgorithm(ECAlgorithm.SHA512))
class Algorithm(object):
@@ -86,6 +86,8 @@ class HMACAlgorithm(Algorithm):
def __init__(self, hash_alg):
self.hash_alg = hash_alg
+ SHA256, SHA384, SHA512 = hashlib.sha256, hashlib.sha384, hashlib.sha512
+
def prepare_key(self, key):
if not isinstance(key, string_types) and not isinstance(key, bytes):
raise TypeError('Expecting a string- or bytes-formatted key.')
@@ -110,7 +112,9 @@ if has_crypto:
"""
def __init__(self, hash_alg):
- self.hash_alg = hash_alg
+ self.hash_alg = hash_alg()
+
+ SHA256, SHA384, SHA512 = hashes.SHA256, hashes.SHA384, hashes.SHA512
def prepare_key(self, key):
if isinstance(key, interfaces.RSAPrivateKey) or \
@@ -163,7 +167,9 @@ if has_crypto:
ECDSA and the specified hash function
"""
def __init__(self, hash_alg):
- self.hash_alg = hash_alg
+ self.hash_alg = hash_alg()
+
+ SHA256, SHA384, SHA512 = hashes.SHA256, hashes.SHA384, hashes.SHA512
def prepare_key(self, key):
if isinstance(key, interfaces.EllipticCurvePrivateKey) or \