package com.carrotcreative.cream.util;

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

import android.support.annotation.NonNull;

public class HashingUtil
{
    private static final int CACHE_SIZE = 50;
    private static LruCache<String, String> mCache = new LruCache<>(CACHE_SIZE);

    private HashingUtil() {}

    /**
     *
     * @param secret The string that needs to be hashed.
     * @return The hashed value.
     */
    public static String getHash(@NonNull String secret)
    {
        String hashedValue = mCache.get(secret);
        if(hashedValue == null)
        {
            hashedValue = generateHash(secret);
            mCache.put(secret, hashedValue);
        }

        return hashedValue;
    }

    private static String generateHash(String secret)
    {
        String hashedString = null;
        try
        {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(secret.getBytes("UTF-8"));
            hashedString = new String(messageDigest.digest());
        }
        catch (NoSuchAlgorithmException e) { /* Do nothing */ }
        catch (UnsupportedEncodingException e) { /* Do nothing */ }

        if(hashedString == null)
        {
            hashedString = secret.replaceAll("\\s+", "_");
        }
        return hashedString;

    }
}

@@ -279,16 +279,93 @@
 E = 50;%0A
+    private static final char%5B%5D hexArray = %220123456789ABCDEF%22.toCharArray();%0A
     priv
@@ -1163,18 +1163,18 @@
 g = 
-new String
+bytesToHex
 (mes
@@ -1479,16 +1479,378 @@
 String;%0A
+    %7D%0A%0A    public static String bytesToHex(byte%5B%5D bytes)%0A    %7B%0A        char%5B%5D hexChars = new char%5Bbytes.length * 2%5D;%0A        for ( int j = 0; j %3C bytes.length; j++ )%0A        %7B%0A            int v = bytes%5Bj%5D & 0xFF;%0A            hexChars%5Bj * 2%5D = hexArray%5Bv %3E%3E%3E 4%5D;%0A            hexChars%5Bj * 2 + 1%5D = hexArray%5Bv & 0x0F%5D;%0A        %7D%0A        return new String(hexChars);
 %0A    %7D%0A%7D

Add bytesToHex function