博客
关于我
JAVA实现MD5算法、SHA1算法和SHA256算法
阅读量:316 次
发布时间:2019-03-03

本文共 2932 字,大约阅读时间需要 9 分钟。

JAVA实现MD5算法、SHA1算法和SHA256算法

MD5、SHA1和SHA256是常见的哈希算法。JAVA中的hashCode是int类型,占64位。MD5生成128位哈希码,SHA1生成160位哈希码,SHA256生成256位哈希码。

MD5计算hashCode

以下是一个实现MD5算法的JAVA类:

package utils;import org.junit.Test;import java.security.MessageDigest;public class MD5Utils {    public static String md5Code(String input) {        try {            MessageDigest digest = MessageDigest.getInstance("MD5");            byte[] hash = digest.digest(input.getBytes("UTF-8"));            StringBuilder hexString = new StringBuilder();            for (int i = 0; i < hash.length; i++) {                String hex = Integer.toHexString(0xff & hash[i]);                if (hex.length() == 1) {                    hexString.append('0');                }                hexString.append(hex);            }            return hexString.toString();        } catch (Exception e) {            throw new RuntimeException(e);        }    }    @Test    public void test() {        String str = "Welcome";        String res = md5Code(str);        System.out.println(res);    }}

SHA1计算hashCode

以下是一个实现SHA1算法的JAVA类:

package utils;import org.junit.Test;import java.security.MessageDigest;public class SHA1Utils {    public static String sha1Code(String input) {        try {            MessageDigest digest = MessageDigest.getInstance("SHA1");            byte[] hash = digest.digest(input.getBytes("UTF-8"));            StringBuilder hexString = new StringBuilder();            for (int i = 0; i < hash.length; i++) {                String hex = Integer.toHexString(0xff & hash[i]);                if (hex.length() == 1) {                    hexString.append('0');                }                hexString.append(hex);            }            return hexString.toString();        } catch (Exception e) {            throw new RuntimeException(e);        }    }    @Test    public void test() {        String str = "Welcome";        String res = sha1Code(str);        System.out.println(res);    }}

SHA256计算hashCode

以下是一个实现SHA256算法的JAVA类:

package utils;import org.junit.Test;import java.security.MessageDigest;public class SHA256Utils {    public static String sha256Code(String input) {        try {            MessageDigest digest = MessageDigest.getInstance("SHA-256");            byte[] hash = digest.digest(input.getBytes("UTF-8"));            StringBuffer hexString = new StringBuffer();            for (int i = 0; i < hash.length; i++) {                String hex = Integer.toHexString(0xff & hash[i]);                if (hex.length() == 1) {                    hexString.append('0');                }                hexString.append(hex);            }            return hexString.toString();        } catch (Exception e) {            throw new RuntimeException(e);        }    }    @Test    public void test() {        String value = "Welcome";        String res = sha256Code(value);        System.out.println(res);    }}

以上代码均为JAVA实现MD5、SHA1和SHA256算法的示例代码,具体实现细节可根据实际需求进行调整。

转载地址:http://yguq.baihongyu.com/

你可能感兴趣的文章
Python+SQL实战:京东用户行为数据分析案例解析(下)
查看>>
PYTHON+Twisted+sqlanydb=ABORT()
查看>>
Python函数之返回值、作用域和局部变量
查看>>
Python+统计学 | 探索常用的数据分析统计分布
查看>>
python, selenium中用于切换帧的函数
查看>>
python函数
查看>>
python----线程、进程、协程的区别及多线程详解
查看>>
python---5
查看>>
python---使用celery分布式系统异步发送短信(云通讯)来验证登录
查看>>
python--003--百分号字符串拼接、format
查看>>
Python--10、线程
查看>>
Python--Redis实战:第四章:数据安全与性能保障:第4节:复制
查看>>
python--基本代码规范
查看>>
Python--抽象类接口类
查看>>
PYTHON--欧拉习题二
查看>>
python--高级应用总结
查看>>
python-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
查看>>
Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
查看>>
Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
查看>>
Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
查看>>