Further simplification; correct-er handling of ASCII vs hex SSID, and password

git-svn-id: https://zxing.googlecode.com/svn/trunk@1932 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2011-09-25 11:23:33 +00:00
parent faf72f02eb
commit 92adf1a6d5

View file

@ -18,7 +18,6 @@ package com.google.zxing.client.android.wifi;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.text.TextUtils;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -29,7 +28,6 @@ import java.util.regex.Pattern;
*/ */
public final class WifiConfigManager { public final class WifiConfigManager {
private static final Pattern HEX_DIGITS_64 = Pattern.compile("[0-9A-Fa-f]{64}");
private static final Pattern HEX_DIGITS = Pattern.compile("[0-9A-Fa-f]+"); private static final Pattern HEX_DIGITS = Pattern.compile("[0-9A-Fa-f]+");
private WifiConfigManager() { private WifiConfigManager() {
@ -93,25 +91,21 @@ public final class WifiConfigManager {
config.allowedPairwiseCiphers.clear(); config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear(); config.allowedProtocols.clear();
// Android API insists that an ascii SSID must be quoted to be correctly handled. // Android API insists that an ascii SSID must be quoted to be correctly handled.
config.SSID = convertToQuotedString(ssid); config.SSID = quoteNonHex(ssid);
return config; return config;
} }
// Adding a WEP network // Adding a WEP network
private static void changeNetworkWEP(WifiManager wifiManager, String ssid, String password) { private static void changeNetworkWEP(WifiManager wifiManager, String ssid, String password) {
WifiConfiguration config = changeNetworkCommon(ssid); WifiConfiguration config = changeNetworkCommon(ssid);
if (isHexWepKey(password)) { config.wepKeys[0] = quoteNonHex(password, 10, 26, 58);
config.wepKeys[0] = password; config.wepTxKeyIndex = 0;
} else {
config.wepKeys[0] = convertToQuotedString(password);
}
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
updateNetwork(wifiManager, config); updateNetwork(wifiManager, config);
} }
@ -119,30 +113,23 @@ public final class WifiConfigManager {
private static void changeNetworkWPA(WifiManager wifiManager, String ssid, String password) { private static void changeNetworkWPA(WifiManager wifiManager, String ssid, String password) {
WifiConfiguration config = changeNetworkCommon(ssid); WifiConfiguration config = changeNetworkCommon(ssid);
// Hex passwords that are 64 bits long are not to be quoted. // Hex passwords that are 64 bits long are not to be quoted.
if (HEX_DIGITS_64.matcher(password).matches()) { config.preSharedKey = quoteNonHex(password, 64);
config.preSharedKey = password;
} else {
config.preSharedKey = convertToQuotedString(password);
}
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
// For WPA config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
// For WPA2
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
updateNetwork(wifiManager, config); updateNetwork(wifiManager, config);
} }
// Adding an open, unsecured network // Adding an open, unsecured network
private static void changeNetworkUnEncrypted(WifiManager wifiManager, String ssid) { private static void changeNetworkUnEncrypted(WifiManager wifiManager, String ssid) {
WifiConfiguration config = changeNetworkCommon(ssid); WifiConfiguration config = changeNetworkCommon(ssid);
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
updateNetwork(wifiManager, config); updateNetwork(wifiManager, config);
} }
@ -156,6 +143,10 @@ public final class WifiConfigManager {
return null; return null;
} }
private static String quoteNonHex(String value, int... allowedLengths) {
return isHexOfLength(value, allowedLengths) ? value : convertToQuotedString(value);
}
/** /**
* Encloses the incoming string inside double quotes, if it isn't already quoted. * Encloses the incoming string inside double quotes, if it isn't already quoted.
* @param string the input string * @param string the input string
@ -163,32 +154,35 @@ public final class WifiConfigManager {
* as well. * as well.
*/ */
private static String convertToQuotedString(String string) { private static String convertToQuotedString(String string) {
if (string == null){ if (string == null || string.length() == 0) {
return null; return null;
} }
if (TextUtils.isEmpty(string)) { // If already quoted, return as-is
return ""; if (string.charAt(0) == '"' && string.charAt(string.length() - 1) == '"') {
}
int lastPos = string.length() - 1;
if (lastPos < 0 || (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
return string; return string;
} }
return '\"' + string + '\"'; return '\"' + string + '\"';
} }
/** /**
* Check if wepKey is a valid hexadecimal string. * @param value input to check
* @param wepKey the input to be checked * @param allowedLengths allowed lengths, if any
* @return true if the input string is indeed hex or empty. False if the input string is non-hex * @return true if value is a non-null, non-empty string of hex digits, and if allowed lengths are given, has
* or null. * an allowed length
*/ */
private static boolean isHexWepKey(CharSequence wepKey) { private static boolean isHexOfLength(CharSequence value, int... allowedLengths) {
if (wepKey == null) { if (value == null || !HEX_DIGITS.matcher(value).matches()) {
return false; return false;
} }
int length = wepKey.length(); if (allowedLengths.length == 0) {
// WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?) return true;
return (length == 10 || length == 26 || length == 58) && HEX_DIGITS.matcher(wepKey).matches(); }
for (int length : allowedLengths) {
if (value.length() == length) {
return true;
}
}
return false;
} }
} }