博客
关于我
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 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>