Skip to content

Instantly share code, notes, and snippets.

@arnoldmartinez
Created April 2, 2017 00:58
Show Gist options
  • Save arnoldmartinez/f48d94d33e91b63ea58deee5f142492a to your computer and use it in GitHub Desktop.
Save arnoldmartinez/f48d94d33e91b63ea58deee5f142492a to your computer and use it in GitHub Desktop.
Algorithm for computing SHA-256
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;
/**
* @author ARNOLD
*
*/
public class Algorithm {
private transient String sha;
private static final String SHA_256;
static {
SHA_256 = "SHA-256";
}
/**
* @param message
* @return String
*/
public String generateSha(String message) {
try {
sha = getHash(message);
} catch (NoSuchAlgorithmException noSuchAlgorithmException) {
noSuchAlgorithmException.printStackTrace();
}
return sha;
}
/**
* @param sha
* @return String
*/
private String getHash(String sha) throws NoSuchAlgorithmException {
MessageDigest engine = MessageDigest.getInstance(SHA_256);
engine.update(sha.getBytes());
byte[] cyphered = engine.digest();
return DatatypeConverter.printHexBinary(cyphered);
}
}
@arnoldmartinez
Copy link
Author

I've used the bcprov-jdk15on-1.50 jar

here is the link

https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on/1.50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment