-
-
Save krllus/6032f02e6e39d81caa29ba07cb285f10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MIT License | |
Copyright (c) 2016 Henry Chladil | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
private NdefRecord createWifiRecord(String[] data) { | |
String ssid = data[0]; | |
String password = data[1]; | |
String auth = data[2]; | |
String crypt = data[3]; | |
byte[] authByte = getAuthBytes(auth); | |
byte[] cryptByte = getCryptBytes(crypt); | |
byte[] ssidByte = ssid.getBytes(); | |
byte[] passwordByte = password.getBytes(); | |
byte[] ssidLength = {(byte)((int)Math.floor(ssid.length()/256)), (byte)(ssid.length()%256)}; | |
byte[] passwordLength = {(byte)((int)Math.floor(password.length()/256)), (byte)(password.length()%256)}; | |
byte[] cred = {0x00, 0x36}; | |
byte[] idx = {0x00, 0x01, 0x01}; | |
byte[] mac = {0x00, 0x06}; | |
byte[] keypad = {0x00, 0x0B}; | |
byte[] payload = concat(NFCHelper.CREDENTIAL, cred, | |
NFCHelper.NETWORK_IDX, idx, | |
NFCHelper.NETWORK_NAME, ssidLength, ssidByte, | |
NFCHelper.AUTH_TYPE, NFCHelper.AUTH_WPA_PERSONAL, authByte, | |
NFCHelper.CRYPT_TYPE, NFCHelper.CRYPT_WEP, NFCHelper.CRYPT_AES_TKIP, | |
NFCHelper.NETWORK_KEY, passwordLength, passwordByte); | |
// NFCHelper.MAC_ADDRESS, mac); | |
return NdefRecord.createMime(NFC_TOKEN_MIME_TYPE, payload); | |
} | |
private static byte[] getAuthBytes(String auth) { | |
byte[] authByte = {0x00}; | |
if (auth.equalsIgnoreCase("Open")) { | |
authByte = NfcHelper.WPS_AUTH_OPEN; | |
} else if (auth.equalsIgnoreCase("wpa personal")) { | |
authByte = NfcHelper.WPS_AUTH_WPA_PERSONAL; | |
} else if (auth.equalsIgnoreCase("shared")) { | |
authByte = NfcHelper.WPS_AUTH_SHARED; | |
} else if (auth.equalsIgnoreCase("wpa enterprise")) { | |
authByte = NfcHelper.WPS_AUTH_WPA_ENTERPRISE; | |
} else if (auth.equalsIgnoreCase("wpa2 enterprise")) { | |
authByte = NfcHelper.WPS_AUTH_WPA2_ENTERPRISE; | |
} else if (auth.equalsIgnoreCase("WPA2 Personal")) { | |
authByte = NfcHelper.WPS_AUTH_WPA2_PERSONAL; | |
} else if (auth.equalsIgnoreCase("WPA/WPA2 Personal")) { | |
authByte = NfcHelper.WPS_AUTH_WPA_WPA2_PERSONAL; | |
} else { | |
authByte = NfcHelper.WPS_AUTH_WPA2_PERSONAL; | |
} | |
return authByte; | |
} | |
private static byte[] getCryptBytes(String crypt) { | |
byte[] cryptByte = {0x00, 0x00}; | |
if (crypt.equalsIgnoreCase("wep")) { | |
cryptByte = NfcHelper.WPS_CRYPT_WEP; | |
} else if (crypt.equalsIgnoreCase("tkip")) { | |
cryptByte = NfcHelper.WPS_CRYPT_TKIP; | |
} else if (crypt.equalsIgnoreCase("AES")) { | |
cryptByte = NfcHelper.WPS_CRYPT_AES; | |
} else if (crypt.equalsIgnoreCase("aes/tkip")) { | |
cryptByte = NfcHelper.WPS_CRYPT_AES_TKIP; | |
} else { | |
cryptByte = NfcHelper.WPS_CRYPT_NONE; | |
} | |
return cryptByte; | |
} | |
//public static final byte[] WPS_VERSION = {0x10, 0x4A}; | |
public static final byte[] WPS_CREDENTIALS = {0x10, 0x0e}; | |
public static final byte[] WPS_AUTH_TYPE = {0x10, 0x03}; | |
public static final byte[] WPS_CRYPT_TYPE = {0x10, 0x0F}; | |
//public static final byte[] WPS_MAC_ADDRESS = {0x10, 0x20}; | |
public static final byte[] WPS_NETWORK_IDX = {0x10, 0x26}; | |
public static final byte[] WPS_NETWORK_KEY = {0x10, 0x27}; | |
public static final byte[] WPS_NETWORK_NAME = {0x10, 0x45}; | |
//public static final byte[] WPS_OOB_PASSWORD = {0x10, 0x2C}; | |
//public static final byte[] WPS_VENDOR_EXT = {0x10, 0x49}; | |
//public static final byte[] WPS_VENDOR_WFA = {0x00, 0x37, 0x2A}; | |
//public static final byte[] WPS_VERSION2 = {0x00}; | |
//public static final byte[] WPS_KEY_SHAREABLE = {0x02}; | |
public static final byte[] WPS_AUTH_OPEN = {0x00, 0x01}; | |
public static final byte[] WPS_AUTH_WPA_PERSONAL = {0x00, 0x02}; | |
public static final byte[] WPS_AUTH_SHARED = {0x00, 0x04}; | |
public static final byte[] WPS_AUTH_WPA_ENTERPRISE = {0x00, 0x08}; | |
public static final byte[] WPS_AUTH_WPA2_ENTERPRISE = {0x00, 0x10}; | |
public static final byte[] WPS_AUTH_WPA2_PERSONAL = {0x00, 0x20}; | |
public static final byte[] WPS_AUTH_WPA_WPA2_PERSONAL = {0x00, 0x22}; | |
public static final byte[] WPS_CRYPT_NONE = {0x00, 0x01}; | |
public static final byte[] WPS_CRYPT_WEP = {0x00, 0x02}; | |
public static final byte[] WPS_CRYPT_TKIP = {0x00, 0x04}; | |
public static final byte[] WPS_CRYPT_AES = {0x00, 0x08}; | |
public static final byte[] WPS_CRYPT_AES_TKIP = {0x00, 0x0C}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment