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

From Updox API
Jump to: navigation, search
(Code Samples)
 
Line 1: Line 1:
 
public static void generateHmacHeaders(HttpURLConnection connection) throws Exception {
 
public static void generateHmacHeaders(HttpURLConnection connection) throws Exception {
  
    // Create the date string
+
     final SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z)"); <span style="color:green">// Create the date string</span>
     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());
  
    // Create the message
+
     final String message = applicationId + ":" + applicationPassword + ":" + timestampString;<span style="color:green">// Create the message</span>
     final String message = applicationId + ":" + applicationPassword + ":" + timestampString;
+
  
    // Create the authorization hash
+
     final String auth = "HMAC " + hmacSha1ToBase64(message, apiSecret); <span style="color:green">// Create the authorization hash</span>
     final String auth = "HMAC " + hmacSha1ToBase64(message, apiSecret);
+
  
    // Set the request headers
+
     connection.setRequestProperty("updox-timestamp", timestampString); <span style="color:green">// Set the request headers</span>
     connection.setRequestProperty("updox-timestamp", timestampString);
+
 
     connection.setRequestProperty("Authorization", auth);
 
     connection.setRequestProperty("Authorization", auth);
 
}
 
}
Line 19: Line 15:
 
public static String hmacSha1ToBase64(String value, String key) throws Exception{
 
public static String hmacSha1ToBase64(String value, String key) throws Exception{
  
    // Get an hmac_sha1 key from the raw key bytes
+
     byte[] keyBytes = key.getBytes();<span style="color:green">// Get an hmac_sha1 key from the raw key bytes</span>
     byte[] keyBytes = key.getBytes();
+
 
     SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
 
     SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
  
     // Get an hmac_sha1 Mac instance and initialize with the signing key
+
     Mac mac = Mac.getInstance("HmacSHA1"); <span style="color:green">// Get an hmac_sha1 Mac instance and initialize with the signing key</span>
    Mac mac = Mac.getInstance("HmacSHA1");
+
 
     mac.init(signingKey);
 
     mac.init(signingKey);
  
    // Compute the hmac on input data bytes
+
     byte[] rawHmac = mac.doFinal(value.getBytes());<span style="color:green">// Compute the hmac on input data bytes</span>
     byte[] rawHmac = mac.doFinal(value.getBytes());
+
  
    // Convert raw bytes to Hex
+
     byte[] base64Bytes = Base64.encodeBase64(rawHmac);<span style="color:green">// Convert raw bytes to Hex</span>
     byte[] base64Bytes = Base64.encodeBase64(rawHmac);
+
  
    // Covert array of Hex bytes to a String
+
     return new String(base64Bytes, "UTF-8");<span style="color:green">// Covert array of Hex bytes to a String</span>
     return new String(base64Bytes, "UTF-8");
+
 
}
 
}

Revision as of 16:02, 27 November 2013

public static void generateHmacHeaders(HttpURLConnection connection) throws Exception {

   final SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z)"); // Create the date string
   d.setTimeZone(TimeZone.getTimeZone("GMT"));
   final String timestampString = d.format(new Date());
   final String message = applicationId + ":" + applicationPassword + ":" + timestampString;// Create the message
   final String auth = "HMAC " + hmacSha1ToBase64(message, apiSecret); // Create the authorization hash
   connection.setRequestProperty("updox-timestamp", timestampString);  // Set the request headers
   connection.setRequestProperty("Authorization", auth);

}

public static String hmacSha1ToBase64(String value, String key) throws Exception{

   byte[] keyBytes = key.getBytes();// Get an hmac_sha1 key from the raw key bytes
   SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
   Mac mac = Mac.getInstance("HmacSHA1"); // Get an hmac_sha1 Mac instance and initialize with the signing key
   mac.init(signingKey);
   byte[] rawHmac = mac.doFinal(value.getBytes());// Compute the hmac on input data bytes
   byte[] base64Bytes = Base64.encodeBase64(rawHmac);// Convert raw bytes to Hex
   return new String(base64Bytes, "UTF-8");// Covert array of Hex bytes to a String

}