Created
December 12, 2012 13:25
-
-
Save soulfly/4267705 to your computer and use it in GitHub Desktop.
Java HMAC_SHA1_ALGORITHM
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
import javax.crypto.Mac; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.security.SignatureException; | |
/** | |
* @author Igor Khomenko | |
* injoit.com | |
* | |
* This class defines common routines for generating | |
* authentication signatures for requests. | |
*/ | |
public class Signature { | |
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; | |
/** | |
* Computes RFC 2104-compliant HMAC signature. | |
* * @param data | |
* The data to be signed. | |
* @param key | |
* The signing key. | |
* @return | |
* The Base64-encoded RFC 2104-compliant HMAC signature. | |
* @throws | |
* java.security.SignatureException when signature generation fails | |
*/ | |
public static String calculateHMAC_SHA(String data, String key) throws SignatureException{ | |
String result = null; | |
try { | |
// get an hmac_sha1 key from the raw key bytes | |
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); | |
// get an hmac_sha1 Mac instance and initialize with the signing key | |
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); | |
mac.init(signingKey); | |
byte[] digest = mac.doFinal(data.getBytes()); | |
StringBuilder sb = new StringBuilder(digest.length*2); | |
String s; | |
for (byte b : digest){ | |
s = Integer.toHexString(0xFF & b); | |
if(s.length() == 1) { | |
sb.append('0'); | |
} | |
sb.append(s); | |
} | |
result = sb.toString(); | |
} catch (Exception e) { | |
throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment