From 792ef642fa03eda4af5bc3535487faacef4fcf65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Thu, 15 Sep 2022 08:16:18 +0800 Subject: Nonce string size uses formula Less efficient, but it's more about clarity for now. Variable name shortened a bit. Nonce size made static. --- crypt.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'crypt.h') diff --git a/crypt.h b/crypt.h index f836bcc..5efe114 100644 --- a/crypt.h +++ b/crypt.h @@ -12,15 +12,16 @@ static inline bool crypt_maccmp(const char *key, const char *query, gcry_mac_open(&hd, GCRY_MAC_HMAC_SHA256, GCRY_MAC_FLAG_SECURE, NULL); gcry_mac_setkey(hd, key, strlen(key)); gcry_mac_write(hd, query, strlen(query)); - size_t hmac_sha256_len = 32; - unsigned char hmac_sha256[hmac_sha256_len + 1]; - gcry_mac_read(hd, hmac_sha256, &hmac_sha256_len); + static size_t hmacsha256_len = 32; + unsigned char hmacsha256[hmacsha256_len + 1]; + gcry_mac_read(hd, hmacsha256, &hmacsha256_len); gcry_mac_close(hd); - char hmac_sha256_str[65] = { [0] = '\0' }; - for (int i = 0; i < hmac_sha256_len; i++) - sprintf(hmac_sha256_str, "%s%02x", hmac_sha256_str, - hmac_sha256[i]); - return !strcmp(hmac, hmac_sha256_str); + char hmacsha256_str[hmacsha256_len * 2 + 1]; + hmacsha256_str[0] ='\0'; + for (int i = 0; i < hmacsha256_len; i++) + sprintf(hmacsha256_str, "%s%02x", hmacsha256_str, + hmacsha256[i]); + return !strcmp(hmac, hmacsha256_str); } static inline void crypt_getnonce(char *string, const size_t string_len) -- cgit v1.2.3