java实现HMAC-MD5算法

	private final String HMAC_MD5_NAME = "HmacMD5";

	public HMACDataTransformer(String key) {
		this(key, "hmac");
	}
	public HMACDataTransformer(String key, String name) {
		super(key, name);
		// TODO Auto-generated constructor stub
	}
	@Override
	public byte[] encode(byte[] data) throws TransformerException {
		SecretKeySpec sk = new SecretKeySpec(StringTools.getAsciiBytes(this.key), HMAC_MD5_NAME);
		Mac mac;
		try {
			mac = Mac.getInstance(HMAC_MD5_NAME);
			mac.init(sk);
			return mac.doFinal(data);
		} catch (NoSuchAlgorithmException e) {
			throw new TransformerException(e);
		} catch (InvalidKeyException e) {
			throw new TransformerException(e);
		}
	}
	@Override
	public String encode(String data) throws TransformerException {
		return StringTools.getUTF8String(encode(StringTools.getUTF8Bytes(data)));
	}

你可能感兴趣的:(java,算法,String,byte,Constructor)