| Index: chrome/android/java/src/org/chromium/chrome/browser/util/HashUtil.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/util/HashUtil.java b/chrome/android/java/src/org/chromium/chrome/browser/util/HashUtil.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a83d442597e3119ea7fdb31397338c8a1a1a86d1
|
| --- /dev/null
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/util/HashUtil.java
|
| @@ -0,0 +1,48 @@
|
| +// Copyright 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.chrome.browser.util;
|
| +
|
| +import android.util.Log;
|
| +
|
| +import java.security.MessageDigest;
|
| +import java.security.NoSuchAlgorithmException;
|
| +import java.util.Formatter;
|
| +
|
| +import javax.annotation.Nullable;
|
| +
|
| +/**
|
| + * Helper functions for working with hashes.
|
| + */
|
| +public final class HashUtil {
|
| + private static final String TAG = HashUtil.class.getSimpleName();
|
| +
|
| + private HashUtil() {
|
| + }
|
| +
|
| + public static String getMd5Hash(@Nullable String salt, String text) {
|
| + return getHash(salt, text, "MD5");
|
| + }
|
| +
|
| + private static String getHash(@Nullable String salt, String text, String algorithm) {
|
| + try {
|
| + String digestText = (salt == null ? "" : salt) + text;
|
| + MessageDigest m = MessageDigest.getInstance(algorithm);
|
| + byte[] digest = m.digest(digestText.getBytes());
|
| + return encodeHex(digest);
|
| + } catch (NoSuchAlgorithmException e) {
|
| + Log.e(TAG, "Unable to find digest algorithm " + algorithm);
|
| + return null;
|
| + }
|
| + }
|
| +
|
| + private static String encodeHex(byte[] data) {
|
| + StringBuilder sb = new StringBuilder(data.length * 2);
|
| + Formatter formatter = new Formatter(sb);
|
| + for (byte b : data) {
|
| + formatter.format("%02x", b);
|
| + }
|
| + return sb.toString();
|
| + }
|
| +}
|
|
|