Utils Class (used by GetSCT methods)

Some have wanted the Utils class for some of the methods I used in the GetSCT blog.  Contains some helper methods you may use at times.  Here it is

using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Globalization;
using System.Xml;
using System.Management;
using Microsoft.Win32;
using System.Reflection;
using System.IO.IsolatedStorage;

namespace WSESimpleTCPDLL
{
 /// <summary>
 /// Summary description for Utils.
 /// </summary>
 public class Utils
 {
  private static RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

  private Utils()
  {
  }

  /// <summary>
  /// Return the first X bytes from the array.
  /// </summary>
  /// <param name="ba"></param>
  /// <param name="len"></param>
  /// <returns></returns>
  public static byte[] GetBytes(byte[] ba, int len)
  {
   if ( ba == null )
    throw new ArgumentNullException("ba");
   if ( len < 0 )
    throw new ArgumentOutOfRangeException("len must be > 0");
   if ( ba.Length < len )
    throw new ArgumentOutOfRangeException("len must be <= ba length.");
   byte[] newArray = new byte[len];
   Buffer.BlockCopy(ba, 0, newArray, 0, len);
   return newArray;
  }

  public static byte[] JoinArrays(byte[] b1, byte[] b2)
  {
   byte[] ba = new byte[b1.Length + b2.Length];
   Buffer.BlockCopy(b1, 0, ba, 0, b1.Length);
   Buffer.BlockCopy(b2, 0, ba, b1.Length, b2.Length);
   return ba;
  }

  /// <summary>
  /// Returns true if date is between start and end date inclusive.
  /// </summary>
  public static bool DateBetween(DateTime start, DateTime end, DateTime date)
  {
   if ( date >= start && date <= end )
    return true;
   return false;
  }

  public static bool ArraysEqual(byte[] ba1, byte[] ba2)
  {
   if ( ba1 == null && ba2 == null )
    return true;
   if ( ba1 == null || ba2 == null )
    return false;
   if ( ba1.Length != ba2.Length )
    return false;

   for(int i=0; i < ba1.Length; i++)
   {
    if ( ba1[i] != ba2[i] )
     return false;
   }
   return true;
  }

  public static bool IsNullOrEmpty(string s)
  {
   if ( s == null )
    return true;
   if ( s.Length == 0 )
    return true;
   return false;
  }

  public static string ToLocalDateTimeString(DateTime value)
  {
   return value.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
  }

  /// <summary>
  /// Parses the UTC formatted string, returning DateTime in UTC.
  /// </summary>
  /// <param name="value"></param>
  /// <returns></returns>
  public static DateTime FromLocalDateTimeString(string value)
  {
   return DateTime.Parse(value, CultureInfo.InvariantCulture);
  }

  /// <summary>
  /// Returns string formatted in Zulu (UTC) time.  value must be UTC DateTime.
  /// </summary>
  /// <param name="value"></param>
  /// <returns></returns>
  public static string ToUTCDateTimeString(DateTime value)
  {
   // Date expected to be UTC date already.
   // e.g 2003-10-26T14:33:41.1234567Z
   return value.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
  }

  /// <summary>
  /// Parses the UTC formatted string, returning DateTime in UTC.
  /// </summary>
  /// <param name="value"></param>
  /// <returns></returns>
  public static DateTime FromUTCDateTimeString(string value)
  {
   return DateTime.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
  }

  public static byte[] InterleaveArrays(byte[] ba1, byte[] ba2)
  {
   if ( ba1 == null  )
    throw new ArgumentNullException("ba1");
   if ( ba2 == null )
    throw new ArgumentNullException("ba2");
   if ( ba1.Length != ba2.Length )
    throw new ArgumentException("ba1 length must equal ba2 length.");
   if ( (ba1.Length % 2) > 0 )
    throw new ArgumentException("arrays must be an even length.");

   byte[] key = new byte[ba1.Length * 2];
   int step = 0;
   for(int i=0; i < ba1.Length; i++)
   {
    key[step] = ba1[i];
    key[step+1] = ba2[i];
    step+=2;
   }
   return key;
  }

