/*
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

package com.facebook.common.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Base64;

/**
 * Static methods for secure hashing.
 */
public class SecureHashUtil {

  public static String makeSHA1Hash(String text) {
    try {
      return makeSHA1Hash(text.getBytes("utf-8"));
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
  }

  public static String makeSHA1Hash(byte[] bytes) {
    try {
      MessageDigest md = MessageDigest.getInstance("SHA-1");
      md.update(bytes, 0, bytes.length);
      byte[] sha1hash = md.digest();
      return convertToHex(sha1hash);
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
  }

  public static String makeSHA1HashBase64(byte[] bytes) {
    try {
      MessageDigest md = MessageDigest.getInstance("SHA-1");
      md.update(bytes, 0, bytes.length);
      byte[] sha1hash = md.digest();
      return Base64.encodeToString(sha1hash, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(e);
    }
  }

  public static String makeMD5Hash(String text) {
    try {
      return makeMD5Hash(text.getBytes("utf-8"));
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
  }

  public static String makeMD5Hash(byte[] bytes) {
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(bytes, 0, bytes.length);
      byte[] sha1hash = md.digest();
      return convertToHex(sha1hash);
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
  }

  static final byte[] HEX_CHAR_TABLE = {
      (byte) '0', (byte) '1', (byte) '2', (byte) '3',
      (byte) '4', (byte) '5', (byte) '6', (byte) '7',
      (byte) '8', (byte) '9', (byte) 'a', (byte) 'b',
      (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f'
  };

  public static String convertToHex(byte[] raw) throws UnsupportedEncodingException {
    StringBuilder sb = new StringBuilder(raw.length);
    for (byte b : raw) {
      int v = b & 0xFF;
      sb.append((char) HEX_CHAR_TABLE[v >>> 4]);
      sb.append((char) HEX_CHAR_TABLE[v & 0xF]);
    }
    return sb.toString();
  }
}

@@ -839,359 +839,138 @@
     
-try %7B%0A      MessageDigest md = MessageDigest.getInstance(%22SHA-1%22);%0A      md.update(bytes, 0, bytes.length);%0A      byte%5B%5D sha1hash = md.digest();%0A      return convertToHex(sha1hash);%0A    %7D catch (NoSuchAlgorithmException e) %7B%0A      throw new RuntimeException(e);%0A    %7D catch (UnsupportedEncodingException e) %7B%0A      throw new RuntimeException(e);%0A    %7D
+return makeHash(bytes, %22SHA-1%22);%0A  %7D%0A%0A  public static String makeSHA256Hash(byte%5B%5D bytes) %7B%0A    return makeHash(bytes, %22SHA-256%22);
 %0A  %7D
@@ -1633,357 +1633,38 @@
     
-try %7B%0A      MessageDigest md = MessageDigest.getInstance(%22MD5%22);%0A      md.update(bytes, 0, bytes.length);%0A      byte%5B%5D sha1hash = md.digest();%0A      return convertToHex(sha1hash);%0A    %7D catch (NoSuchAlgorithmException e) %7B%0A      throw new RuntimeException(e);%0A    %7D catch (UnsupportedEncodingException e) %7B%0A      throw new RuntimeException(e);%0A    %7D
+return makeHash(bytes, %22MD5%22);
 %0A  %7D
@@ -2220,16 +2220,16 @@
 ;%0A    %7D%0A
-
     retu
@@ -2249,11 +2249,433 @@
 g();%0A  %7D
+%0A%0A  private static String makeHash(byte%5B%5D bytes, String algorithm) %7B%0A    try %7B%0A      MessageDigest md = MessageDigest.getInstance(algorithm);%0A      md.update(bytes, 0, bytes.length);%0A      byte%5B%5D hash = md.digest();%0A      return convertToHex(hash);%0A    %7D catch (NoSuchAlgorithmException e) %7B%0A      throw new RuntimeException(e);%0A    %7D catch (UnsupportedEncodingException e) %7B%0A      throw new RuntimeException(e);%0A    %7D%0A  %7D
 %0A%7D%0A

Add SHA-256 to SecureHashUtil