/** * Copyright 2014 Oracle | BlueKai */ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.EnumMap; import java.util.Map; /** * Demonstration on email and phone hashing * */ class Hashing { public enum Algorithm { SHA256("SHA-256"); private final String messageDigestAlgorithmName; private Algorithm(String messageDigestAlgorithmName) { this.messageDigestAlgorithmName = messageDigestAlgorithmName; } public String getMessageDigestAlgorithmName() { return messageDigestAlgorithmName; } } Map mdMap = new EnumMap(Algorithm.class); public MessageDigest digestInstance(Algorithm algorithm) throws NoSuchAlgorithmException { if (algorithm == null) { throw new NoSuchAlgorithmException(); } MessageDigest md; if (!mdMap.containsKey(algorithm)) { md = MessageDigest.getInstance(algorithm.getMessageDigestAlgorithmName()); mdMap.put(algorithm, md); } else { md = mdMap.get(algorithm); } return md; } public String normalizeEmail(String email) { return email.trim().toLowerCase().replaceFirst("\\+[^@]*@", "@"); } public String normalizePhoneNumber(String phoneNumber) { return phoneNumber.trim().replaceFirst("^0+", "").replaceAll("[^0-9]", ""); } private String createHash(String stringToHash, Algorithm algorithm) throws NoSuchAlgorithmException { MessageDigest md = digestInstance(algorithm); md.update(stringToHash.getBytes()); byte byteData[] = md.digest(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < byteData.length; i++) { sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); } public String createEmailHash(String email, Algorithm algorithm) throws NoSuchAlgorithmException { return createHash(normalizeEmail(email), algorithm); } public String createPhoneHash(String phoneNumber, Algorithm algorithm) throws NoSuchAlgorithmException { return createHash(normalizePhoneNumber(phoneNumber), algorithm); } /** * Notice: This is the end of hashing class functionality The rest of this class is an actual use cases and demo */ public static void assertEqual(Object actual, Object expected) throws AssertionError { if ((actual == null && expected != null) || (actual != null && !actual.equals(expected))) { throw new AssertionError(String.format("'%s' is expected to be equal to '%s'", actual, expected)); } } public static void main(String[] args) throws NoSuchAlgorithmException { Hashing hashCreator = new Hashing(); assertEqual(hashCreator.normalizeEmail(" Test@somewhere.com "), "test@somewhere.com"); assertEqual(hashCreator.normalizeEmail("Test+alias@gMail.Com "), "test@gmail.com"); assertEqual(hashCreator.normalizePhoneNumber(" 00123-456-7890 "), "1234567890"); assertEqual(hashCreator.createEmailHash(" Test@somewhere.com ", Algorithm.SHA256), "c6835820412cddddd5197dab400fec65c3d9a2617ae4ceb1442ec753abeec0ba"); assertEqual(hashCreator.createPhoneHash(" 00123-456-7890 ", Algorithm.SHA256), "c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646"); System.out.println("All tests pass!"); } }