Previous Tip  |  Next Tip  |  Index (recent)   |  Design Tips   | [Bill's Home]

243. Hash signatures - Base-64 or Hex

The Hash signature, such as MD5, SHA-1, and so on, are the foundation of authentication, and verification. Unfortunately they come in two different formats: hexademical and Base-64. Base-64 is the type of encoding used in email, and converts binary streams into ASCII characters, while Hexademical format converts the bit stream into hexademical characters. For example, the word "hello" when hashed into Base-64 with MD5 results in "XUFAKrxLKna5cZ2REBfFkg== ", while in hex it is "5d41402abc4b2a76b9719d911017c592". The following shows a sample conversion for hex: [Click here to download the EXE][Click here to download of the solution]

and for Base-64:

The basic conversion for the hashing is:

 string hashText;
 XCryptEngine xe = new XCryptEngine(); 
   string inText = this.txtInput.Text;
 xe.InitializeEngine(XCryptEngine.AlgorithmType.MD5);
                   
   if (this.chBase64.Checked==true) hashText = xe.Encrypt(inText);
   else hashText = Base64ToHex(xe.Encrypt(inText));
   this.txtMD5.Text = hashText + " Length: "+hashText.Length;
 xe.InitializeEngine(XCryptEngine.AlgorithmType.SHA);
                   
   if (this.chBase64.Checked==true) hashText = xe.Encrypt(inText);
   else hashText = Base64ToHex(xe.Encrypt(inText));
   this.txtSHA.Text = hashText + " Length: "+hashText.Length;
 xe.InitializeEngine(XCryptEngine.AlgorithmType.SHA256);
   if (this.chBase64.Checked==true) hashText = xe.Encrypt(inText);
   else hashText = Base64ToHex(xe.Encrypt(inText));
   this.txtSHA256.Text = hashText + " Length: "+hashText.Length;
 xe.InitializeEngine(XCryptEngine.AlgorithmType.SHA512);
   if (this.chBase64.Checked==true) hashText = xe.Encrypt(inText);
   else hashText = Base64ToHex(xe.Encrypt(inText));
   this.txtSHA512.Text = hashText + " Length: "+hashText.Length;
               

and then we can create a method to convert from hex to Base-64:

 public static string Base64ToHex(string input)
 {
 StringBuilder sb = new StringBuilder();
   byte [] inputBytes = Convert.FromBase64String(input);
   foreach(byte b in inputBytes)
   {
    sb.Append(string.Format("{0:x2}", b));
   }
   return sb.ToString();
 }

If you are interested, Base-64 takes six bits at a time in the binary stream and converts the six bit value into an ASCII character. The conversion is:

Bit

Enc

Bit

Enc

Bit

Enc

Bit

Enc

0

A

16

Q

32

g

48

w

1

B

17

R

33

h

49

x

2

C

18

S

34

i

50

y

3

D

19

T

35

j

51

z

4

E

20

U

36

k

52

0

5

F

21

V

37

l

53

1

6

G

22

W

38

m

54

2

7

H

23

X

39

n

55

3

8

I

24

Y

40

o

56

4

9

J

25

Z

41

p

57

5

10

K

26

a

42

q

58

6

11

L

27

b

43

r

59

7

12

M

28

c

44

s

60

8

13

N

29

d

45

t

61

9

14

O

30

e

46

u

62

+

15

P

31

f

47

v

63

/

Thus if a binary file had the bit sequence:

10100010101010001010101010

It would first be split into groups of 6 bits, as follows:

101000 101010 100010 101010 000000

This would be converted into the ASCII sequence:

oqiqA

With hex conversion, the bit stream is split into four bits, at a time, and each of these is converted into a hex character. The conversion table is:

Val Hex
0 0
1 1
..
9 9
10 A
11 B
12 C
13 D
14 E
15 F

To convert:

10100010101010001010101010

It would first be split into groups of 4 bits, as follows:

1010 0010 1010 1000 1010 1010

This would be converted into the hex sequence:

A2A8AA

Other related .NET articles I've written include:

- Design Tip 298. [.NET] HMAC-SHA1.
- Design Tip 243. [.NET] Base-64 or Hex hash values.
- Design Tip 242. [.NET] Digital Certificates.
- Design Tip 241. [.NET] Public-key Encryption.
- Design Tip 240. [.NET] Diffie-Hellman Method.
- Design Tip 239. [.NET] Symmetric Encryption (Private-key).
- Design Tip 238. [.NET] Obfuscation Part II.
- Design Tip 237. [.NET] Obfuscation Part I
- Design Tip 236. [.NET] Data packet capture (filters: IP, TCP, and so on).
- Design Tip 235. [.NET] Data packet capture.
- Design Tip 234. [.NET] Interface to network adapter.
- Design Tip 232. [.NET] Creating an SSH client.
- Design Tip 231. [.NET] Creating an SNMP client.
- Design Tip 216. [.NET] Client/server communications.
- Design Tip 210. [XML/.NET] XML and .NET.
- Design Tip 207. [.NET] Treeviews for interest.
- Design Tip 206. [.NET/Design] Design, evaluate, design, .....
- Design Tip 205. [.NET] Treeviews.
- Design Tip 203. [.NET] Replacing menus with Treeviews.
- Design Tip 202. [.NET/Flash] .NET and Flash - the perfect pair.