using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Extentions
{
public static class ExtFileInfo
{
public static MD5 MD5Crypto;
public static String GetMD5Hash(this FileInfo f)
{
if (!File.Exists(f.FullName)) return "0";
if (MD5Crypto == null) MD5Crypto = MD5.Create();
Byte[] data = File.ReadAllBytes(f.FullName);
Byte[] bHash = MD5Crypto.ComputeHash(data);
StringBuilder sBuilder = new StringBuilder();
foreach (Byte b in bHash) sBuilder.AppendFormat("{0:x2}", b);
return sBuilder.ToString();
}
}
public static class ExtString
{
/// Encodes the string into Base64 and returns the result.
public static String EncodeBase64(this String s)
{
try
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(s));
}
catch (Exception)
{
return null;
}
}
/// Decodes the string from Base64 and returns the result.
public static String DecodeBase64(this String s)
{
try
{
return Encoding.Default.GetString(Convert.FromBase64String(s));
}
catch (Exception)
{
return null;
}
}
/// Encrypts the string and returns the result.
public static Byte[] Encrypt(this String s)
{
try
{
Byte[] buffer = new byte[s.Length];
for (int i = 0; i < s.Length; i++)
{
buffer[i] = (byte)(s[i] ^ (0x54 ^ (i & 0xFF)));
}
return buffer;
}
catch (Exception)
{
return null;
}
}
/// Decrypts the string and returns the result.
public static String Decrypt(this Byte[] b)
{
try
{
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < b.Length; i++)
{
sBuilder.Append((Char)(b[i] ^ (0x54 ^ (i & 0xFF))));
}
return sBuilder.ToString();
}
catch (Exception)
{
return null;
}
}
}
}