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

你可能感兴趣的文章
pl/sql developer乱码,日期格式等问题解决
查看>>
PL/SQL 中的if elsif 练习
查看>>
PL/SQL 存储函数和过程
查看>>
query简单入门到精通细节 - (六)Jquery效果之“淡入与淡出”
查看>>