當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java SHA-512 Hash用法及代碼示例


SHA-2係列密碼哈希函數由六個哈希函數組成。這些是:

  1. SHA-224,具有224位哈希值
  2. SHA-256,具有256位哈希值
  3. SHA-384,具有384位哈希值
  4. SHA-512,具有512位哈希值
  5. SHA-512 /224,具有512位哈希值
  6. SHA-512 /256,具有512位哈希值
  7. 其中,SHA-256和SHA-512分別是最常用的使用32位和64位字計算的哈希函數。 SHA-224和SHA-384分別是SHA-256和SHA-512的截短版本,使用不同的初始值進行計算。

    要在Java中計算加密哈希值,將使用MessageDigest Class(位於包java.security下)。


    MessagDigest類提供以下加密哈希函數,以查找文本的哈希值,如下所示:

  • MD2
  • MD5
  • SHA-1
  • SHA-224
  • SHA-256
  • SHA-384
  • SHA-512

這些算法以稱為getInstance()的靜態方法初始化。選擇算法後,將計算消息摘要值,並將結果作為字節數組返回。 BigInteger類用於將結果字節數組轉換為其符號表示。然後,將該表示形式轉換為十六進製格式,以獲取預期的MessageDigest。

例子:

Input:hello world
Output :309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f

Input :GeeksForGeeks
Output :acc10c4e0b38617f59e88e49215e2e894afaee5ec948c2af6f44039f03c9fe47a9210e01d5cd926c142bdc9179c2ad30f927a8faf69421ff60a5eaddcf8cb9c

程序:下麵的程序顯示了SHA-512哈希函數的實現:

// Java program to calculate SHA-512 hash value 
  
import java.math.BigInteger; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
  
public class GFG { 
    public static String encryptThisString(String input) 
    { 
        try { 
            // getInstance() method is called with algorithm SHA-512 
            MessageDigest md = MessageDigest.getInstance("SHA-512"); 
  
            // digest() method is called 
            // to calculate message digest of the input string 
            // returned as array of byte 
            byte[] messageDigest = md.digest(input.getBytes()); 
  
            // Convert byte array into signum representation 
            BigInteger no = new BigInteger(1, messageDigest); 
  
            // Convert message digest into hex value 
            String hashtext = no.toString(16); 
  
            // Add preceding 0s to make it 32 bit 
            while (hashtext.length() < 32) { 
                hashtext = "0" + hashtext; 
            } 
  
            // return the HashText 
            return hashtext; 
        } 
  
        // For specifying wrong message digest algorithms 
        catch (NoSuchAlgorithmException e) { 
            throw new RuntimeException(e); 
        } 
    } 
  
    // Driver code 
    public static void main(String args[]) throws NoSuchAlgorithmException 
    { 
  
        System.out.println("HashCode Generated by SHA-512 for:"); 
  
        String s1 = "GeeksForGeeks"; 
        System.out.println("\n" + s1 + ":" + encryptThisString(s1)); 
  
        String s2 = "hello world"; 
        System.out.println("\n" + s2 + ":" + encryptThisString(s2)); 
    } 
}
輸出:
HashCode Generated by SHA-512 for:

GeeksForGeeks:acc10c4e0b38617f59e88e49215e2e894afaee5
ec948c2af6f44039f03c9fe47a9210e01d5cd926c142bdc9179c2ad
30f927a8faf69421ff60a5eaddcf8cb9c

hello world:309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee5
11a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cf
d830e81f605dcf7dc5542e93ae9cd76f

應用:

  • 密碼學
  • 數據的完整性


相關用法


注:本文由純淨天空篩選整理自RishabhPrabhu大神的英文原創作品 SHA-512 Hash In Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。