Skip to content

Instantly share code, notes, and snippets.

@putraxor
Forked from samma835/instakey.java
Created January 20, 2016 03:23
Show Gist options
  • Save putraxor/3890ed50ea2f8a7d7bd0 to your computer and use it in GitHub Desktop.
Save putraxor/3890ed50ea2f8a7d7bd0 to your computer and use it in GitHub Desktop.
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