Truncate HMAC to 16 bytes

This commit is contained in:
akshaya
2024-08-14 11:33:57 -04:00
parent 3f3f1ce9a4
commit ad094f6443
2 changed files with 5 additions and 4 deletions

View File

@@ -25,5 +25,6 @@ proc kdf*(key: openArray[byte]): seq[byte] =
result = hash[0..15]
# This function computes a HMAC for 'data' using given 'key'.
proc hmac*(key, data: openArray[byte]): array[32, byte] =
return sha256.hmac(key, data).data
proc hmac*(key, data: openArray[byte]): seq[byte] =
let hmac = sha256.hmac(key, data).data
result = hmac[0..15]

View File

@@ -53,7 +53,7 @@ suite "cryptographic_functions_tests":
let data: seq[byte] = cast[seq[byte]]("thisisdata")
# Expected HMAC (hexadecimal representation)
let expectedHmacHex = "b075dd302655e085d35e8cef5dfdf101e0701c21bd00baf0e568d5d556c1150c"
let expectedHmacHex = "b075dd302655e085d35e8cef5dfdf101"
let expectedHmac = fromHex(expectedHmacHex)
# Compute HMAC
@@ -106,7 +106,7 @@ suite "cryptographic_functions_tests":
# Define test data
let emptyKey: array[0, byte] = []
let emptyData: array[0, byte] = []
let expectedHmacHex = "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad" # SHA-256 HMAC of empty key and data
let expectedHmacHex = "b613679a0814d9ec772f95d778c35fc5" # SHA-256 HMAC of empty key and data
let expectedHmac = fromHex(expectedHmacHex)
# Compute HMAC