I have a web API with a custom API authentication system that users each have a SecretKey and a public ApiKey. Using these two keys client(or user) can generate a token for the authentication on the server.
Consider this function generating an authentication token
public string GetToken(string apiKey, string secretKey, string expireTimestamp)
{
using var hashAlgorithm = SHA256.Create();
var byteValue = Encoding.UTF8.GetBytes(apiKey + secretKey + expireTimestamp)
var byteHash = hashAlgorithm.ComputeHash(byteValue)
return Convert.ToBase64String(byteHash) + expireTimestamp
}
So a token is a combination of
$$token = SHA256(APIKey \mathbin\| SecretKey \mathbin\| expireTimestamp) \mathbin\| expireTimestamp$$
Using this token we have a unique expirable authentication system for our APIs,
questions:
- Should I concatenate keys+expTime or I should use something like HMAC? is this scenario vulnerable to attacks like length extension attacks without HMAC?
- What is the vulnerability of this system (if there is any)?