diff --git a/src/main/jwt_full/private/crypto/ecdsa.nim b/src/main/jwt_full/private/crypto/ecdsa.nim index 13dc9cb..6f296e2 100644 --- a/src/main/jwt_full/private/crypto/ecdsa.nim +++ b/src/main/jwt_full/private/crypto/ecdsa.nim @@ -96,7 +96,7 @@ proc getEcHashCfg(alg: JwtAlgorithm): HashCfg = else: raise newException(ValueError, "Unsupported ECDSA signature algorithm '" & $alg & "'") - result = HASHES[hashAlg] + result = hashConfig(hashAlg) proc bearEcSign( message: string, diff --git a/src/main/jwt_full/private/crypto/hash.nim b/src/main/jwt_full/private/crypto/hash.nim index fc0062e..4ada3ea 100644 --- a/src/main/jwt_full/private/crypto/hash.nim +++ b/src/main/jwt_full/private/crypto/hash.nim @@ -33,11 +33,37 @@ let HASHES* = newTable[HashAlgorithm, HashCfg]([ size: sha512Size)), ]) +proc hashConfig*(alg: HashAlgorithm): HashCfg = + case alg: + of SHA1: return HashCfg( + alg: SHA1, + vtable: addr sha1Vtable, + oid: HASH_OID_SHA1, + size: sha1Size) + + of SHA256: return HashCfg( + alg: SHA256, + vtable: addr sha256Vtable, + oid: HASH_OID_SHA256, + size: sha256Size) + + of SHA384: return HashCfg( + alg: SHA384, + vtable: addr sha384Vtable, + oid: HASH_OID_SHA384, + size: sha384Size) + + of SHA512: return HashCfg( + alg: SHA512, + vtable: addr sha512Vtable, + oid: HASH_OID_SHA512, + size: sha512Size) + proc hash*(data: string, alg: HashAlgorithm): string = var ctx: HashCompatContext var pCtx = cast[ptr ptr HashClass](addr ctx) - let hashCfg = HASHES[alg] + let hashCfg = hashConfig(alg) result = newString(hashCfg.size) hashCfg.vtable.init(pCtx) hashCfg.vtable.update(pCtx, unsafeAddr data[0], data.len) diff --git a/src/main/jwt_full/private/crypto/rsa.nim b/src/main/jwt_full/private/crypto/rsa.nim index d49053f..865cf47 100644 --- a/src/main/jwt_full/private/crypto/rsa.nim +++ b/src/main/jwt_full/private/crypto/rsa.nim @@ -133,7 +133,7 @@ proc getRsaHashCfg(alg: JwtAlgorithm): HashCfg = else: raise newException(ValueError, "Unsupported RSA signature algorithm '" & $alg & "'") - result = HASHES[hashAlg] + result = hashConfig(hashAlg) proc bearRsaSign( message: string,