From ac4ab7b08e58e22a1126f3217cf812926929bc79 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Fri, 24 Jun 2022 19:02:13 +0000 Subject: SyntheticPasswordManager: consolidate hex encoding logic Remove the in-class implementation of hex encoding and just use HexEncoding, which was already being used in one place. No change in behavior since the encoding remains the same. In particular, upper case A-F is still used instead of lower case a-f. Test: atest SyntheticPasswordTests Change-Id: I493e4b894f76670bcb9cba8a8a774f3f5832e769 --- .../locksettings/SyntheticPasswordManager.java | 20 +++++--------------- .../server/locksettings/SyntheticPasswordTests.java | 7 +++++++ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java b/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java index 111ffd29228c..5fdfe1983f0a 100644 --- a/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java +++ b/services/core/java/com/android/server/locksettings/SyntheticPasswordManager.java @@ -272,9 +272,8 @@ public class SyntheticPasswordManager { * AuthenticationToken.mSyntheticPassword for details on what each block means. */ private void recreate(byte[] escrowSplit0, byte[] escrowSplit1) { - mSyntheticPassword = String.valueOf(HexEncoding.encode( - SyntheticPasswordCrypto.personalisedHash( - PERSONALIZATION_SP_SPLIT, escrowSplit0, escrowSplit1))).getBytes(); + mSyntheticPassword = bytesToHex(SyntheticPasswordCrypto.personalisedHash( + PERSONALIZATION_SP_SPLIT, escrowSplit0, escrowSplit1)); } /** @@ -1415,18 +1414,9 @@ public class SyntheticPasswordManager { return result; } - protected static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(); - private static byte[] bytesToHex(byte[] bytes) { - if (bytes == null) { - return "null".getBytes(); - } - byte[] hexBytes = new byte[bytes.length * 2]; - for ( int j = 0; j < bytes.length; j++ ) { - int v = bytes[j] & 0xFF; - hexBytes[j * 2] = HEX_ARRAY[v >>> 4]; - hexBytes[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; - } - return hexBytes; + @VisibleForTesting + static byte[] bytesToHex(byte[] bytes) { + return HexEncoding.encodeToString(bytes).getBytes(); } /** diff --git a/services/tests/servicestests/src/com/android/server/locksettings/SyntheticPasswordTests.java b/services/tests/servicestests/src/com/android/server/locksettings/SyntheticPasswordTests.java index c0a38b874914..b5eea70f57a4 100644 --- a/services/tests/servicestests/src/com/android/server/locksettings/SyntheticPasswordTests.java +++ b/services/tests/servicestests/src/com/android/server/locksettings/SyntheticPasswordTests.java @@ -567,6 +567,13 @@ public class SyntheticPasswordTests extends BaseLockSettingsServiceTests { } } + @Test + public void testHexEncodingIsUppercase() { + final byte[] raw = new byte[] { (byte)0xAB, (byte)0xCD, (byte)0xEF }; + final byte[] expected = new byte[] { 'A', 'B', 'C', 'D', 'E', 'F' }; + assertArrayEquals(expected, SyntheticPasswordManager.bytesToHex(raw)); + } + // b/62213311 //TODO: add non-migration work profile case, and unify/un-unify transition. //TODO: test token after user resets password -- cgit v1.2.3-59-g8ed1b