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

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

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");
   }