C# - Sha1 hashing


SUBMITTED BY: Guest

DATE: Oct. 25, 2013, 2:27 p.m.

FORMAT: C#

SIZE: 2.3 kB

HITS: 1097

  1. Overview of Sha1
  2. Sha stands for secure hash algorithm and sha1 is basically the version (sha0-sha3) and was published 1993. It is the most common hash function.
  3. If you want to learn more about it just look it up.
  4. Other interesting types of encryption include Symmetric encryption which is where we use an IV and a key which we use to Encrypt and Decrypt text and Asymmetric encryption is where We have a different Key and IV to encrypt and decrypt but it is recommended to use GenerateKey() and generateIV() each session.
  5. With this tutorial we have a function called Sha1has and we call that with the text we want encrypted. We declare a new instance of the Sha1CryptoServiceProvider. We declare a byte and we set that to our string we're converting then we use our sha1 to compute the byte hash. Then we run a loop for every byte we find in our byte hash and we hash it :P then we output the encrypted text.
  6. public string Sha1hash(string StringToEncrypt)
  7. {
  8. System.Security.Cryptography.SHA1CryptoServiceProvider Sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
  9. byte[] b_ToHash = System.Text.Encoding.ASCII.GetBytes(StringToEncrypt);
  10. b_ToHash = Sha1.ComputeHash(b_ToHash);
  11. string HashedString = "";
  12. foreach (byte b in b_ToHash) {
  13. HashedString += b.ToString("x2");
  14. }
  15. txtOutput.Text = HashedString;
  16. return null;
  17. }
  18. private void btnEncrypt_Click(System.Object sender, System.EventArgs e)
  19. {
  20. Sha1hash(txtEncrypt.Text);
  21. }
  22. Above is the function we use (which I described). Sorry i didn't describe it very well it's a confusing subject.
  23. To call out function I have two textboxes one is for the text to be encrypted and one is for the output. The output is named txtOutput and the text to be encrypted is named txtEncrypt.
  24. We create a button called btnEncrypt and on the click event of the button we call the function and in brackets we give it the text we're encrypting. After we've clicked the button our hashed text will appear in the output.
  25. Sha1Hash(txtEncrypt.Text)
  26. I've zipped up the project if you want to download the code.
  27. Picture: http://cur.lv/4ckul
  28. I hope I've helped, thanks.

comments powered by Disqus