博客
关于我
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-网络编程
查看>>
python写入文件出现空行
查看>>
PYTHON-返回给定x的多边形路径的y坐标
查看>>
python-进程池与线程池,协程
查看>>
Python-通过函数传递变量
查看>>
Python内置函数详解——总结篇
查看>>
Python.对象集合中的标识.和散列
查看>>
Python.硒.拖放错误“AttributeError:move_to 需要 WebElement“
查看>>
Python内置函数详解
查看>>
Python/Kivy:从一个类调用函数到另一个类并在 Python 中显示小部件
查看>>
Python/Kivy:我可以在双击标签时调用函数吗
查看>>
python内置函数5-frozenset()
查看>>
Python/Plotly:如何使用要显示的信息自定义悬停模板?
查看>>
Python/Selenium - 无法单击 www.instagram.com 上的“接受 cookie“按钮
查看>>
Python内置函数(35)——next
查看>>
python写入mysql
查看>>
python写入csv老是乱码或者各种不好
查看>>
Python内置函数
查看>>
Python2 安装MySQLdb库
查看>>
Python2 已终结,入手Python 3,你需要这30个技巧
查看>>