Blog | G5 Cyber Security

Secure Server Authentication with HMAC

TL;DR

HMAC (Hash-based Message Authentication Code) provides a strong way to verify that messages from your server are genuine and haven’t been tampered with. This guide explains how to implement it for better security.

What is HMAC?

Imagine you want to be sure a letter you receive is really from the person it says it’s from, and hasn’t been changed in transit. HMAC does something similar for computer messages. It uses a secret key and a hashing algorithm (like SHA256) to create a unique ‘fingerprint’ of the message. Both the sender and receiver know this key.

Why use HMAC for server authentication?

How to Implement HMAC-based Server Authentication

  1. Choose a Secret Key: This is crucially important. It must be:
    • Randomly generated.
    • Long enough (at least 256 bits).
    • Kept secret! Do not hardcode it into your application code. Store it securely, e.g., using environment variables or a secrets management system.
  2. Message Preparation: Combine the data you want to send with other relevant information (timestamp, request ID, etc.). This forms the message that will be authenticated.

    Example:

    message = "request_id=123&timestamp=2024-10-27T10:00:00Z&data=some_sensitive_info"
  3. Calculate the HMAC: Use a cryptographic library in your server code to calculate the HMAC of the message using the secret key.

    Example (Python with hmac and hashlib):

    import hmac
    import hashlib
    
    secret_key = b'YourSecretKey'
    message = b"request_id=123&timestamp=2024-10-27T10:00:00Z&data=some_sensitive_info"
    
    hmac_obj = hmac.new(secret_key, message, hashlib.sha256)
    digest = hmac_obj.hexdigest()
    print(digest)
  4. Send the Message and HMAC: Transmit both the prepared message and the calculated HMAC to the client.
  5. Client-Side Verification: The client receives the message and HMAC. It must:
    • Have access to the same secret key (securely!).
    • Re-calculate the HMAC of the received message using the shared secret key, following the exact same process as the server.
    • Compare the calculated HMAC with the received HMAC. If they match, the message is authentic and hasn’t been tampered with.

    Example (Python):

    import hmac
    import hashlib
    
    secret_key = b'YourSecretKey'
    message = b"request_id=123&timestamp=2024-10-27T10:00:00Z&data=some_sensitive_info"
    received_hmac = "the_hmac_sent_from_server"
    
    hmac_obj = hmac.new(secret_key, message, hashlib.sha256)
    digest = hmac_obj.hexdigest()
    
    if digest == received_hmac:
        print("Message is authentic!")
    else:
        print("Message is invalid!")

Important Considerations

Exit mobile version