# HG changeset patch # User Jiří Pinkava # Date 1359325472 -3600 # Node ID a8127b6c243e73ec0e8d75f16d21279cd4ff8537 # Parent 8daca77fabc154d4c7a158e18e96570c7a3f50a3 smack.sasl: simplify conversion bytes->hex string diff -r 8daca77fabc1 -r a8127b6c243e src/com/beem/project/beem/smack/sasl/ScramSaslClient.java --- a/src/com/beem/project/beem/smack/sasl/ScramSaslClient.java Mon Jan 21 23:13:22 2013 +0100 +++ b/src/com/beem/project/beem/smack/sasl/ScramSaslClient.java Sun Jan 27 23:24:32 2013 +0100 @@ -49,6 +49,9 @@ private static final int NONCE_BYTE_COUNT = 32; private static final int NONCE_HEX_COUNT = 2 * NONCE_BYTE_COUNT; + private static final char[] hexChars = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; private SCRAMSHA1ClientAuthenticator clientAuthenticator; private CallbackHandler cbh; private String authzid; @@ -248,10 +251,11 @@ prng = SecureRandom.getInstance("SHA1PRNG"); prng.nextBytes(nonceBytes); for (int i = 0; i < NONCE_BYTE_COUNT; i++) { + final int val = nonceBytes[i] & 0xff; //low nibble - hexNonce[i * 2] = getHexChar((byte) (nonceBytes[i] & 0x0f)); + hexNonce[i * 2] = hexChars[val / 0x10]; //high nibble - hexNonce[(i * 2) + 1] = getHexChar((byte) ((nonceBytes[i] & 0xf0) >> 4)); + hexNonce[(i * 2) + 1] = hexChars[val % 0x10]; } return new String(hexNonce); } catch (NoSuchAlgorithmException e) { @@ -289,50 +293,4 @@ throw new SaslException("Cannot get userid/password", e); } } - - /** - * This function returns hex character representing the value of the input. - * - * @param value Input value in byte - * - * @return Hex value of the Input byte value - */ - private static char getHexChar(byte value) { - switch (value) { - case 0: - return '0'; - case 1: - return '1'; - case 2: - return '2'; - case 3: - return '3'; - case 4: - return '4'; - case 5: - return '5'; - case 6: - return '6'; - case 7: - return '7'; - case 8: - return '8'; - case 9: - return '9'; - case 10: - return 'a'; - case 11: - return 'b'; - case 12: - return 'c'; - case 13: - return 'd'; - case 14: - return 'e'; - case 15: - return 'f'; - default: - return 'Z'; - } - } }