Skip to content

Instantly share code, notes, and snippets.

@richthegeek
Last active August 29, 2015 14:26
Show Gist options
  • Save richthegeek/d27f929ffc044d230389 to your computer and use it in GitHub Desktop.
Save richthegeek/d27f929ffc044d230389 to your computer and use it in GitHub Desktop.
Veoo Subscriber API HMAC generation
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public static String generateHMAC(String path, String body, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
return toHexString(mac.doFinal(path.concat(body).getBytes()));
}
var crypto = require('crypto');
encrypt = function (path, body, secret) {
var hmac = crypto.createHmac('sha1', secret);
hmac.update(path.toLowerCase(), 'utf8');
hmac.update(JSON.stringify(body || {}), 'utf8');
return hmac.digest('hex');
}
function encrypt($path, $body, $secret) {
$path = strtolower($path);
$body = is_string($body) ? $body : json_encode($body);
return hash_hmac('sha1', $path . $body, $secret);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment