码迷,mamicode.com
首页 > 编程语言 > 详细

Java RSA (SHA1withRSA)签名和验签

时间:2018-03-12 13:27:42      阅读:1607      评论:0      收藏:0      [点我收藏+]

标签:span   hex   turn   ide   deb   charset   ring   log   gate   

static {
    try {
        SIGNATURE = Signature.getInstance("SHA1withRSA", "BC");
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException e) {
        throw Throwables.propagate(e);
    }
}

public static String getSignature(Map<String, String> map, String pfxPath, String pfxPasswd) {
    try {
        SIGNATURE.initSign(X509Certs.getPrivateKey(pfxPath, pfxPasswd).getPrivateKey());
        SIGNATURE.update(DigestUtils.sha1Hex(buildReqParams(map, false)).getBytes(Charsets.UTF_8));
        return Base64.encodeBase64String(SIGNATURE.sign()).replaceAll("\r|\n", "");
    } catch (Throwable e) {
        throw Throwables.propagate(e);
    }
}

public static void verifySign(Map<String, String> map, String certPath) throws GatewayFrontException {
    try {
        byte[] sign = Base64.decodeBase64(map.get("signature").getBytes(Charsets.UTF_8));
        map.remove("signature");
        SIGNATURE.initVerify(X509Certs.getVerifyPublicKey(certPath));
        SIGNATURE.update(DigestUtils.sha1Hex(ExPayUtil.buildReqParams(map, false)).getBytes(Charsets.UTF_8));
        Preconditions.checkState(SIGNATURE.verify(sign), ExPayConstant.VERIFY_SIGNATURE_FAILUE);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

 

Java RSA (SHA1withRSA)签名和验签

标签:span   hex   turn   ide   deb   charset   ring   log   gate   

原文地址:https://www.cnblogs.com/frankyou/p/8548492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!