包名

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

方法

/**
 * 1免登 - 通过appSecret计算出来的签名值,并进行urlEncode
 * 1签名算法为
 * */
public static String getAppSecretSign(String appSecret, String timestamp) {
	String signStr = "";
	// 根据timestamp, appSecret计算签名值
	try {
		
		String stringToSign = timestamp;
		Mac mac = Mac.getInstance("HmacSHA256");
		mac.init(new SecretKeySpec(appSecret.getBytes("UTF-8"), "HmacSHA256"));
		byte[] signatureBytes = mac.doFinal(stringToSign.getBytes("UTF-8"));
		String signature = new String(Base64.encodeBase64(signatureBytes));
		signStr = urlEncode(signature,"UTF-8");
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		
	}
	return signStr;
}

 /**
 * 1钉钉免登签名算法 - urlEncode
 * 
 * */
public static String urlEncode(String value, String encoding) {
    if (value == null) {
        return "";
    }
    try {
        String encoded = URLEncoder.encode(value, encoding);
        return encoded.replace("+", "%20").replace("*", "%2A")
            .replace("~", "%7E").replace("/", "%2F");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException("FailedToEncodeUri", e);
    }
}
 posted on 2020-04-11 20:49  倾目  阅读(1022)  评论(0编辑  收藏  举报