Skip to content

Instantly share code, notes, and snippets.

@robbles
Created July 5, 2013 01:17
Show Gist options
  • Save robbles/5931074 to your computer and use it in GitHub Desktop.
Save robbles/5931074 to your computer and use it in GitHub Desktop.
HMAC signatures for API requests in different languages
var crypto = require('crypto');
var message = 'GET /foo/bar?arg1=aaaa&arg2=bbbb';
var secret = 'secret';
crypto.createHmac('sha256', secret).update(message).digest('base64');
// '82LHSJc4h/5BpLrBcGyWjTIiLuJTBYkgyGAb7cNmXew='
<?php
$message = 'GET /foo/bar?arg1=aaaa&arg2=bbbb';
$secret = 'secret';
$signature = base64_encode(hash_hmac('sha256', $message, $secret, true));
echo $signature;
# 82LHSJc4h/5BpLrBcGyWjTIiLuJTBYkgyGAb7cNmXew=
import hmac
import hashlib
message = 'GET /foo/bar?arg1=aaaa&arg2=bbbb'
secret = 'secret'
hmac.new(key=secret, msg=message, digestmod=hashlib.sha256).digest().encode('base64').strip()
# '82LHSJc4h/5BpLrBcGyWjTIiLuJTBYkgyGAb7cNmXew='
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment