Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 43a53526 authored by Jozef BABJAK's avatar Jozef BABJAK Committed by Steve Kondik
Browse files

Performance of toHex() improved.

- Constant array with hex digits instead of re-creation of String.
- Concatenating both values at one go.
- Without necessity of various wrapper objects.

According my benchmark, the new implementation is more than 5 times
faster.

Change-Id: I90cc4a31db4272e245228cb3163846841adf427c
parent 8cc233a7
Loading
Loading
Loading
Loading
+16 −8
Original line number Diff line number Diff line
@@ -506,21 +506,29 @@ public class LockPatternUtils {
            byte[] saltedPassword = (password + getSalt()).getBytes();
            byte[] sha1 = MessageDigest.getInstance(algo = "SHA-1").digest(saltedPassword);
            byte[] md5 = MessageDigest.getInstance(algo = "MD5").digest(saltedPassword);
            hashed = (toHex(sha1) + toHex(md5)).getBytes();
            hashed = toHex(sha1, md5);
        } catch (NoSuchAlgorithmException e) {
            Log.w(TAG, "Failed to encode string because of missing algorithm: " + algo);
        }
        return hashed;
    }

    private static String toHex(byte[] ary) {
        final String hex = "0123456789ABCDEF";
        String ret = "";
        for (int i = 0; i < ary.length; i++) {
            ret += hex.charAt((ary[i] >> 4) & 0xf);
            ret += hex.charAt(ary[i] & 0xf);
    private static final byte[] HEX_CHARS = new byte[]{
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
    };

    private static byte[] toHex(final byte[] array1, final byte[] array2) {
        final byte[] result = new byte[(array1.length + array2.length) * 2];
        int i = 0;
        for (final byte b : array1) {
            result[i++] = HEX_CHARS[(b >> 4) & 0xf];
            result[i++] = HEX_CHARS[b & 0xf];
        }
        return ret;
        for (final byte b : array2) {
            result[i++] = HEX_CHARS[(b >> 4) & 0xf];
            result[i++] = HEX_CHARS[b & 0xf];
        }
        return result;
    }

    /**