Java對PHP服務器hmac_sha1簽名認證方法的匹配實現 的另一種方式

	public static String hmacSHA1(String data,String key) {
		String result = "";
		byte[] bytesKey = key.getBytes();
		final SecretKeySpec secretKey = new SecretKeySpec(bytesKey, "HmacSHA1");
		try {
			Mac mac = Mac.getInstance("HmacSHA1");
			mac.init(secretKey);
			final byte[] macData = mac.doFinal(data.getBytes());
			byte[] hex = new Hex().encode(macData);
			result = new String(hex, "ISO-8859-1");
		} catch (NoSuchAlgorithmException e) {
			// TODO 自動生成的 catch 塊
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO 自動生成的 catch 塊
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO 自動生成的 catch 塊
			e.printStackTrace();
		}
		return result;
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章