Skip to content

Instantly share code, notes, and snippets.

@bogdan
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogdan/811591b1c7f36f3a28fb to your computer and use it in GitHub Desktop.
Save bogdan/811591b1c7f36f3a28fb to your computer and use it in GitHub Desktop.
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;
import java.security.*;
public class HelloWorld {
public static void main(String []args)
throws UnsupportedEncodingException, NoSuchAlgorithmException {
String[] codes = {"ONE", "TWO"};
String digest = verification_digest("ef591fbbc527d77402d9a10dba92c195", "purchase",
"812881", "developer@example.com", "200.00", "2014-01-01T00:00:00+00:00",
codes);
System.out.println(digest);
}
public static String verification_digest(String key, String event_category,
String event_number, String email, String subtotal,
String order_date, String[] coupons)
throws UnsupportedEncodingException, NoSuchAlgorithmException {
String string = key + "|" + event_category + "|" +
event_number + "|" + email + "|" + (subtotal != null ? subtotal : "") + "|" +
(order_date != null ? order_date : "") + "|" + strJoin(coupons, ",");
string = string.replaceAll("\\|\\|", "|").replaceAll("\\|$", "");
System.out.println(string);
return hash256(string);
}
public static String strJoin(String[] aArr, String sSep) {
StringBuilder sbStr = new StringBuilder();
for (int i = 0, il = aArr.length; i < il; i++) {
if (i > 0)
sbStr.append(sSep);
sbStr.append(aArr[i]);
}
return sbStr.toString();
}
public static String hash256(String data) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(data.getBytes());
return bytesToHex(md.digest());
}
public static String bytesToHex(byte[] bytes) {
StringBuffer result = new StringBuffer();
for (byte byt : bytes) result.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1));
return result.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment