工具类

您所在的位置:网站首页 Java哈希值怎么计算的 工具类

工具类

2024-07-16 18:57| 来源: 网络整理| 查看: 265

开发任务二:

计算两个字符的hash值工具类,输入项为(string1,string2,hashAlgorithm),根据hash算法计算string1和string2的hash值,再将两个hash值拼接后再计算出一个hash值输出。 hashAlgorithm名为配置文件可配。

        这是师傅给我的第二个开发任务,比较简单,在网上找了很久,计算hash值的实例并不多。

代码:

工具类:

package com.xdja.timingDemo.utility; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.Security; @Component @PropertySource(value = {"classpath:param.properties"},ignoreResourceNotFound=true)//获取配置文件中的hashAlgorithm public class HashCode { @Value("${hashAlgorithm}") private String algorithm; public void getHashCode( String s1, String s2) throws NoSuchAlgorithmException { //扩展支持的算法 Security.addProvider(new BouncyCastleProvider()); System.out.println("-----------------------获取的hashAlgorithm是:"+algorithm); String hashAlgorithm = algorithm; //s1字符串的hasncode String resultStr1 = getDigestResult(s1,hashAlgorithm); //s2字符串的hasncode String resultStr2 = getDigestResult(s2,hashAlgorithm); //两个hashcode拼接字符串s3 String s3 = resultStr1 + resultStr2; //s3的hashcode String resultStr3 = getDigestResult(s3,hashAlgorithm); System.out.println("---------------分割线------------------------"); System.out.println("s1的hashcode为:"+resultStr1); System.out.println("---------------------------------------------"); System.out.println("s2的hashcode为:"+resultStr2); System.out.println("---------------------------------------------"); /*System.out.println("s1+s2="+s3);*/ System.out.println("拼接s1+s2的hashcode为:"+resultStr3); System.out.println("hashcode长度为:"+resultStr3.length()); } private String getDigestResult(String str, String hashAlgorithm){ MessageDigest mDigest = null; try { mDigest = MessageDigest.getInstance(hashAlgorithm); } catch (NoSuchAlgorithmException e) { System.out.println("系统算法不支持将默认为MD5!"); try { mDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); } e.printStackTrace(); } mDigest.update(str.getBytes()); BigInteger bigInt = new BigInteger(1, mDigest.digest()); String resultStr = bigInt.toString(16); return resultStr; } }

测试类:

package com.xdja.timingDemo.controller; import com.xdja.timingDemo.utility.HashCode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.PropertySource; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.security.NoSuchAlgorithmException; @RestController public class TestHashCode { @Autowired HashCode hashCode; @GetMapping(value = "cert/getHashCode") public void getHashCode(){ try { hashCode.getHashCode("abcdEFG","efgabcd"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } }

 param.properties:

hashAlgorithm=MD5 //hashAlgorithm=SHA-1

运行springboot的主类Application.java启动项目,在浏览器中输入http://localhost:8082/cert/getHashCode得到结果。

控制台:

参考官方文档:https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#MessageDigest

MessageDigest支持的算法有限,如下图:

在方法首行加上:

Security.addProvider(new BouncyCastleProvider());

基本可以实现所有hash算法,比如 Tiger、MD4等。

本次开发使用的框架为SpringBoot,部分配置延用上一个任务,即上一篇博客https://blog.csdn.net/weixin_42209368/article/details/87713631 

至此,小任务完成。如有其他更好的方法,请指教!!!

 

 

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3