签名验证使用示例(MD5)

这篇博客详细介绍了DigestUtils工具类中关于MD5、SHA-1、SHA-256、SHA-384以及SHA-512等哈希算法的使用方法,包括计算字节数组、输入流和字符串的哈希值,并返回16进制的表示形式。通过这些方法,开发者可以方便地进行数据签名验证。
摘要由CSDN通过智能技术生成

1.定义常量:

# 鉴权开关
signature.enable=false
signature.secretkey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx
2.引用常量:

private
@Autowired
AccessConfigProperties accessConfigProperties;

@Value("${signature.enable}")
private boolean signatureEnable;
3.签名验证:

if (signatureEnable) {
    String appid = request.getHeader("appid");
    String signature = request.getHeader("signature");
    String timestamp = request.getHeader("timestamp");
    String secretKey = accessConfigProperties.getSecretKey(appid);
    if (secretKey == null) { // 接入产品非法
        throw new SignatureException(ErrorCode.ERR_SIGNATURE_FAIL);
    }
    if (StringUtils.isEmpty(signature) || StringUtils.isEmpty(timestamp) || StringUtils.isEmpty(appid)) {
        throw new SignatureException(ErrorCode.ERR_SIGNATURE_FAIL, "参数缺失");
    }
    String key = timestamp + uid + appid + secretKey;
    String result = DigestUtils.md5DigestAsHex(key.getBytes());
    if (result.equals(signature)) {
        filterChain.doFilter(request, response);
        return;
    } else {
        throw new SignatureException(ErrorCode.ERR_SIGNATURE_FAIL);
    }
}
附: DigestUtils工具类

DigestUtils是一个算法工具类,在package org.apache.commons.codec.digest;这个包下。

该类中常用的方法有:

[plain]  view plain   copy
  print ? 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.      * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.  
  3.      *   
  4.      * @param data  
  5.      *            Data to digest  
  6.      * @return MD5 digest  
  7.      */  
  8.     public static byte[] md5(byte[] data) {  
  9.         return getMd5Digest().digest(data);  
  10.     }  

[plain]  view plain   copy
  print ? 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.      * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.  
  3.      *   
  4.      * @param data  
  5.      *            Data to digest  
  6.      * @return MD5 digest  
  7.      * @throws IOException  
  8.      *             On error reading from the stream  
  9.      * @since 1.4  
  10.      */  
  11.     public static byte[] md5(InputStream data) throws IOException {  
  12.         return digest(getMd5Digest(), data);  
  13.     }  


[plain]  view plain   copy
  print ? 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.      * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.  
  3.      *   
  4.      * @param data  
  5.      *            Data to digest  
  6.      * @return MD5 digest  
  7.      */  
  8.     public static byte[] md5(String data) {  
  9.         return md5(getBytesUtf8(data));  
  10.     }  
[plain]  view plain   copy
  print ? 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.     * Calculates the MD5 digest and returns the value as a 32 character hex string.  
  3.     *   
  4.     * @param data  
  5.     *            Data to digest  
  6.     * @return MD5 digest as a hex string  
  7.     */  
  8.    public static String md5Hex(byte[] data) {  
  9.        return Hex.encodeHexString(md5(data));  
  10.    }  

[plain]  view plain   copy
  print ? 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.      * Calculates the MD5 digest and returns the value as a 32 character hex string.  
  3.      *   
  4.      * @param data  
  5.      *            Data to digest  
  6.      * @return MD5 digest as a hex string  
  7.      * @throws IOException  
  8.      *             On error reading from the stream  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值