  public static byte[] GetArrayFromUIntPair(uint ui1, uint ui2)
  {
   byte[] ba = new byte[8];
   byte[] ba1 = BitConverter.GetBytes(ui1);
   byte[] ba2 = BitConverter.GetBytes(ui2);
   Buffer.BlockCopy(ba1, 0, ba, 0, 4);
   Buffer.BlockCopy(ba2, 0, ba, 4, 4);
   return ba;
  }

  public static uint GetRandomUInt()
  {
   byte[] ri = GetRandomBytes(4);
   return BitConverter.ToUInt32(ri, 0);
  }

  public static byte[] GetRandomBytes(int len)
  {
   if ( len < 1 )
    throw new ArgumentOutOfRangeException("len must be greater then >= 1.");

   byte[] random = new byte[len];
   rng.GetBytes(random); // The array is now filled with cryptographically strong random bytes.
   return random;
  }

  public static byte[] RijndaelEncrypt(ICryptoTransform encryptor, byte[] data)
  {
              if ( encryptor == null )
    throw new ArgumentNullException("encryptor");
   if ( data == null )
    throw new ArgumentNullException("data");

   //Encrypt the data.
   using(MemoryStream msEncrypt = new MemoryStream())
   using(CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
   {
    //Write all data to the crypto stream and flush it.
    csEncrypt.Write(data, 0, data.Length);
    csEncrypt.FlushFinalBlock();

    //Get encrypted array of bytes.
    byte[] encrypted = msEncrypt.ToArray();
    return encrypted;
   }
  }

  public static byte[] RijndaelDecrypt(ICryptoTransform decryptor, byte[] encrypted)
  {
   if ( decryptor == null )
    throw new ArgumentNullException("decryptor");
   if ( encrypted == null )
    throw new ArgumentNullException("encrypted");

   using(MemoryStream msDecrypt = new MemoryStream(encrypted))
   using(CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
   {
    byte[] fromEncrypt = new byte[encrypted.Length];

    int read = csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
    if ( read < fromEncrypt.Length )
    {
     byte[] clearBytes = new byte[read];
     Buffer.BlockCopy(fromEncrypt, 0, clearBytes, 0, read);
     return clearBytes;
    }
    return fromEncrypt;
   }
  }

  public static string GetFormattedXML(string xml)
  {
   using (StringWriter sw = new StringWriter() )
   {
    XmlTextWriter xw = new XmlTextWriter(sw);
    xw.Formatting = Formatting.Indented;
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.LoadXml(xml);
    xmldoc.WriteTo(xw);
    xw.Close();
    return sw.ToString();
   }
  }

  public static string GetPrimaryMAC()
  {
   try
   {
    string qry = "select * FROM Win32_NetworkAdapter";
    ObjectQuery objectQuery = new ObjectQuery( qry );
    ManagementObjectSearcher searcher = new ManagementObjectSearcher( objectQuery );
    string macAdd = "";
    foreach ( ManagementObject nicObj in searcher.Get() )
    {
     macAdd = nicObj["MACAddress"].ToString();
     break;
    }
    return macAdd;
   }
   catch
   {
    return "";
   }
  }

  public static string GetAssemblyVersion(Assembly assembly, int count)
  {
   if ( assembly == null )
    throw new ArgumentNullException("assembly");
   string ver = assembly.GetName().Version.ToString(count);
   return ver;
  }

  public static string GetRegValue(RegistryKey rKey, string subKey, string value)
  {
   if ( rKey == null )
    throw new ArgumentNullException("rKey");

   try
   {
    using( RegistryKey rk = rKey.OpenSubKey(subKey) )
    {
     string result = rk.GetValue(value) as string;
     if ( result == null )
      result = "";
     return result;
    }
   }
   catch
   {
    return "";
   }
  }

 }
}

–William

Advertisements
This entry was posted in C#. Bookmark the permalink.

4 Responses to Utils Class (used by GetSCT methods)

  1. Unknown says:

