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

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

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

MD5、SHA1和SHA256是最常见的哈希算法。JAVA中的hashCode是int类型的,占64位。

MD5是128位的哈希码计算算法;
SHA1是160位的哈希码计算算法;
SHA256是256位的哈希码计算算法。

MD5计算hashCode

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

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

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);    }}

在这里插入图片描述

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

你可能感兴趣的文章
mysql 自增id和UUID做主键性能分析,及最优方案
查看>>
Mysql 自定义函数
查看>>
mysql 行转列 列转行
查看>>
Mysql 表分区
查看>>
mysql 表的操作
查看>>
mysql 视图,视图更新删除
查看>>
MySQL 触发器
查看>>
mysql 让所有IP访问数据库
查看>>
mysql 记录的增删改查
查看>>
MySQL 设置数据库的隔离级别
查看>>
MySQL 证明为什么用limit时,offset很大会影响性能
查看>>
Mysql 语句操作索引SQL语句
查看>>
MySQL 误操作后数据恢复(update,delete忘加where条件)
查看>>
MySQL 调优/优化的 101 个建议!
查看>>
mysql 转义字符用法_MySql 转义字符的使用说明
查看>>
mysql 输入密码秒退
查看>>
mysql 递归查找父节点_MySQL递归查询树状表的子节点、父节点具体实现
查看>>
mysql 里对root及普通用户赋权及更改密码的一些命令
查看>>
Mysql 重置自增列的开始序号
查看>>
MySQL 高可用性之keepalived+mysql双主
查看>>