From d445dcce6779c6cb47409cf73cf27be93d178e09 Mon Sep 17 00:00:00 2001 From: Siri Hansen Date: Mon, 21 Sep 2020 14:44:26 +0200 Subject: [PATCH] Add version compatibility for crypto --- src/jwt.erl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/jwt.erl b/src/jwt.erl index 3ae1cb5..b17b679 100644 --- a/src/jwt.erl +++ b/src/jwt.erl @@ -13,6 +13,17 @@ -define(HOUR, 3600). -define(DAY, (?HOUR * 24)). +%% Handle version compatibility for crypto +-ifdef(OTP_RELEASE). + -if(?OTP_RELEASE >= 23). + -define(HMAC(Type, Key, Data), crypto:mac(hmac, Type, Key, Data)). + -else. + -define(HMAC(Type, Key, Data), crypto:hmac(Type, Key, Data)). + -endif. +-else. + -define(HMAC(Type, Key, Data), crypto:hmac(Type, Key, Data)). +-endif. + -type expiration() :: {hourly, non_neg_integer()} | {daily, non_neg_integer()} | non_neg_integer(). -type context() :: map(). @@ -271,7 +282,7 @@ jwt_sign(Alg, Payload, Key) -> jwt_sign_with_crypto(algorithm_to_crypto(Alg), Payload, Key). jwt_sign_with_crypto({hmac, Crypto}, Payload, Key) -> - base64url:encode(crypto:hmac(Crypto, Key, Payload)); + base64url:encode(?HMAC(Crypto, Key, Payload)); jwt_sign_with_crypto({Algo, Crypto}, Payload, Pem) when (Algo =:= rsa orelse Algo =:= ecdsa) andalso is_binary(Pem) ->