Created
March 22, 2014 14:44
-
-
Save samma835/9708293 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.ilegendsoft.app; | |
import java.io.UnsupportedEncodingException; | |
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import javax.crypto.Mac; | |
import javax.crypto.spec.SecretKeySpec; | |
public class Main { | |
private final static String PRIVATE_KEY = "b4a23f5e39b5929e0666ac5de94c89d1618a2916"; | |
private final static String MSG = "{\"username\":\"pindleskin8\",\"guid\":\"92a0491f-3663-47d3-ad12-147f8f7a5f63\",\"password\":\"shanghai13\",\"device_id\":\"android-1caa01e98bb5c6cb\"}"; | |
public static void main(String[] args) { | |
System.out.println(hmacDigest(MSG, PRIVATE_KEY, "HmacSHA256")); | |
} | |
public static String hmacDigest(String msg, String keyString, String algo) { | |
String digest = null; | |
try { | |
SecretKeySpec key = new SecretKeySpec( | |
(keyString).getBytes("UTF-8"), algo); | |
Mac mac = Mac.getInstance(algo); | |
mac.init(key); | |
byte[] bytes = mac.doFinal(msg.getBytes("ASCII")); | |
StringBuffer hash = new StringBuffer(); | |
for (int i = 0; i < bytes.length; i++) { | |
String hex = Integer.toHexString(0xFF & bytes[i]); | |
if (hex.length() == 1) { | |
hash.append('0'); | |
} | |
hash.append(hex); | |
} | |
digest = hash.toString(); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} catch (InvalidKeyException e) { | |
e.printStackTrace(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
return digest; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment