Posts Tagged ‘ convert ’

How to hash a string (Password)

If you want to hash a string (password or whatever) you can use the following hashing methods.
Generally a Hash has the advantage that there is no way back, which means if you hash a password you can not find out the password with the hash.
The only way to do that is bruteforcing which could take years (depending on the computer and on the strength of the password).

Note that the MD5 algorithm is not as save as the SHA algorithms

public static string md5encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}

public static string sha1encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
SHA1CryptoServiceProvider sha1hasher = new SHA1CryptoServiceProvider();
byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}

public static string sha256encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
SHA256Managed sha256hasher = new SHA256Managed();
byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}

public static string sha384encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
SHA384Managed sha384hasher = new SHA384Managed();
byte[] hashedDataBytes = sha384hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}

public static string sha512encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
SHA512Managed sha512hasher = new SHA512Managed();
byte[] hashedDataBytes = sha512hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}

public static string byteArrayToString(byte[] inputArray)
{
StringBuilder output = new StringBuilder(“”);
for (int i = 0; i < inputArray.Length; i++) { output.Append(inputArray[i].ToString("X2")); } return output.ToString(); } [/sourcecode]

Write a DataTable to CSV and read a DataTable from CSV

If you want to write a DataTable into a .csv file or fill a DataTable with a .csv file you can use the following method

public static string DataTableToCsv(DataTable table, char seperator)
{
StringBuilder data = new StringBuilder();

foreach (DataRow row in table.Rows)
{
foreach (object value in row.ItemArray)
{
data.Append(value.ToString() + seperator);
}
data.AppendLine();
}

return data.ToString();
}

public static void DataTableToCsv(DataTable table, string path)
{
DataTableToCsv(table, path, ‘;’);
}

public static void DataTableToCsv(DataTable table, string path, char seperator)
{
using (StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.Default))
{
sw.Write(DataTableToCsv(table, seperator));
}
}

public static DataTable CsvToDataTable(string path, char seperator)
{
if (!File.Exists(path))
throw new FileNotFoundException();

DataTable table = new DataTable();

using (StreamReader reader = new StreamReader(path, Encoding.Default))
{
bool first = true;
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] cells = line.Split(seperator);

if (first)
{
first = false;
for (int i = 0; i < cells.Length; i++) { table.Columns.Add(); } } table.Rows.Add(); for (int i = 0; i < table.Columns.Count; i++) { table.Rows[table.Rows.Count - 1][i] = cells[i]; } } } return table; } public static DataTable CsvToDataTable(string path) { return CsvToDataTable(path, ';'); } [/sourcecode]

Convert an enum to a List

Sometimes you need a function to convert a enum to a string array.

There is already a class (Enum) which helps us to do so

string[] abc = Enum.GetNames(...);