Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis cellularmitosis/

Last active Oct 17, 2019
What would you like to do?
Some examples of generating HMACs (SHA256)

Blog 2019/2/1

<- previous | index | next ->

Generating HMACs in Python, Node, and PHP

HMACs came up in a discussion at work so I threw together a few quick demos of how to generate them using the built-in API's of a few langs.

var crypto = require("crypto");
var message = "a message which needs an hmac";
var key = "keyser soze";
var hmac_hex = crypto.createHmac("sha256", key).update(message).digest("hex");
$message = "a message which needs an hmac";
$key = "keyser soze";
$hmac_hex = hash_hmac("sha256", $message, $key);
#!/usr/bin/env python
import hmac
import hashlib
message = "a message which needs an hmac"
key = "keyser soze"
hmac_hex =, message, hashlib.sha256).hexdigest()
print hmac_hex
$ ./
set -e
node demo.js
php demo.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.