Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis
Last active October 17, 2019 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cellularmitosis/84d46b2116fcc1ed4efae6b1f476db47 to your computer and use it in GitHub Desktop.
Save cellularmitosis/84d46b2116fcc1ed4efae6b1f476db47 to your computer and use it in GitHub Desktop.
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");
console.log(hmac_hex);
<?
$message = "a message which needs an hmac";
$key = "keyser soze";
$hmac_hex = hash_hmac("sha256", $message, $key);
print_r($hmac_hex);
?>
#!/usr/bin/env python
import hmac
import hashlib
message = "a message which needs an hmac"
key = "keyser soze"
hmac_hex = hmac.new(key, message, hashlib.sha256).hexdigest()
print hmac_hex
$ ./run.sh
f42febfb52f8d2853452b1c835947d70b3e92b1da5ba0c9568b50e531b88bc5b
f42febfb52f8d2853452b1c835947d70b3e92b1da5ba0c9568b50e531b88bc5b
f42febfb52f8d2853452b1c835947d70b3e92b1da5ba0c9568b50e531b88bc5b
#!/bin/sh
set -e
node demo.js
php demo.php
python demo.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment