Last active
August 29, 2015 14:26
-
-
Save richthegeek/d27f929ffc044d230389 to your computer and use it in GitHub Desktop.
Veoo Subscriber API HMAC generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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