Java加密篇(4)-Sha256

4 篇文章 0 订阅

一些物联网的接口有用到

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值