SHA1("UiTM Jasin Melaka 2015. Fare Well Everyone!")
Hexadecimal: 0eb2acda4c34c9a13097a299f561c7b2d592e8bf
Even a small change in the message will, with overwhelming probability, result in a completely different hash due to the avalanche effect.
SHA1("UiTM Jasin Melaka 2015. FareWell Everyone!")
Hexadecimal: 543232d470af26e6b25644f820cbab88c214d1f1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.security.*; public class HashTextTest { public static void main(String[] args){ System.out.println(sha1("UiTM Jasin Melaka 2015. Fare Well Everyone!")); } public static String sha1(String str){ StringBuffer shasum = new StringBuffer(); try{ MessageDigest mDigest = MessageDigest.getInstance("SHA1"); byte[] result = mDigest.digest(str.getBytes()); for (int i = 0; i < result.length; i++) { shasum.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1)); } } catch(NoSuchAlgorithmException e){ e.printStackTrace(); } return shasum.toString(); } } |
File SHA-1 Checksum
You can check files have the correct checksum or not..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import java.io.*; import java.security.*; public class HashFileTest { public static void main(String[] args){ boolean result = verifyChecksum("/Downloads/Iso/kali-linux-2.0-amd64.iso", "aaeb89a78f155377282f81a785aa1b38ee5f8ba0"); System.out.println("File's checksum matches the expected one? " + result); } public static boolean verifyChecksum(String file, String checksum) { String shaHash = ""; try{ System.out.println("Verifying File..'"+file+"'"); System.out.println("Please Wait.."); MessageDigest sha1 = MessageDigest.getInstance("SHA1"); FileInputStream fis = new FileInputStream(file); byte[] data = new byte[1024]; int read = 0; while ((read = fis.read(data)) != -1) { sha1.update(data, 0, read); } byte[] Bytes = sha1.digest(); StringBuffer shasum = new StringBuffer(); for (int i = 0; i < Bytes.length; i++) { shasum.append(Integer.toString((Bytes[i] & 0xff) + 0x100, 16).substring(1)); } shaHash = shasum.toString(); } catch(IOException | NoSuchAlgorithmException e){ e.printStackTrace(); } return shaHash.equals(checksum); } } |
No comments:
Post a Comment