Hashing with SHA1 Algorithm in C#

For those who want a “standard” text formatting of the hash, you can use something like the following: static string Hash(string input) { using (SHA1Managed sha1 = new SHA1Managed()) { var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input)); var sb = new StringBuilder(hash.Length * 2); foreach (byte b in hash) { // can be “x2” if you want lowercase … Read more

How to convert a byte to its binary string representation

Use Integer#toBinaryString(): byte b1 = (byte) 129; String s1 = String.format(“%8s”, Integer.toBinaryString(b1 & 0xFF)).replace(‘ ‘, ‘0’); System.out.println(s1); // 10000001 byte b2 = (byte) 2; String s2 = String.format(“%8s”, Integer.toBinaryString(b2 & 0xFF)).replace(‘ ‘, ‘0’); System.out.println(s2); // 00000010 DEMO.

What’s the difference between a word and byte?

Byte: Today, a byte is almost always 8 bit. However, that wasn’t always the case and there’s no “standard” or something that dictates this. Since 8 bits is a convenient number to work with it became the de facto standard. Word: The natural size with which a processor is handling data (the register size). The … Read more

Byte array to image conversion

You are writing to your memory stream twice, also you are not disposing the stream after use. You are also asking the image decoder to apply embedded color correction. Try this instead: using (var ms = new MemoryStream(byteArrayIn)) { return Image.FromStream(ms); }