    http://www.batteryfast.co.uk/acer/as07b31.htm Acer as07b31 Battery http://www.batteryfast.co.uk/acer/as07b32.htm Acer as07b32 Battery http://www.batteryfast.co.uk/acer/as07b41.htm Acer as07b41 Battery http://www.batteryfast.co.uk/acer/as07b42.htm Acer as07b42 Battery http://www.batteryfast.co.uk/acer/aspire-5520.htm Acer aspire 5520 Battery http://www.batteryfast.co.uk/acer/aspire-5920.htm Acer aspire 5920 Battery http://www.batteryfast.co.uk/acer/batbl50l6.htm Acer batbl50l6 Battery http://www.batteryfast.co.uk/apple/15-inch-macbook-pro.htm Apple 15 inch macbook pro Battery http://www.batteryfast.co.uk/apple/15-inch-powerbook-g4.htm Apple 15 inch powerbook g4 Battery http://www.batteryfast.co.uk/acer/travelmate-3260.htm Acer travelmate 3260 Battery http://www.batteryfast.co.uk/acer/travelmate-4200-series.htm Acer travelmate 4200 series Battery http://www.batteryfast.co.uk/acer/travelmate-4203-series.htm Acer travelmate 4203 series Battery http://www.batteryfast.co.uk/acer/travelmate-4230-series.htm Acer travelmate 4230 series Battery http://www.batteryfast.co.uk/apple/a1148.htm Apple a1148 Battery http://www.batteryfast.co.uk/apple/a1185.htm Apple a1185 Battery http://www.batteryfast.co.uk/apple/a1185-black.htm Apple a1185 black Battery http://www.batteryfast.co.uk/apple/a1185-white.htm Apple a1185 white Battery http://www.batteryfast.co.uk/asus/a42-a2.htm Asus a42-a2 Battery http://www.batteryfast.co.uk/dell/latitude-d620.htm Dell latitude d620 Battery http://www.batteryfast.co.uk/dell/latitude-d630.htm Dell latitude d630 Battery http://www.batteryfast.co.uk/dell/original-inspiron-1525.htm Dell original inspiron 1525 Battery http://www.batteryfast.co.uk/dell/latitude-d820.htm Dell latitude d820 Battery http://www.batteryfast.co.uk/dell/latitude-d830.htm Dell latitude d830 Battery http://www.batteryfast.co.uk/dell/vostro-1500.htm Dell vostro 1500 Battery http://www.batteryfast.co.uk/dell/vostro-1700.htm Dell vostro 1700 Battery http://www.batteryfast.co.uk/dell/xps-m1330.htm Dell xps m1330 Battery http://www.batteryfast.co.uk/dell/xps-m1530.htm Dell xps m1530 Battery http://www.batteryfast.co.uk/dell/d620.htm Dell d620 Battery http://www.batteryfast.co.uk/dell/d630.htm Dell d630 Battery http://www.batteryfast.co.uk/dell/d820.htm Dell d820 Battery http://www.batteryfast.co.uk/dell/d830.htm Dell d830 Battery http://www.batteryfast.co.uk/dell/inspiron-1520.htm Dell inspiron 1520 Battery http://www.batteryfast.co.uk/dell/6400.htm Dell 6400 Battery http://www.batteryfast.co.uk/dell/inspiron-1720.htm Dell inspiron 1720 Battery http://www.batteryfast.co.uk/dell/inspiron-5150.htm Dell inspiron 5150 Battery http://www.batteryfast.co.uk/dell/inspiron-6400.htm Dell inspiron 6400 Battery http://www.batteryfast.co.uk/gateway/m360.htm Gateway m360 Battery http://www.batteryfast.co.uk/gateway/m460.htm Gateway m460 Battery http://www.batteryfast.co.uk/gateway/m680.htm Gateway m680 Battery http://www.batteryfast.co.uk/gateway/squ-412.htm Gateway squ-412 Battery http://www.batteryfast.co.uk/hp/530.htm Hp 530 Battery http://www.batteryfast.co.uk/hp/dv6000.htm Hp dv6000 Battery http://www.batteryfast.co.uk/hp/dv2000.htm Hp dv2000 Battery http://www.batteryfast.co.uk/hp/hstnn-db42.htm Hp hstnn-db42 Battery http://www.batteryfast.co.uk/hp/nc6000.htm Hp nc6200 Battery

