Created
December 29, 2022 12:44
-
-
Save iKunalChhabra/139153eb8f7ea70da55d9f8fae971b95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
class Hash { | |
public static void main(String[] args) throws NoSuchAlgorithmException { | |
String message = "Hello World"; | |
Hash hash = new Hash(); | |
System.out.println("MD5: " + hash.getMD5(message)); | |
System.out.println("SHA-1: " + hash.getSHA1(message)); | |
System.out.println("SHA-256: " + hash.getSHA256(message)); | |
System.out.println("SHA-512: " + hash.getSHA512(message)); | |
} | |
private String getHash(String message, String algorithm) throws NoSuchAlgorithmException { | |
MessageDigest hash = MessageDigest.getInstance(algorithm); | |
hash.update(message.getBytes()); | |
byte[] digest = hash.digest(); | |
BigInteger bigInt = new BigInteger(1, digest); | |
return bigInt.toString(16); | |
} | |
public String getMD5(String message) throws NoSuchAlgorithmException { | |
return getHash(message, "MD5"); | |
} | |
public String getSHA1(String message) throws NoSuchAlgorithmException { | |
return getHash(message, "SHA-1"); | |
} | |
public String getSHA256(String message) throws NoSuchAlgorithmException { | |
return getHash(message, "SHA-256"); | |
} | |
public String getSHA512(String message) throws NoSuchAlgorithmException { | |
return getHash(message, "SHA-512"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment