carson1978
2015-05-14 08:53
采纳率: 100%
浏览 2.0k

签名算法,java代码转为 C#

请帮忙把如下的java签名算法转换为c#的,牛人伸手啊!!

具体代码如下:
public static String genSignature(String sk, String stringToSign) {
String signature = "";

    try {
        String encodedSign = URLEncoder.encode(stringToSign, ENCODING);
        try {
            Mac mac = Mac.getInstance(HASH);
            mac.init(new SecretKeySpec(sk.getBytes(ENCODING), HASH));
            byte[] hashData = mac.doFinal(encodedSign.getBytes(ENCODING));

            StringBuilder sb = new StringBuilder(hashData.length * 2);
            for (byte data : hashData) {
                String hex = Integer.toHexString(data & 0xFF);
                if (hex.length() == 1) {
                    // append leading zero
                    sb.append("0");
                }
                sb.append(hex);
            }
            signature = sb.toString().toLowerCase();
        } catch (Exception e) {
            logger.warn("sha256 exception for[" + sk + "," + stringToSign + "]. e:", e);
        }
    } catch (UnsupportedEncodingException e) {
        logger.warn("encode error, string[" + stringToSign + "] e:" + e);
    }

    return signature;
}

4条回答 默认 最新

相关推荐 更多相似问题