diff options
author | Samuel Holland <samuel@sholland.org> | 2018-04-30 11:39:12 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-04-30 11:39:12 -0500 |
commit | 7eedf08d4bbe8d15a38cde777d92e72930ffa2e8 (patch) | |
tree | 12293719d001f3eef3a54244688279e62e2513a1 /app/src/main/java/com/wireguard/crypto/KeyEncoding.java | |
parent | app: Sort dependencies (diff) | |
download | wireguard-android-7eedf08d4bbe8d15a38cde777d92e72930ffa2e8.tar.xz wireguard-android-7eedf08d4bbe8d15a38cde777d92e72930ffa2e8.zip |
global: Automatic code formatting
Signed-off-by: Samuel Holland <samuel@sholland.org>
Diffstat (limited to 'app/src/main/java/com/wireguard/crypto/KeyEncoding.java')
-rw-r--r-- | app/src/main/java/com/wireguard/crypto/KeyEncoding.java | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/app/src/main/java/com/wireguard/crypto/KeyEncoding.java b/app/src/main/java/com/wireguard/crypto/KeyEncoding.java index 92e03230..ba867668 100644 --- a/app/src/main/java/com/wireguard/crypto/KeyEncoding.java +++ b/app/src/main/java/com/wireguard/crypto/KeyEncoding.java @@ -13,10 +13,10 @@ public final class KeyEncoding { public static final int KEY_LENGTH_HEX = 64; private static final String KEY_LENGTH_BASE64_EXCEPTION_MESSAGE = "WireGuard base64 keys must be 44 characters encoding 32 bytes"; - private static final String KEY_LENGTH_HEX_EXCEPTION_MESSAGE = - "WireGuard hex keys must be 64 characters encoding 32 bytes"; private static final String KEY_LENGTH_EXCEPTION_MESSAGE = "WireGuard keys must be 32 bytes"; + private static final String KEY_LENGTH_HEX_EXCEPTION_MESSAGE = + "WireGuard hex keys must be 64 characters encoding 32 bytes"; private KeyEncoding() { // Prevent instantiation. @@ -82,23 +82,6 @@ public final class KeyEncoding { return key; } - public static String keyToBase64(final byte[] key) { - final char[] output = new char[KEY_LENGTH_BASE64]; - if (key.length != KEY_LENGTH) - throw new IllegalArgumentException(KEY_LENGTH_EXCEPTION_MESSAGE); - int i; - for (i = 0; i < KEY_LENGTH / 3; ++i) - encodeBase64(key, i * 3, output, i * 4); - final byte[] endSegment = { - key[i * 3], - key[i * 3 + 1], - 0, - }; - encodeBase64(endSegment, 0, output, i * 4); - output[KEY_LENGTH_BASE64 - 1] = '='; - return new String(output); - } - public static byte[] keyFromHex(final String str) { final char[] input = str.toCharArray(); final byte[] key = new byte[KEY_LENGTH]; @@ -110,7 +93,7 @@ public final class KeyEncoding { for (int i = 0; i < KEY_LENGTH_HEX; ++i) { c = input[i]; c_num = c ^ 48; - c_num0 = (c_num - 10) >>8; + c_num0 = (c_num - 10) >> 8; c_alpha = (c & ~32) - 55; c_alpha0 = ((c_alpha - 10) ^ (c_alpha - 16)) >> 8; if ((c_num0 | c_alpha0) == 0) @@ -119,19 +102,36 @@ public final class KeyEncoding { if (state == 0) c_acc = c_val * 16; else - key[i / 2] = (byte)(c_acc | c_val); + key[i / 2] = (byte) (c_acc | c_val); state = ~state; } return key; } + public static String keyToBase64(final byte[] key) { + final char[] output = new char[KEY_LENGTH_BASE64]; + if (key.length != KEY_LENGTH) + throw new IllegalArgumentException(KEY_LENGTH_EXCEPTION_MESSAGE); + int i; + for (i = 0; i < KEY_LENGTH / 3; ++i) + encodeBase64(key, i * 3, output, i * 4); + final byte[] endSegment = { + key[i * 3], + key[i * 3 + 1], + 0, + }; + encodeBase64(endSegment, 0, output, i * 4); + output[KEY_LENGTH_BASE64 - 1] = '='; + return new String(output); + } + public static String keyToHex(final byte[] key) { final char[] output = new char[KEY_LENGTH_HEX]; if (key.length != KEY_LENGTH) throw new IllegalArgumentException(KEY_LENGTH_EXCEPTION_MESSAGE); for (int i = 0; i < KEY_LENGTH; ++i) { - output[i * 2] = (char)(87 + (key[i] >> 4 & 0xf) + ((((key[i] >> 4 & 0xf) - 10) >> 8) & ~38)); - output[i * 2 + 1] = (char)(87 + (key[i] & 0xf) + ((((key[i] & 0xf) - 10) >> 8) & ~38)); + output[i * 2] = (char) (87 + (key[i] >> 4 & 0xf) + ((((key[i] >> 4 & 0xf) - 10) >> 8) & ~38)); + output[i * 2 + 1] = (char) (87 + (key[i] & 0xf) + ((((key[i] & 0xf) - 10) >> 8) & ~38)); } return new String(output); } |