Skip to content

Instantly share code, notes, and snippets.

@minhphong306
Created November 30, 2017 07:20
Show Gist options
  • Save minhphong306/3306707edb4cb199e925c066aa3a1896 to your computer and use it in GitHub Desktop.
Save minhphong306/3306707edb4cb199e925c066aa3a1896 to your computer and use it in GitHub Desktop.
Code some hash function in java: MD5, SHA-1, SHA-256
package network_security;
import java.math.BigInteger;
import java.security.MessageDigest;
/**
*
* @author Admin
*/
public class HashJCA {
public String getHashMD5(String origin) throws Exception {
MessageDigest m = MessageDigest.getInstance("MD5");
m.update(origin.getBytes("UTF-8"), 0, origin.length());
String md5 = new BigInteger(1, m.digest()).toString(16);
return md5;
}
public String getHashSHA1(String origin) throws Exception {
String sha1;
MessageDigest m = MessageDigest.getInstance("SHA-1");
m.update(origin.getBytes("UTF-8"), 0, origin.length());
sha1 = new BigInteger(1, m.digest()).toString(16);
return sha1;
}
public String getHashSHA256(String origin) throws Exception {
String sha256;
MessageDigest m = MessageDigest.getInstance("SHA-256");
m.update(origin.getBytes("UTF-8"), 0, origin.length());
sha256 = new BigInteger(1, m.digest()).toString(16);
return sha256;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment