package com.uas.finance.util;

import org.springframework.security.crypto.codec.Hex;

/**
 * Created by hejq on 2017-11-24.
 */
public class HmacUtils {

    private static HmacEncoder hmacEncoder = new HmacSHA256Encoder();
    private static final byte[] key = new byte[]{104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 117, 98, 116, 111, 98, 46, 99, 111, 109, 47, 101, 114, 112, 47, 115, 97, 108, 101, 47, 111, 114, 100, 101, 114, 115, 63, 115, 111, 109, 101, 116, 104, 105, 110, 103};

    public HmacUtils() {
    }

    public static String encode(Object message) {
        byte[] encodeData = hmacEncoder.encode(String.valueOf(message).getBytes(), key);
        return new String(Hex.encode(encodeData));
    }

    public static String encode(Object message, String key) {
        byte[] encodeData = hmacEncoder.encode(String.valueOf(message).getBytes(), key.getBytes());
        return new String(Hex.encode(encodeData));
    }

    private static class HmacSHA256Encoder extends HmacEncoder {
        public HmacSHA256Encoder() {
            super("HmacSHA256");
        }
    }
}
