/*
 *  Copyright (C) 2010-2012 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

package com.gallatinsystems.common.util;

import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

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

import com.google.gdata.util.common.util.Base64;

/**
 * utility for generating checksums and digests
 * 
 * @author Christopher Fagiani
 */
public class MD5Util {
    private static final Logger log = Logger.getLogger(MD5Util.class.getName());

    public static byte[] md5(byte[] arr) {
        try {
            MessageDigest complete = MessageDigest.getInstance("MD5");
            complete.update(arr);
            return complete.digest();
        } catch (NoSuchAlgorithmException e) {
            log.log(Level.SEVERE, "Could not generate checksum", e);
        }
        return null;
    }

    /**
     * generates a checksum based on an MD5 message digest
     * 
     * @param arr
     * @return
     */
    public static String generateChecksum(byte[] arr) {
        byte[] digest = md5(arr);
        if (digest != null) {
            String result = "";
            for (int i = 0; i < digest.length; i++) {
                result += Integer.toString((digest[i] & 0xff) + 0x100, 16)
                        .substring(1);
            }
            return result;
        }
        return null;
    }

    public static String generateHMAC(String content, String privateKey) {
        Mac mac;
        try {
            mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec secret = new SecretKeySpec(privateKey.getBytes(),
                    mac.getAlgorithm());
            mac.init(secret);
            byte[] digest = mac.doFinal(content.getBytes());
            return Base64.encode(digest);
        } catch (NoSuchAlgorithmException e) {
            log.log(Level.SEVERE, e.getMessage(), e);
        } catch (InvalidKeyException e) {
            log.log(Level.SEVERE, e.getMessage(), e);
        }
        return null;
    }
}

@@ -1854,26 +1854,28 @@
     
-String result = %22%22
+return toHex(digest)
 ;%0A  
@@ -1884,207 +1884,219 @@
     
+%7D%0A
     
-for (int i = 0; i %3C digest.length; i++) %7B
+    return null;
 %0A    
+%7D%0A%0A
     
-        result += Integer.toString((digest%5Bi%5D & 0xff) + 0x100, 16)%0A                        .substring(1);%0A            %7D%0A            return result
+public static String toHex(byte%5B%5D data) %7B%0A        StringBuilder builder = new StringBuilder();%0A        for (byte b : data) %7B%0A            builder.append(String.format(%22%2502x%22, b))
 ;%0A  
@@ -2110,36 +2110,50 @@
 %0A        return 
-null
+builder.toString()
 ;%0A    %7D%0A%0A    pub

Refactor bytes -> hex conversion