Replace globally GC'ed variable with function.

This commit is contained in:
Jonathan Bernard 2021-12-25 21:24:35 -06:00
parent a740d4ce54
commit 60f52cb22f
3 changed files with 29 additions and 3 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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,