Difference between revisions of "Template:Json API HMAC Block- Java"

From Updox API
Jump to: navigation, search
(Code Samples)
 
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
public static void generateHmacHeaders(HttpURLConnection connection) throws Exception {
+
   
 
+
   
    // Create the date string
+
    public static void generateHmacHeaders(HttpURLConnection connection, String applicationId, String applicationPassword, String accountId, String userId) throws Exception{
    final SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z)");
+
        final SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z)");
    d.setTimeZone(TimeZone.getTimeZone("GMT"));
+
        d.setTimeZone(TimeZone.getTimeZone("GMT"));
    final String timestampString = d.format(new Date());
+
        final String timestampString = d.format(new Date());
 
+
        final String message =  
    // Create the message
+
          (applicationId == null ? "" : applicationId) + ":" +  
    final String message = applicationId + ":" + applicationPassword + ":" + timestampString;
+
          (applicationPassword == null ? "" : applicationPassword) + ":" +  
 
+
          (accountId == null ? "" : accountId) + ":" +
    // Create the authorization hash
+
          (userId == null ? "" : userId) + ":" +
    final String auth = "HMAC " + hmacSha1ToBase64(message, apiSecret);
+
          timestampString;
 
+
        final String auth = "HMAC " + hmacSha1ToBase64(message, apiSecret); //note space after HMAC
    // Set the request headers
+
        connection.setRequestProperty("updox-timestamp", timestampString);
    connection.setRequestProperty("updox-timestamp", timestampString);
+
        connection.setRequestProperty("Authorization", auth);
    connection.setRequestProperty("Authorization", auth);
+
    }
}
+
{{Json HMACtoBASE64 Block-Java}}
 
+
public static String hmacSha1ToBase64(String value, String key) throws Exception{
+
 
+
    // Get an hmac_sha1 key from the raw key bytes
+
    byte[] keyBytes = key.getBytes();
+
    SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
+
 
+
    // Get an hmac_sha1 Mac instance and initialize with the signing key
+
    Mac mac = Mac.getInstance("HmacSHA1");
+
    mac.init(signingKey);
+
 
+
    // Compute the hmac on input data bytes
+
    byte[] rawHmac = mac.doFinal(value.getBytes());
+
 
+
    // Convert raw bytes to Hex
+
    byte[] base64Bytes = Base64.encodeBase64(rawHmac);
+
 
+
    // Covert array of Hex bytes to a String
+
    return new String(base64Bytes, "UTF-8");
+
}
+

Latest revision as of 11:34, 28 January 2014


   public static void generateHmacHeaders(HttpURLConnection connection, String applicationId, String applicationPassword, String accountId, String userId) throws Exception{
       final SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z)");
       d.setTimeZone(TimeZone.getTimeZone("GMT"));
       final String timestampString = d.format(new Date());
       final String message = 
          (applicationId == null ? "" : applicationId) + ":" + 
          (applicationPassword == null ? "" : applicationPassword) + ":" + 
          (accountId == null ? "" : accountId) + ":" + 
          (userId == null ? "" : userId) + ":" + 
          timestampString;
       final String auth = "HMAC " + hmacSha1ToBase64(message, apiSecret); //note space after HMAC
       connection.setRequestProperty("updox-timestamp", timestampString);  
       connection.setRequestProperty("Authorization", auth);
   }
   
   public static String hmacSha1ToBase64(String value, String key) throws Exception{
       byte[] keyBytes = key.getBytes();
       SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
       Mac mac = Mac.getInstance("HmacSHA1");
       mac.init(signingKey);
       byte[] rawHmac = mac.doFinal(value.getBytes());
       byte[] base64Bytes = Base64.encodeBase64(rawHmac);
       return new String(base64Bytes, "UTF-8");
   }