  2. Unknown says:

    http://www.batteryfast.com.au/asus/a31-s6.htm Asus a31-s6 Batteryhttp://www.batteryfast.com.au/asus/a32-f5.htm Asus a32-f5 Batteryhttp://www.batteryfast.com.au/asus/a32-s5.htm Asus a32-s5 Batteryhttp://www.batteryfast.com.au/asus/a32-s6.htm Asus a32-s6 Batteryhttp://www.batteryfast.com.au/asus/a32-u6.htm Asus a32-u6 Batteryhttp://www.batteryfast.com.au/asus/a33-f3.htm Asus a33-f3 Batteryhttp://www.batteryfast.com.au/asus/a33-s6.htm Asus a33-s6 Batteryhttp://www.batteryfast.com.au/asus/a4000.htm Asus a4000 Batteryhttp://www.batteryfast.com.au/asus/a42-a3.htm Asus a42-a3 Batteryhttp://www.batteryfast.com.au/gateway/m325.htm Gateway m325 Batteryhttp://www.batteryfast.com.au/hp/dv8000.htm Hp dv8000 Batteryhttp://www.batteryfast.com.au/hp/dv8100.htm Hp dv8100 Batteryhttp://www.batteryfast.com.au/hp/dv8200.htm Hp dv8200 Batteryhttp://www.batteryfast.com.au/hp/elitebook-8530w.htm Hp elitebook 8530w Batteryhttp://www.batteryfast.com.au/hp/f1739a.htm Hp f1739a Batteryhttp://www.batteryfast.com.au/hp/f2019a.htm Hp f2019a Batteryhttp://www.batteryfast.com.au/hp/f3172a.htm Hp f3172a Batteryhttp://www.batteryfast.com.au/hp/f4486a.htm Hp f4486a Batteryhttp://www.batteryfast.com.au/hp/hstnn-c29c.htm Hp hstnn-c29c Batteryhttp://www.batteryfast.com.au/hp/hstnn-db02.htm Hp hstnn-db02 Batteryhttp://www.batteryfast.com.au/hp/hstnn-db03.htm Hp hstnn-db03 Batteryhttp://www.batteryfast.com.au/hp/hstnn-db11.htm Hp hstnn-db11 Batteryhttp://www.batteryfast.com.au/hp/hstnn-db17.htm Hp hstnn-db17 Batteryhttp://www.batteryfast.com.au/hp/HSTNN-DB20.htm Hp HSTNN-DB20 Batteryhttp://www.batteryfast.com.au/hp/hstnn-db36.htm Hp hstnn-db36 Batteryhttp://www.batteryfast.com.au/hp/hstnn-db42.htm Hp hstnn-db42 Batteryhttp://www.batteryfast.com.au/hp/hstnn-db63.htm Hp hstnn-db63 Batteryhttp://www.batteryfast.com.au/hp/hstnn-db64.htm Hp hstnn-db64 Batteryhttp://www.batteryfast.com.au/hp/hstnn-fb39.htm Hp hstnn-fb39 Batteryhttp://www.batteryfast.com.au/hp/hstnn-i57c.htm Hp hstnn-i57c Batteryhttp://www.batteryfast.com.au/hp/hstnn-ib04.htm Hp hstnn-ib04 Batteryhttp://www.batteryfast.com.au/hp/hstnn-ib33.htm Hp hstnn-ib33 Batteryhttp://www.batteryfast.com.au/hp/hstnn-ib44.htm Hp hstnn-ib44 Batteryhttp://www.batteryfast.com.au/hp/hstnn-ib45.htm Hp hstnn-ib45 Batteryhttp://www.batteryfast.com.au/hp/hstnn-ob06.htm Hp hstnn-ob06 Batteryhttp://www.batteryfast.com.au/hp/hstnn-ob80.htm Hp hstnn-ob80 Batteryhttp://www.batteryfast.com.au/hp/hstnn-ub02.htm Hp hstnn-ub02 Batteryhttp://www.batteryfast.com.au/hp/hstnn-ub68.htm Hp hstnn-ub68 Batteryhttp://www.batteryfast.com.au/hp/m2000.htm Hp m2000 Batteryhttp://www.batteryfast.com.au/hp/mini-1000.htm Hp mini 1000 Batteryhttp://www.batteryfast.com.au/hp/mini-1001.htm Hp mini 1001 Batteryhttp://www.batteryfast.com.au/hp/mini-1050.htm Hp mini 1050 Batteryhttp://www.batteryfast.com.au/hp/n3402.htm Hp n3402 Batteryhttp://www.batteryfast.com.au/hp/N6400.htm Hp N6400 Batteryhttp://www.batteryfast.com.au/hp/nc2400.htm Hp nc2400 Batteryhttp://www.batteryfast.com.au/hp/nc4000.htm Hp nc4000 Battery

