Generate SHA 512 Hash Example
This program demonstrates generating SHA 512 hash of a file.
Problem Statement
Write a Java program that generates SHA 512 hash for a file.
Source Code
| 1 | import java.io.*; |
| 2 | import java.security.*; |
| 3 | public class SHA512HashExample { |
| 4 | public static void main(String[] args) { |
| 5 | System.out.println("Eduinq SHA-512 Hash Example"); |
| 6 | try(FileInputStream fis=new FileInputStream("sample.txt")) { |
| 7 | MessageDigest md=MessageDigest.getInstance("SHA-512"); |
| 8 | byte[] buffer=new byte[1024]; |
| 9 | int len; |
| 10 | while((len=fis.read(buffer))!=-1){ md.update(buffer,0,len); } |
| 11 | byte[] hash=md.digest(); |
| 12 | StringBuilder sb=new StringBuilder(); |
| 13 | for(byte b:hash) sb.append(String.format("%02x",b)); |
| 14 | System.out.println("Eduinq SHA-512 Hash: "+sb.toString()); |
| 15 | } catch(Exception e) { |
| 16 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 17 | } |
| 18 | } |
| 19 | } |
Program Output
Eduinq SHA-512 Hash Example Eduinq SHA-512 Hash: (long hex string)
Explanation
SHA 512 provides stronger security than SHA 256. - Generates long hash for file content. - Exception handling ensures safe operations. - In enterprise systems, SHA 512 is used for high-security applications. Conclusion: Shows generating SHA 512 hash.