package org.jinstagram.realtime;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

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

import com.google.gson.Gson;

import org.apache.commons.codec.binary.Hex;
import org.jinstagram.exceptions.InstagramException;

public class SubscriptionUtil {
	
	private static final String HMAC_SHA1 = "HmacSHA1";
    
    public static SubscriptionResponseObject[] getSubscriptionResponseData(String jsonBody) throws InstagramException {
    	  Gson gson = new Gson();
    	  SubscriptionResponseObject[] responseData;

          try {
        	  responseData = gson.fromJson(jsonBody, SubscriptionResponseObject[].class);
          } catch (Exception e) {
              throw new InstagramException("Error parsing json to object type ");
          }

          return responseData;
    }
    
    /**
     * Method to verify Instagram's POST request for a specific subscription.
     * This method follow the algorithm in <a href="https://github.com/Instagram/python-instagram/blob/master/instagram/subscriptions.py">python-instagram</a> implementation.
     * @param clientSecret Client secret of our Instagram application
     * @param rawJsonData Raw JSON data from the POST request
     * @param xHubSignature The signature given by the HTTP header of the POST request
     * @return VerificationResult a class that represent result of the signature verification
     * @throws InstagramException
     */
    public static VerificationResult verifySubscriptionPostSignature(String clientSecret, String rawJsonData, String xHubSignature) throws InstagramException{
    	SecretKeySpec keySpec = new SecretKeySpec(clientSecret.getBytes(), HMAC_SHA1);
    	Mac mac;
    	
    	try {
			mac = Mac.getInstance(HMAC_SHA1);
			mac.init(keySpec);
			byte[] result = mac.doFinal(rawJsonData.getBytes());
			String encodedResult = Hex.encodeHexString(result);
			
			return new VerificationResult(encodedResult.equals(xHubSignature), encodedResult);
    	} catch (NoSuchAlgorithmException e) {
			throw new InstagramException("Invalid algorithm name!");
		} catch (InvalidKeyException e){
			throw new InstagramException("Invalid key: " + clientSecret);
		}
    }
    
    public static class VerificationResult {
    	
    	private final boolean success;
    	private final String resultingSignature;
    	
    	public VerificationResult(boolean success, String resultingSignature){
    		this.success = success;
    		this.resultingSignature = resultingSignature;
    	}

		public boolean isSuccess() {
			return success;
		}

		public String getResultingSignature() {
			return resultingSignature;
		}
    	
    }
}

@@ -2244,24 +2244,771 @@
 %0A    %7D%0A    %0A
+    public static VerificationResult verifySubscriptionPostSignature(String clientSecret, byte%5B%5D rawJsonData, String xHubSignature) throws InstagramException%7B%0A    %09SecretKeySpec keySpec = new SecretKeySpec(clientSecret.getBytes(), HMAC_SHA1);%0A    %09Mac mac;%0A    %09%0A    %09try %7B%0A%09%09%09mac = Mac.getInstance(HMAC_SHA1);%0A%09%09%09mac.init(keySpec);%0A%09%09%09byte%5B%5D result = mac.doFinal(rawJsonData);%0A%09%09%09String encodedResult = Hex.encodeHexString(result);%0A%09%09%09%0A%09%09%09return new VerificationResult(encodedResult.equals(xHubSignature), encodedResult);%0A    %09%7D catch (NoSuchAlgorithmException e) %7B%0A%09%09%09throw new InstagramException(%22Invalid algorithm name!%22);%0A%09%09%7D catch (InvalidKeyException e)%7B%0A%09%09%09throw new InstagramException(%22Invalid key: %22 + clientSecret);%0A%09%09%7D%0A    %7D%0A    %0A    %0A
     public s

Add additional method to verify signature. This method using raw JSON data as mentioned in Instagram developer website.