  3. Unknown says:

    <a href="http://www.rmtvip.jp ">アイオン-aion RMT</a> <a href="http://www.rmtvip.jp ">アトランティカ RMT</a><a href="http://www.rmtvip.jp ">リネージュ2 RMT</a><a href="http://www.rmtvip.jp ">アラド戦記 RMT</a><a href="http://www.rmtvip.jp ">FF11 RMT</a> <a href="http://www.rmtvip.jp ">信長の野望 RMT</a><a href="http://www.rmtvip.jp ">SUN RMT</a><a href="http://www.rmtvip.jp ">完美世界 RMT</a><a href="http://www.rmtvip.jp ">新天上碑 RMT</a> <a href="http://www.rmtvip.jp ">エイカ(aika) RMT </a> <a href="http://www.rmtvip.jp ">Atlantica-アトランティカ RMT</a> <a href="http://www.rmtvip.jp ">TW テイルズウィーバー RMT</a> <a href="http://www.rmtvip.jp ">lh_ルーセントハート RMT</a> <a href="http://www.jewelrytopbrand.com/ ">Tiffany jewelry</a> <a href="http://www.rmtvip.jp ">ルシエルブルー(Le Ciel Bleu) RMT</a> <a href="http://www.rmtvip.jp ">ff14 RMT</a> <a href="http://www.rmtvip.jp ">アイオン(aion) RMT </a>———~~———~~————~~—————~~————~~——<a href="http://www.rmt-wm.com/games/tenjohi.html ">新天上碑 RMT </a> <a href="http://www.rmt-wm.com/games/AIKA.html ">エイカ(aika) RMT </a><a href="http://www.rmt-wm.com/games/0010.html ">TW テイルズウィーバー RMT </a><a href="http://www.rmt-wm.com/games/A0006.html ">lh_ルーセントハート RMT </a><a href="http://www.rmt-wm.com/games/aion.html ">アイオン(aion) RMT</a><a href="http://www.rmt-wm.com/games/0003.html ">リネージュ2 RMT</a><a href="http://www.rmt-wm.com/games/A004.html ">アラド戦記 RMT</a><a href="http://www.rmt-wm.com/games/0005.html ">FF11(FFXI) RMT</a><a href="http://www.rmt-wm.com/games/A001.html ">信長の野望 RMT</a><a href="http://www.rmt-wm.com/games/0006.html ">SUN RMT</a><a href="http://www.rmt-wm.com/games/0018.html ">完美世界-Perfect World</a><a href="http://www.replica-watches-mall.com ">Replica Watche</a> <a href="http://www.rmt-wm.com/games/dekaron.html ">デカロン RMT </a><a href="http://www.rmt-wm.com/games/%E5%A5%87%E8%B9%9F%E3%81%AE%E5%A4%A7%E5%9C%B0_RMT.html ">奇跡の大地 RMT</a><a href="http://www.rmt-wm.com/games/Atlantica.html ">Atlantica-アトランティカ RMT</a><a href="http://www.rmt-wm.com/games/Le_Ciel_Bleu.html ">ル シエル ブルー(Le Ciel Bleu) RMT </a> <a href="http://www.rmt-wm.com/games/FF14.html ">ff14 RMT</a> <a href="http://www.rmt-wm.com/games/ラテール_RMT.html ">ラテール RMT </a>

Comments are closed.