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

你可能感兴趣的文章
Python2 已终结,入手Python 3,你需要这30个技巧
查看>>
python写下拉列表在excel,无法使用python获取Excel下拉列表(组合框)值
查看>>
python2.7安装
查看>>
Python2.7安装ncmbot时提示:Microsoft Visual C++9.0 is required
查看>>
PYTHON2.Git.1
查看>>
python20181211
查看>>
Python内存管理机制
查看>>
Python2中range 和xrange的区别??
查看>>
python2和3的区别
查看>>
python2和python3的pip版本升级
查看>>
python2和python3的区别
查看>>
python2备份交换机配置脚本
查看>>
Python3 - 在字符串格式化程序参数中使用变量
查看>>
Python3 A*寻路算法实现
查看>>
python3 grpc安装
查看>>
python3 mysql 封装多表_python3 mysql-多表查询
查看>>
python3 mysql8_Python MySQL8.0 错误:authentication plugin 'calling_sha2_password' is not supported....
查看>>
Python关键代码总览!从Zero到Hero必看的考点
查看>>
python3 robotframework+pycharm框架搭建
查看>>
Python3 shutil模块
查看>>