Redact personally identifiable information

This CL adds a utility function to redact personally identifiable
information from logs.

Change-Id: I5185dc436a22397dce55487a7531406097591720
diff --git a/src/com/android/telecomm/Log.java b/src/com/android/telecomm/Log.java
index 73892cd..f70cf55 100644
--- a/src/com/android/telecomm/Log.java
+++ b/src/com/android/telecomm/Log.java
@@ -18,6 +18,8 @@
 
 import java.util.IllegalFormatException;
 import java.util.Locale;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 
 /**
  * Manages logging for the entire module.
@@ -118,6 +120,44 @@
         android.util.Log.wtf(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
     }
 
+    /**
+     * Redact personally identifiable information for production users.
+     * If we are running in verbose mode, return the original string, otherwise
+     * return a SHA-1 hash of the input string.
+     */
+    public static String pii(Object pii) {
+        if (pii == null || VERBOSE) {
+            return String.valueOf(pii);
+        }
+        return "[" + secureHash(String.valueOf(pii).getBytes()) + "]";
+    }
+
+    private static String secureHash(byte[] input) {
+        MessageDigest messageDigest;
+        try {
+            messageDigest = MessageDigest.getInstance("SHA-1");
+        } catch (NoSuchAlgorithmException e) {
+            return null;
+        }
+        messageDigest.update(input);
+        byte[] result = messageDigest.digest();
+        return encodeHex(result);
+    }
+
+    private static String encodeHex(byte[] bytes) {
+        StringBuffer hex = new StringBuffer(bytes.length * 2);
+
+        for (int i = 0; i < bytes.length; i++) {
+            int byteIntValue = (int) bytes[i] & 0xff;
+            if (byteIntValue < 0x10) {
+                hex.append("0");
+            }
+            hex.append(Integer.toString(byteIntValue, 16));
+        }
+
+        return hex.toString();
+    }
+
     private static String getPrefixFromObject(Object obj) {
         return obj == null ? "<null>" : obj.getClass().getSimpleName();
     }