Difference between revisions of "Template:Json HMACtoBASE64 Block-Java"

From Updox API
Jump to: navigation, search
 
Line 1: Line 1:
 
      
 
      
 
     public static String hmacSha1ToBase64(String value, String key) throws Exception{
 
     public static String hmacSha1ToBase64(String value, String key) throws Exception{
         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");
         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);
         byte[] rawHmac = mac.doFinal(value.getBytes());<span style="color:green">// Compute the hmac on input data bytes</span>
+
         byte[] rawHmac = mac.doFinal(value.getBytes());
         byte[] base64Bytes = Base64.encodeBase64(rawHmac);<span style="color:green">// Convert raw bytes to Hex</span>
+
         byte[] base64Bytes = Base64.encodeBase64(rawHmac);
         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");
 
     }
 
     }

Latest revision as of 16:22, 27 November 2013

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