EaseFilter Demo Project
Utils.cs
Go to the documentation of this file.
1 //
3 // (C) Copyright 2012 EaseFilter Technologies Inc.
4 // All Rights Reserved
5 //
6 // This software is part of a licensed software product and may
7 // only be used or copied in accordance with the terms of that license.
8 //
10 
11 using System;
12 using System.Collections.Generic;
13 using System.Linq;
14 using System.Text;
15 using System.Globalization;
16 using System.Security.Cryptography;
17 using System.Windows.Forms;
18 using System.Reflection;
19 using System.IO;
20 using System.Text.RegularExpressions;
21 using System.Configuration;
22 
23 namespace EaseFilter.CommonObjects
24 {
25 
26  public class Utils
27  {
28  public static uint WinMajorVersion()
29  {
30  dynamic major;
31  // The 'CurrentMajorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
32  // and will most likely (hopefully) be there for some time before MS decides to change this - again...
33  if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out major))
34  {
35  return (uint)major;
36  }
37 
38  // When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
39  dynamic version;
40  if (!TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
41  return 0;
42 
43  var versionParts = ((string)version).Split('.');
44  if (versionParts.Length != 2) return 0;
45 
46  uint majorAsUInt;
47  return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0;
48  }
49 
53  public static uint WinMinorVersion()
54  {
55  dynamic minor;
56  // The 'CurrentMinorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
57  // and will most likely (hopefully) be there for some time before MS decides to change this - again...
58  if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber",
59  out minor))
60  {
61  return (uint)minor;
62  }
63 
64  // When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
65  dynamic version;
66  if (!TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
67  return 0;
68 
69  var versionParts = ((string)version).Split('.');
70  if (versionParts.Length != 2) return 0;
71  uint minorAsUInt;
72  return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0;
73  }
74 
75 
76  private static bool TryGeRegistryKey(string path, string key, out dynamic value)
77  {
78  value = null;
79  try
80  {
81  var rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path);
82  if (rk == null) return false;
83  value = rk.GetValue(key);
84  return value != null;
85  }
86  catch
87  {
88  return false;
89  }
90  }
91 
92  public static void CopyOSPlatformDependentFiles()
93  {
94  Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();
95  string localPath = Path.GetDirectoryName(assembly.Location);
96  string targetName = Path.Combine(localPath, "FilterAPI.DLL");
97 
98  bool is64BitOperatingSystem = Environment.Is64BitOperatingSystem;
99  uint winMajorVersion = WinMajorVersion();
100 
101  string sourceFolder = localPath;
102 
103  try
104  {
105 
106  if (is64BitOperatingSystem)
107  {
108  if (winMajorVersion >= 10)
109  {
110  sourceFolder = Path.Combine(localPath, "Bin\\Win10X64");
111  }
112  else
113  {
114  sourceFolder = Path.Combine(localPath, "Bin\\x64");
115  }
116  }
117  else
118  {
119  sourceFolder = Path.Combine(localPath, "Bin\\win32");
120  }
121 
122  string sourceFile = Path.Combine(sourceFolder, "FilterAPI.DLL");
123 
124  //only copy files for x86 platform, by default for x64, the files were there already.
125 
126  bool skipCopy = false;
127  if (File.Exists(targetName))
128  {
129  FileInfo sourceFileInfo = new FileInfo(sourceFile);
130  FileInfo targetFileInfo = new FileInfo(targetName);
131 
132  if (sourceFileInfo.LastWriteTime.ToFileTime() == targetFileInfo.LastWriteTime.ToFileTime())
133  {
134  skipCopy = true;
135  }
136  }
137 
138  if (!skipCopy)
139  {
140  File.Copy(sourceFile, targetName, true);
141  }
142 
143 
144  sourceFile = Path.Combine(sourceFolder, "EaseFlt.sys");
145  targetName = Path.Combine(localPath, "EaseFlt.sys");
146 
147 
148  skipCopy = false;
149  if (File.Exists(targetName))
150  {
151  FileInfo sourceFileInfo = new FileInfo(sourceFile);
152  FileInfo targetFileInfo = new FileInfo(targetName);
153 
154  if (sourceFileInfo.LastWriteTime.ToFileTime() == targetFileInfo.LastWriteTime.ToFileTime())
155  {
156  skipCopy = true;
157  }
158  }
159 
160  if (!skipCopy)
161  {
162  File.Copy(sourceFile, targetName, true);
163  }
164 
165  }
166  catch (Exception ex)
167  {
168  string lastError = "Copy platform dependent files 'FilterAPI.DLL' and 'EaseFlt.sys' to folder " + localPath + " got exception:" + ex.Message;
169  EventManager.WriteMessage(80, "CopyOSPlatformDependentFiles", EventLevel.Error, lastError);
170  }
171  }
172 
173 
174  public static string ByteArrayToHexStr(byte[] ba)
175  {
176  StringBuilder hex = new StringBuilder(ba.Length * 2);
177  foreach (byte b in ba)
178  {
179  hex.AppendFormat("{0:x2}", b);
180  }
181 
182  return hex.ToString().ToUpper();
183  }
184 
185  public static byte[] ConvertHexStrToByteArray(string hexString)
186  {
187  if (hexString.Length % 2 != 0)
188  {
189  throw new ArgumentException(String.Format("The binary key cannot have an odd number of digits: {0}", hexString));
190  }
191 
192  byte[] HexAsBytes = new byte[hexString.Length / 2];
193  for (int index = 0; index < HexAsBytes.Length; index++)
194  {
195  string byteValue = hexString.Substring(index * 2, 2);
196  HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
197  }
198 
199  return HexAsBytes;
200  }
201 
207  public static byte[] GetKeyByPassPhrase(string pwStr)
208  {
209  byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
210  byte[] passwordBytes = Encoding.UTF8.GetBytes(pwStr);
211 
212  var rfckey = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
213  byte[] key = rfckey.GetBytes(32);
214 
215  return key;
216 
217 
218  }
219 
220  public static byte[] GetRandomKey()
221  {
222  AesManaged aesManaged = new AesManaged();
223  aesManaged.KeySize = 256;
224  byte[] key = aesManaged.Key;
225 
226  return key;
227  }
228 
229  public static byte[] GetRandomIV()
230  {
231  AesManaged aesManaged = new AesManaged();
232  byte[] IV = aesManaged.IV;
233 
234  return IV;
235  }
236 
237  public static bool IsBase64String(string s)
238  {
239  s = s.Trim();
240 
241  if ((s.Length % 4 == 0) && Regex.IsMatch(s, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None))
242  {
243  return true;
244  }
245  else
246  {
247  return false;
248  }
249 
250  }
251 
252  public static void SaveAppSetting(string fileName, Dictionary<string, string> keyValues)
253  {
254  ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
255  configFileMap.ExeConfigFilename = fileName;
256  Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
257  KeyValueConfigurationCollection settings = configuration.AppSettings.Settings;
258 
259  settings.Clear();
260 
261  foreach (KeyValuePair<string, string> item in keyValues)
262  {
263  settings.Add(item.Key, item.Value);
264  }
265 
266  configuration.Save();
267  }
268 
269  public static void LoadAppSetting(string fileName, ref Dictionary<string, string> keyValues)
270  {
271  ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
272  configFileMap.ExeConfigFilename = fileName;
273  Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
274  KeyValueConfigurationCollection settings = configuration.AppSettings.Settings;
275 
276  foreach (KeyValueConfigurationElement item in settings)
277  {
278  keyValues.Add(item.Key, item.Value);
279  }
280 
281  }
282 
283 
284  public static void ToDebugger(string message)
285  {
286  System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(false);
287  string caller = st.GetFrame(1).GetMethod().Name;
288  System.Diagnostics.Debug.WriteLine(caller + " Time:" + DateTime.Now.ToLongTimeString() + ": " + message);
289  }
290 
291 
292  }
293 }
static void SaveAppSetting(string fileName, Dictionary< string, string > keyValues)
Definition: Utils.cs:252
static byte [] GetRandomIV()
Definition: Utils.cs:229
unsigned char key[]
static uint WinMinorVersion()
Returns the Windows minor version number for this computer.
Definition: Utils.cs:53
static string ByteArrayToHexStr(byte[] ba)
Definition: Utils.cs:174
static void LoadAppSetting(string fileName, ref Dictionary< string, string > keyValues)
Definition: Utils.cs:269
static bool IsBase64String(string s)
Definition: Utils.cs:237
static byte [] ConvertHexStrToByteArray(string hexString)
Definition: Utils.cs:185
static void CopyOSPlatformDependentFiles()
Definition: Utils.cs:92
static byte [] GetKeyByPassPhrase(string pwStr)
Generate 32 bytes key array by pass phrase string
Definition: Utils.cs:207
static uint WinMajorVersion()
Definition: Utils.cs:28
static void ToDebugger(string message)
Definition: Utils.cs:284
static byte [] GetRandomKey()
Definition: Utils.cs:220

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com