博客
关于我
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/

你可能感兴趣的文章
PostgreSQL新手入门
查看>>
postgresql树状结构查询示例
查看>>
PostgreSQL流复制参数max_wal_senders详解
查看>>
postgresql流复制配置
查看>>
PostgreSQL清空表并保留表结构、清空数据库还原数据库为新建时的状态的方法
查看>>
PostgreSQL的 initdb 源代码分析之九
查看>>
PostgreSQL的安装与使用指南
查看>>
postman之参数化详解
查看>>
Postman入门到入土
查看>>
Postman如何做接口测试:如何导入 swagger 接口文档
查看>>
Postman如何做接口测试:如何导入 swagger 接口文档
查看>>
Qlik助力新西兰最大私人医院提高病患护理水平
查看>>
Postman如何生成接口文档
查看>>
Postman学习之常用断言
查看>>
postman居然是用electron开发的,我们来认识下这个框架吧
查看>>
postman常用公共函数
查看>>
Postman常见问题及解决方法
查看>>
PostMan怎样携带登录信息请求后台接口防止出现无法访问资源问题
查看>>
Postman接口传参案例
查看>>
postman接口功能测试
查看>>