...
Code Block | ||
---|---|---|
| ||
package com.sunwave;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.TimeZone;
import java.util.Date;
public class DigestCreator {
private String userId;
private String clientId;
private String clientSecret; // Realm Private Key "select client_secret from sw_external_application where client_id = ? and clinic_id = ?
private String clinicId;
private String transactionId; //"select id from sw_api_transaction where transaction_id = ? and clinic_id = ?"
private String payload;
private String createMd5Digest() throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(payload.getBytes());
return md.digest().toString();
}
private String createSeed() throws NoSuchAlgorithmException, UnsupportedEncodingException {
String seed = null;
if (payload == null) {
seed = userId + ":" + clientId + ":" + getDateTimeBase64() + ":" + clinicId + ":" + transactionId;
} else {
seed = userId + ":" + clientId + ":" + getDateTimeBase64() + ":" + clinicId + ":" + transactionId + ":" + createMd5Digest();
}
return seed;
}
private String getDateTimeBase64() throws UnsupportedEncodingException {
SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Date d = new Date();
java.sql.Timestamp now = new java.sql.Timestamp(new java.util.Date().getTime());
String dateTime = df.format(now);
byte[] encodedDate = Base64.getEncoder().encode(dateTime.getBytes());
return new String(encodedDate, "UTF8");
}
private String createTransactionId() {
return null;
}
private String createToken() throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
return userId + ":" + clientId + ":" + getDateTimeBase64() + ":" + clinicId + ":" + transactionId + ":" + createHMAC(createSeed());
}
private String createHMAC(String message) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
byte[] byteKey = clientSecret.getBytes("UTF-8");
final String HMAC_SHA512 = "HmacSHA512";
Mac sha512_HMAC = Mac.getInstance(HMAC_SHA512);
SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA512);
sha512_HMAC.init(keySpec);
byte[] mac_data = sha512_HMAC.
doFinal(message.getBytes("UTF-8"));
return Base64.getUrlEncoder().encodeToString(mac_data);
}
public DigestCreator(String userId, String clientId, String clientSecret, String clinic_id, String transactionId, String payload) {
this.userId = userId;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.clinicId = clinic_id;
this.transactionId = transactionId;
this.payload = payload;
}
public static void main( String args[] ) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
if ((args.length < 5) || (args.length > 6)) {
System.out.println("usage: java -cp . org.baudekin.DigestCreator user_id clinic_id client_id, client_secret, transaction_id, <pay load>");
System.exit(-1);
}
String payload = null;
// We are doing post
if (args.length == 6) {
payload = args[5];
}
DigestCreator dc = new DigestCreator(args[0], args[2], args[3], args[1], args[4], payload);
System.out.println("Token: " + dc.createToken());
System.exit(0);
}
}
|
...