-
-
Save thanhminhmr/03d6b61da0816246c7b3231c41215194 to your computer and use it in GitHub Desktop.
Hashing a String with SHA1 in Java
This file contains hidden or 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.io.*; | |
import java.util.logging.*; | |
import javax.xml.bind.DatatypeConverter; | |
/** | |
* Hashing with SHA1 | |
* | |
* @param input String to hash | |
* @return String hashed | |
*/ | |
public String sha1(String input) { | |
String sha1 = null; | |
try { | |
MessageDigest msdDigest = MessageDigest.getInstance("SHA-1"); | |
msdDigest.update(input.getBytes("UTF-8"), 0, input.length()); | |
sha1 = DatatypeConverter.printHexBinary(msdDigest.digest()); | |
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) { | |
Logger.getLogger(Encriptacion.class.getName()).log(Level.SEVERE, null, e); | |
} | |
return sha1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment