package net.the4thdimension.android;

import android.util.Base64;

import java.security.SignatureException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class AwsUtils {

	/***
	 * Computes RFC 2104-compliant HMAC signature. This can be used to sign the Amazon S3
	 * request urls
	 * 
	 * @param data The data to be signed.
	 * @param key The signing key.
	 * @return The Base64-encoded RFC 2104-compliant HMAC signature.
	 * @throws java.security.SignatureException when signature generation fails
	 */
	public static String getHMac(String data, String key) throws SignatureException {
		
		if(data == null) {
			throw new NullPointerException("Data to be signed cannot be null");
		}
		
		String result = null;
		try {

			final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

			// get an hmac_sha1 key from the raw key bytes
			SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

			// get an hmac_sha1 Mac instance &
			// initialize with the signing key
			Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
			mac.init(signingKey);

			// compute the hmac on input data bytes
			byte[] digest = mac.doFinal(data.getBytes());

			if(digest != null) {
				// Base 64 Encode the results
				result = Base64.encodeToString(digest, Base64.NO_WRAP);				
			}

		} catch (Exception e) {
			throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
		}

		return result;
	}
}

@@ -197,15 +197,21 @@
  %7B%0A%0A
-%09
+    
 /***%0A
-%09
+    
  * C
@@ -292,17 +292,20 @@
 azon S3%0A
-%09
+    
  * reque
@@ -316,14 +316,19 @@
 rls%0A
-%09 * %0A%09
+     *%0A    
  * @
@@ -361,17 +361,20 @@
 signed.%0A
-%09
+    
  * @para
@@ -379,16 +379,17 @@
 ram key 
+ 
 The sign
@@ -397,17 +397,20 @@
 ng key.%0A
-%09
+    
  * @retu
@@ -466,17 +466,20 @@
 nature.%0A
-%09
+    
  * @thro
@@ -550,14 +550,20 @@
 ils%0A
-%09 */%0A%09
+     */%0A    
 publ
@@ -644,15 +644,20 @@
 n %7B%0A
-%09%09%0A%09%09
+%0A        
 if
+ 
 (dat
@@ -665,27 +665,36 @@
  == null) %7B%0A
-%09%09%09
+            
 throw new Nu
@@ -753,17 +753,27 @@
 %22);%0A
-%09%09%7D%0A%09%09%0A%09%09
+        %7D%0A%0A        
 Stri
@@ -794,20 +794,35 @@
 ll;%0A
-%09%09try %7B%0A%0A%09%09%09
+        try %7B%0A%0A            
 fina
@@ -865,19 +865,28 @@
 SHA1%22;%0A%0A
-%09%09%09
+            
 // get a
@@ -924,19 +924,28 @@
 y bytes%0A
-%09%09%09
+            
 SecretKe
@@ -1020,19 +1020,28 @@
 ITHM);%0A%0A
-%09%09%09
+            
 // get a
@@ -1067,19 +1067,28 @@
 tance &%0A
-%09%09%09
+            
 // initi
@@ -1114,19 +1114,28 @@
 ing key%0A
-%09%09%09
+            
 Mac mac 
@@ -1174,19 +1174,28 @@
 RITHM);%0A
-%09%09%09
+            
 mac.init
@@ -1209,19 +1209,28 @@
 gKey);%0A%0A
-%09%09%09
+            
 // compu
@@ -1261,19 +1261,28 @@
 a bytes%0A
-%09%09%09
+            
 byte%5B%5D d
@@ -1324,13 +1324,23 @@
 );%0A%0A
-%09%09%09
+            
 if
+ 
 (dig
@@ -1354,20 +1354,32 @@
 null) %7B%0A
-%09%09%09%09
+                
 // Base 
@@ -1400,20 +1400,32 @@
 results%0A
-%09%09%09%09
+                
 result =
@@ -1475,21 +1475,32 @@
 AP);
-%09%09%09%09%0A%09%09%09%7D%0A%0A%09%09
+%0A            %7D%0A%0A        
 %7D ca
@@ -1523,11 +1523,20 @@
 ) %7B%0A
-%09%09%09
+            
 thro
@@ -1612,15 +1612,27 @@
 ));%0A
-%09%09%7D%0A%0A%09%09
+        %7D%0A%0A        
 retu
@@ -1646,9 +1646,12 @@
 lt;%0A
-%09
+    
 %7D%0A%7D%0A

Indent AwsUtils