Java加密篇(4)-Sha256

一些物联网的接口有用到

package utils;

import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

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

import utils.json.Json;

public class ShaUtils
{
	private static Charset CHARSET_UTF8 = Charset.forName("UTF-8");
	
	public static <T> String doHmacSHA2(String key, String data ) 
	{
		return doHmacSHA2(key, data, null);
	}
	
	@SuppressWarnings("rawtypes")
	public static <T> String doHmacSHA2(String key, String data, Map<String, T> params ) 
	{
		SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA256");
		
		Mac mac;
		try {
			mac = Mac.getInstance("HmacSHA256");
			mac.init(signingKey);
		} catch (NoSuchAlgorithmException e) {
			throw new IllegalStateException(e.getMessage(), e);
		} catch (InvalidKeyException e) {
			throw new IllegalStateException(e.getMessage(), e);
		}
		if(data != null && data.length() > 0)
		{
			mac.update(data.getBytes(CHARSET_UTF8));
		}
		if(params != null)
		{
			List<Map.Entry<String, T>> parameters = new ArrayList<Map.Entry<String,T>>(params.entrySet());
			Collections.sort(parameters, new MapEntryComparator<String, T>());
			for (Map.Entry<String, T> parameter : parameters) {
				byte[] name = parameter.getKey().getBytes(CHARSET_UTF8);
				Object value = parameter.getValue();
				if(value instanceof Collection){
					for (Object o : (Collection)value){
						mac.update(name);
						if(o != null){
							mac.update(o.toString().getBytes(CHARSET_UTF8));
						}
					}
				}else{
					mac.update(name);
					if(value != null){
						mac.update(value.toString().getBytes(CHARSET_UTF8));
					}
				}
			}
		}
		return encodeHexStr(mac.doFinal());
	}
	
	private static char[] digital = "0123456789ABCDEF".toCharArray();
	public static String encodeHexStr(final byte[] bytes){
		if (bytes == null) {
			return null;
		}
		char[] result = new char[bytes.length * 2];
		for (int i = 0; i < bytes.length; i++) {
			result[i * 2] = digital[(bytes[i] & 0xf0) >> 4];
			result[i * 2 + 1] = digital[bytes[i] & 0x0f];
		}
		return new String(result);
	}
}

class MapEntryComparator<K, V> implements Comparator<Map.Entry<K, V>> {

    @Override
    public int compare(Entry<K, V> o1, Entry<K, V> o2) {
        if (o1 == o2) {
            return 0;
        }
        final String k1 = o1.getKey().toString();
        final String k2 = o2.getKey().toString();
        int l1 = k1.length();
        int l2a = k2.length();
        for (int i = 0; i < l1; i++) {
            char c1 = k1.charAt(i);
            char c2;
            if (i < l2a) {
                c2 = k2.charAt(i);
            } else {
                return 1;
            }
            if (c1 > c2) {
                return 1;
            } else if (c1 < c2) {
                return -1;
            }
        }
        if (l1 < l2a) {
          return -1;
        }else if(l1 == l2a) {
          return 0;
        }else {
          return -1;
        }
    }
}

@SuppressWarnings("rawtypes")
class MyComprator implements Comparator {
    public int compare(Object arg0, Object arg1) {
        Json t1=(Json)arg0;
        Json t2=(Json)arg1;
        
        long l1 = t1.getLong("recordTime");
        long l2 = t2.getLong("recordTime");
        
        if(l1==l2) return 0;
        if(l1 > l2) return 1;
        return -1;
    }
}

你可能感兴趣的:(java,加密,java,开发语言)