smack.sasl: simplify conversion bytes->hex string
authorJiří Pinkava <j-pi@seznam.cz>
Sun, 27 Jan 2013 23:24:32 +0100
changeset 1019 a8127b6c243e
parent 1018 8daca77fabc1
child 1020 d8ef6a82d715
smack.sasl: simplify conversion bytes->hex string
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';
-	}
-    }
 }