Je me suis encore ennuyé...
/**
* @author BjornS
* @created 2. sep. 2010
*/
public enum HashUtil {
SHA1("SHA1"), MD5("MD5"), MD2("MD2"), SHA256("SHA-256"), SHA384("SHA-384"), SHA512("SHA-512");
private final MessageDigest digester;
HashUtil(String algorithm) {
try {
digester = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public String hash(byte[] in) {
return toHexString(digester.digest(in));
}
public String hash(String in) {
return hash(in.getBytes());
}
public String hash(String in, Charset characterSet) {
return hash(in.getBytes(characterSet));
}
public byte[] hashToByteArray(String in) {
return digester.digest(in.getBytes());
}
public byte[] hashToByteArray(String in, Charset characterSet) {
return digester.digest(in.getBytes(characterSet));
}
public byte[] hashToByteArray(byte[] in) {
return digester.digest(in);
}
private String toHexString(byte[] digest) {
StringBuffer hexStr = new StringBuffer(40);
for (byte b : digest) {
hexStr.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
}
return hexStr.toString();
}
}
-
/*** ***/
// Use Charsets from Google Guava rather than manually code the charset request, you also don't have to catch exceptions this way! :)
pulic static void main(String... args) {
UUID uuid = UUID.randomUUID();
String uuidString = uuid.toString().substring(0,12);
String token = HashUtil.MD5.hash(uuidString,Charsets.UTF_8);
}