Skip to content

Instantly share code, notes, and snippets.

@mnirm
Last active December 30, 2020 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnirm/a69f33a6b5ae8939ecde238189addccf to your computer and use it in GitHub Desktop.
Save mnirm/a69f33a6b5ae8939ecde238189addccf to your computer and use it in GitHub Desktop.
Write Wifi Network to NDEF tag: WifiHelper for writing a payload to your ndef tag.
// Author:Github - @mnirm
export class WifiHelper {
CryptoType: CryptoType = new CryptoType();
AuthType: AuthType = new AuthType();
public CredsToPayload(WifiCredentials: IWiFiCredentials){
let payload = [0x10, 0x0E]; // Credential same for every wifi network
// Network ID
payload.push(0x10, 0x26, 0x00, 0x01, 0x01); // Network ID standard no parameter
// Network name
payload.push(0x10, 0x45); // Network name label "Network Name:" part in text
let networkNameArr = this.stringToHexArr(WifiCredentials.NetworkName); // Convert to hexadecimal and get length
if(networkNameArr.length < 255){ // Check if network name is smaller than first byte
payload.push(0x00);
payload.push(networkNameArr.length);
}
payload = payload.concat(networkNameArr); // Add network name
// Authentication
payload.push(0x10, 0x03); // Authentication label
payload.push(0x00, 0x02); // Length bytes authentication type
payload = payload.concat(WifiCredentials.AuthenticationType); // Add authentication type
// Encryption
payload.push(0x10, 0x0F); // Encryption label
payload.push(0x00, 0x02); // Length bytes encryption type
payload = payload.concat(WifiCredentials.EncryptionType); // Add authentication type
// Network key
payload.push(0x10, 0x27); // Network key label
let networkKeyArr = this.stringToHexArr(WifiCredentials.NetworkKey); // Convert to hexadecimal and get length
if(networkKeyArr.length < 255){ // Check if network key is smaller than first byte
payload.push(0x00);
payload.push(networkKeyArr.length);
}
payload = payload.concat(networkKeyArr); // Add network key
// Mac Address
payload.push(0x10, 0x20); // Mac addres label
payload.push(0x00, 0x06); // Length mac address
payload.push(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); // Broadcast address
let payloadSlice = payload.length - 2;
payload.splice(2, 0, 0x00, payloadSlice); // Adding amount of bytes in payload - 4 on the second place in the array
return payload;
}
private stringToHexArr (text) {
var decimals = [];
for (var i = 0; i < text.length; i += 1) {
decimals.push(text.charCodeAt(i));
}
return decimals;
}
}
export class CryptoType {
public static CRYPT_NONE = [0x00, 0x01];
public static CRYPT_WEP = [0x00, 0x02];
public static CRYPT_TKIP =[0x00, 0x04];
public static CRYPT_AES = [0x00, 0x08];
public static CRYPT_AES_TKIP = [0x00, 0x0C];
}
export class AuthType{
public static AUTH_OPEN = [0x00, 0x01];
public static AUTH_WPA_PERSONAL = [0x00, 0x02];
public static AUTH_SHARED = [0x00, 0x04];
public static AUTH_WPA_ENTERPRISE = [0x00, 0x08];
public static AUTH_WPA2_ENTERPRISE = [0x00, 0x10];
public static AUTH_WPA2_PERSONAL = [0x00, 0x20];
public static AUTH_WPA_WPA2_PERSONAL = [0x00, 0x22];
}
export interface IWiFiCredentials{
NetworkName: string,
AuthenticationType: number[],
EncryptionType: number[],
NetworkKey: string,
MacAddress?: string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment