HMAC Examples

Data and code examples for AuricVault®-compliant HMAC generation.

  • Encode the generated HMAC as hexadecimal.

  • Force the encoded hexadecimal to lowercase.

  • A hexadecimal-encoded SHA512 HMAC is always 128 characters long.

Input
secret = "vQrHr8%1Dvr5HQ9nv8jGew*y5*UpH7br"
payload = '{"params":[{"mtid":"12321","configurationId":"SAMPLESAMPLE","utcTimestamp":"1474978163","token":"9aO1Kb0qgN0B9Q95100"}],"id":1,"method":"encrypt"}'
Output Snippet (first and last 10 characters)
1caaeeb3dc....83ffd5a925

NodeJS

var crypto = require('crypto');
var hash = crypto.createHmac('sha512', secret);
hash.update(payload);

// Hex digest is lowercase.
var the_hmac = hash.digest('hex');

PHP

// Default output is hexadecimal.
$hmac = hash_hmac('sha512', $payload, $secret);

Python

def calc_hmac(payload, secret):
    calculated_hmac = hmac.new(
        secret.encode("utf-8"),
        payload.encode("utf-8"),
        hashlib.sha512).hexdigest()
    return calculated_hmac.lower()