JAVAはMD5アルゴリズム、SHA1アルゴリズム、SHA256アルゴリズムを実装しています

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

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/qq_27198345/article/details/110871032