using System.Security.Cryptography;
using System.Text;
namespace NHibernateProvider.Util
{
///
/// Helper class to generate a machine key for use in the application's config file to override the default key used by the provider.
///
internal static class KeyCreator
{
#region Main for Testing
//public static void main(string[] args)
//{
// string decryptionKey = CreateKey(Convert.ToInt32(args[1]));
// string validationKey = CreateKey(Convert.ToInt32(args[2]));
// Console.WriteLine(" ", validationKey, decryptionKey);
//}
#endregion Main for Testing
#region Operations
///
/// Creates a key based on the indicated size.
///
/// size of the key.
/// Generated key of the specified length.
public static string CreateKey(int numBytes)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buff = new byte[numBytes];
rng.GetBytes(buff);
return BytesToHexString(buff);
}
///
/// Converts the given byte array to a hex string representation.
///
/// the data to convert.
/// Hexadecimal string representation of the given byte array./
public static string BytesToHexString(byte[] bytes)
{
StringBuilder hexString = new StringBuilder(64);
for (int counter = 0; counter < bytes.Length; counter++)
{
hexString.Append(string.Format("{0:X2}", bytes[counter]));
}
return hexString.ToString();
}
#endregion Operations
}
}