Author Archives: CooLMinE
How to hash a string with salt using a multi-algorithm method
It is considered a very bad idea to store user credentials in plain text, especially passwords. For that very reason it is always a good idea to hash passwords before you store them, ideally with a unique salt that you can store in another location. That way if for whatever reason your database is compromised your users passwords will not be in plain text and it will require a bit of work in order to find out what each password is, especially if the salt for each password is stored somewhere else.
The following method is a multi algorithm method, that means that with this single method you can use more than one algorithm to hash your data. The snippet below shows an example how to hash your data with a single method using seven different algorithms.
Our main method that will do all the work for us:
1 2 3 4 5 6 | private string StringToHash(string data, string salt, HashAlgorithm algorithm) { byte[] saltedBytes = Encoding.UTF8.GetBytes(data + salt); // Combine the data with the salt byte[] hashedBytes = algorithm.ComputeHash(saltedBytes); // Compute the hash value of our input return BitConverter.ToString(hashedBytes); } |
Host a process window inside your applications window
The following snippet will allow you to host the window of any application inside your own application. This isn’t a recommended practice but it’s a fun method that might spawn some interesting ideas.
In order to get this working we will need to pinvoke two Win32 functions, SetParent
and SetWindowPos
.
Place the following lines in your class
1 2 3 4 | [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll", EntryPoint = "SetWindowPos")] public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags); |
Shortening a URL using bitly’s API in C#
Using the snippet below you can convert links in your application from this
1 | http://www.fluxbytes.com/ |
to this
1 | http://bit.ly/WVk1qN |
This is especially important in cases where you have a fairly long URL which you want to post somewhere and you either don’t want to use such a long URL, or simply you are limited by characters. An example of that situation would be a URL such as
1 | http://www.google.com/url?sa=t&rct=j&q=c+sharp+convert+string+to+binary&source=web&cd=1&cad=rja&ved=0CDQQFjAA&url=http%3A%2F%2Fwww.fluxbytes.com%2Fcsharp%2Fconvert-string-to-binary-and-binary-to-string-in-c%2F&ei=T79SUcOVI4rEPOeogPgP&usg=AFQjCNEihqo_KsRZuGpb0-ZpWdqjdqr_sA |
which can be shortened down to
1 | http://bit.ly/WVuXF4 |
The first thing you are going to need it an account from bitly.com. After you have created an account, log in and navigate to https://bitly.com/a/your_api_key. The page will contain your username and your API key which are both needed.
Convert string to binary and binary to string in C#
The following two snippets allow you to convert a string to binary text and also to convert binary back to string.
String to binary method:
1 2 3 4 5 6 7 8 9 10 | public static string StringToBinary(string data) { StringBuilder sb = new StringBuilder(); foreach (char c in data.ToCharArray()) { sb.Append(Convert.ToString(c, 2).PadLeft(8, '0')); } return sb.ToString(); } |
Binary to string method:
1 2 3 4 5 6 7 8 9 10 | public static string BinaryToString(string data) { List<Byte> byteList = new List<Byte>(); for (int i = 0; i < data.Length; i += 8) { byteList.Add(Convert.ToByte(data.Substring(i, 8), 2)); } return Encoding.ASCII.GetString(byteList.ToArray()); } |
Usage:
1 2 3 4 5 | // returns "01100110011011000111010101111000011000100111100101110100011001010111001100101110011000110110111101101101" StringToBinary("fluxbytes.com") // returns "fluxbytes.com" BinaryToString("01100110011011000111010101111000011000100111100101110100011001010111001100101110011000110110111101101101"); |
Posted in C#.
Tagged binary, binary to string, C#, csharp, snippet, string to binary, winforms