Loading keystore/java/android/security/KeyStore.java +30 −11 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ import android.net.LocalSocket; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; /** Loading Loading @@ -57,12 +58,12 @@ public class KeyStore { public byte[] get(byte[] key) { ArrayList<byte[]> values = execute('g', key); return (values == null || values.size() == 0) ? null : values.get(0); return (values == null || values.isEmpty()) ? null : values.get(0); } public String get(String key) { byte[] value = get(key.getBytes()); return (value == null) ? null : new String(value); byte[] value = get(getBytes(key)); return (value == null) ? null : toString(value); } public boolean put(byte[] key, byte[] value) { Loading @@ -71,7 +72,7 @@ public class KeyStore { } public boolean put(String key, String value) { return put(key.getBytes(), value.getBytes()); return put(getBytes(key), getBytes(value)); } public boolean delete(byte[] key) { Loading @@ -80,7 +81,7 @@ public class KeyStore { } public boolean delete(String key) { return delete(key.getBytes()); return delete(getBytes(key)); } public boolean contains(byte[] key) { Loading @@ -89,7 +90,7 @@ public class KeyStore { } public boolean contains(String key) { return contains(key.getBytes()); return contains(getBytes(key)); } public byte[][] saw(byte[] prefix) { Loading @@ -98,13 +99,13 @@ public class KeyStore { } public String[] saw(String prefix) { byte[][] values = saw(prefix.getBytes()); byte[][] values = saw(getBytes(prefix)); if (values == null) { return null; } String[] strings = new String[values.length]; for (int i = 0; i < values.length; ++i) { strings[i] = new String(values[i]); strings[i] = toString(values[i]); } return strings; } Loading @@ -120,7 +121,7 @@ public class KeyStore { } public boolean password(String oldPassword, String newPassword) { return password(oldPassword.getBytes(), newPassword.getBytes()); return password(getBytes(oldPassword), getBytes(newPassword)); } public boolean password(byte[] password) { Loading @@ -128,7 +129,7 @@ public class KeyStore { } public boolean password(String password) { return password(password.getBytes()); return password(getBytes(password)); } public boolean lock() { Loading @@ -142,7 +143,7 @@ public class KeyStore { } public boolean unlock(String password) { return unlock(password.getBytes()); return unlock(getBytes(password)); } public int getLastError() { Loading Loading @@ -208,4 +209,22 @@ public class KeyStore { } return null; } private static byte[] getBytes(String string) { try { return string.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { // will never happen throw new RuntimeException(e); } } private static String toString(byte[] bytes) { try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { // will never happen throw new RuntimeException(e); } } } keystore/tests/src/android/security/KeyStoreTest.java +11 −0 Original line number Diff line number Diff line Loading @@ -39,6 +39,9 @@ public class KeyStoreTest extends ActivityUnitTestCase<Activity> { private static final String TEST_KEYNAME2 = "testkey2"; private static final String TEST_KEYVALUE = "test value"; // "Hello, World" in Chinese private static final String TEST_I18N = "\u4F60\u597D, \u4E16\u754C"; private KeyStore mKeyStore = null; public KeyStoreTest() { Loading Loading @@ -83,6 +86,14 @@ public class KeyStoreTest extends ActivityUnitTestCase<Activity> { assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); } public void testI18n() throws Exception { assertFalse(mKeyStore.put(TEST_I18N, TEST_I18N)); assertFalse(mKeyStore.contains(TEST_I18N)); mKeyStore.password(TEST_I18N); assertTrue(mKeyStore.put(TEST_I18N, TEST_I18N)); assertTrue(mKeyStore.contains(TEST_I18N)); } public void testDelete() throws Exception { assertTrue(mKeyStore.delete(TEST_KEYNAME)); mKeyStore.password(TEST_PASSWD); Loading Loading
keystore/java/android/security/KeyStore.java +30 −11 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ import android.net.LocalSocket; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; /** Loading Loading @@ -57,12 +58,12 @@ public class KeyStore { public byte[] get(byte[] key) { ArrayList<byte[]> values = execute('g', key); return (values == null || values.size() == 0) ? null : values.get(0); return (values == null || values.isEmpty()) ? null : values.get(0); } public String get(String key) { byte[] value = get(key.getBytes()); return (value == null) ? null : new String(value); byte[] value = get(getBytes(key)); return (value == null) ? null : toString(value); } public boolean put(byte[] key, byte[] value) { Loading @@ -71,7 +72,7 @@ public class KeyStore { } public boolean put(String key, String value) { return put(key.getBytes(), value.getBytes()); return put(getBytes(key), getBytes(value)); } public boolean delete(byte[] key) { Loading @@ -80,7 +81,7 @@ public class KeyStore { } public boolean delete(String key) { return delete(key.getBytes()); return delete(getBytes(key)); } public boolean contains(byte[] key) { Loading @@ -89,7 +90,7 @@ public class KeyStore { } public boolean contains(String key) { return contains(key.getBytes()); return contains(getBytes(key)); } public byte[][] saw(byte[] prefix) { Loading @@ -98,13 +99,13 @@ public class KeyStore { } public String[] saw(String prefix) { byte[][] values = saw(prefix.getBytes()); byte[][] values = saw(getBytes(prefix)); if (values == null) { return null; } String[] strings = new String[values.length]; for (int i = 0; i < values.length; ++i) { strings[i] = new String(values[i]); strings[i] = toString(values[i]); } return strings; } Loading @@ -120,7 +121,7 @@ public class KeyStore { } public boolean password(String oldPassword, String newPassword) { return password(oldPassword.getBytes(), newPassword.getBytes()); return password(getBytes(oldPassword), getBytes(newPassword)); } public boolean password(byte[] password) { Loading @@ -128,7 +129,7 @@ public class KeyStore { } public boolean password(String password) { return password(password.getBytes()); return password(getBytes(password)); } public boolean lock() { Loading @@ -142,7 +143,7 @@ public class KeyStore { } public boolean unlock(String password) { return unlock(password.getBytes()); return unlock(getBytes(password)); } public int getLastError() { Loading Loading @@ -208,4 +209,22 @@ public class KeyStore { } return null; } private static byte[] getBytes(String string) { try { return string.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { // will never happen throw new RuntimeException(e); } } private static String toString(byte[] bytes) { try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { // will never happen throw new RuntimeException(e); } } }
keystore/tests/src/android/security/KeyStoreTest.java +11 −0 Original line number Diff line number Diff line Loading @@ -39,6 +39,9 @@ public class KeyStoreTest extends ActivityUnitTestCase<Activity> { private static final String TEST_KEYNAME2 = "testkey2"; private static final String TEST_KEYVALUE = "test value"; // "Hello, World" in Chinese private static final String TEST_I18N = "\u4F60\u597D, \u4E16\u754C"; private KeyStore mKeyStore = null; public KeyStoreTest() { Loading Loading @@ -83,6 +86,14 @@ public class KeyStoreTest extends ActivityUnitTestCase<Activity> { assertTrue(mKeyStore.put(TEST_KEYNAME, TEST_KEYVALUE)); } public void testI18n() throws Exception { assertFalse(mKeyStore.put(TEST_I18N, TEST_I18N)); assertFalse(mKeyStore.contains(TEST_I18N)); mKeyStore.password(TEST_I18N); assertTrue(mKeyStore.put(TEST_I18N, TEST_I18N)); assertTrue(mKeyStore.contains(TEST_I18N)); } public void testDelete() throws Exception { assertTrue(mKeyStore.delete(TEST_KEYNAME)); mKeyStore.password(TEST_PASSWD); Loading