using Tango.WiFi.Win32.Interop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Tango.WiFi
{
internal static class EapUserFactory
{
///
/// Generates the EAP user XML
///
internal static string Generate(Dot11CipherAlgorithm cipher, string username, string password, string domain)
{
string profile = string.Empty;
string template = string.Empty;
switch (cipher)
{
case Dot11CipherAlgorithm.CCMP: // WPA-2
case Dot11CipherAlgorithm.TKIP: // WPA
template = GetTemplate("PEAP-MS-CHAPv2");
profile = string.Format(template, username, FixPass(password), domain);
break;
default:
throw new NotImplementedException("Profile for selected cipher algorithm is not implemented");
}
return profile;
}
///
/// Fetches the template for an EAP user
///
private static string GetTemplate(string name)
{
string resourceName = string.Format("Tango.WiFi.EapUserXML.{0}.xml", name);
using (StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)))
{
return reader.ReadToEnd();
}
}
private static string FixPass(string pass)
{
pass = EncodeToBase64(pass);
pass = pass.Replace("&", "&");
pass = pass.Replace("<", "<");
pass = pass.Replace(">", ">");
return pass;
}
private static string EncodeToBase64(string toEncode)
{
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
}
}