X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FHashUtils.java;fp=src%2Fbe%2Fnikiroo%2Futils%2FHashUtils.java;h=df8d7c62daee97649c3e7528a6b884b3e041fd6e;hp=0000000000000000000000000000000000000000;hb=f433d15308b70e23280a65cef8c54002a7a971ce;hpb=5ddc36eacad78641be59db473f9bae9bad47eb20 diff --git a/src/be/nikiroo/utils/HashUtils.java b/src/be/nikiroo/utils/HashUtils.java new file mode 100644 index 0000000..df8d7c6 --- /dev/null +++ b/src/be/nikiroo/utils/HashUtils.java @@ -0,0 +1,89 @@ +package be.nikiroo.utils; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * Small class to easily hash some values in a few different ways. + *

+ * Does not handle the salt itself, you have to add it yourself. + * + * @author niki + */ +public class HashUtils { + /** + * Hash the given value. + * + * @param value + * the value to hash + * + * @return the hash that can be used to confirm a value + * + * @throws RuntimeException + * if UTF-8 support is not available (!) or SHA-512 support is + * not available + * @throws NullPointerException + * if email or pass is NULL + */ + static public String sha512(String value) { + return hash("SHA-512", value); + } + + /** + * Hash the given value. + * + * @param value + * the value to hash + * + * @return the hash that can be used to confirm the a value + * + * @throws RuntimeException + * if UTF-8 support is not available (!) or MD5 support is not + * available + * @throws NullPointerException + * if email or pass is NULL + */ + static public String md5(String value) { + return hash("MD5", value); + } + + /** + * Hash the given value. + * + * @param algo + * the hash algorithm to use ("MD5" and "SHA-512" are supported) + * @param value + * the value to hash + * + * @return the hash that can be used to confirm a value + * + * @throws RuntimeException + * if UTF-8 support is not available (!) or the algorithm + * support is not available + * @throws NullPointerException + * if email or pass is NULL + */ + static private String hash(String algo, String value) { + try { + MessageDigest md = MessageDigest.getInstance(algo); + md.update(value.getBytes("UTF-8")); + byte byteData[] = md.digest(); + + StringBuffer hexString = new StringBuffer(); + for (int i = 0; i < byteData.length; i++) { + String hex = Integer.toHexString(0xff & byteData[i]); + if (hex.length() % 2 == 1) + hexString.append('0'); + hexString.append(hex); + } + + return hexString.toString(); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(algo + " hashing not available", e); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException( + "UTF-8 encoding is required in a compatible JVM", e); + } + } +}