Hack Forums
[Tutorial] [Text Tutorial] SHA-512 Encryption Handling of User Credentials [Text Tutorial] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: [Tutorial] [Text Tutorial] SHA-512 Encryption Handling of User Credentials [Text Tutorial] (/showthread.php?tid=5806502)



[Text Tutorial] SHA-512 Encryption Handling of User Credentials [Text Tutorial] - KodaKing - Mar 2, 2018

The IDEcrashers Guide To Handling Salted Hash Algorithms for User Credentials in a Terminal Environment
Ω-:[KodaKing]:-Ω



The System.Security library maintains the System.Security.Cryptography nested class.
When hashing encryptions from user input, this can assist in more descrete authorization handling.


Code
using System;
using System.Console;
using System.Collections.Generic;
using System.Security;
using System.Security.Cryptography;
using System.Linq;
using System.IO;


When dealing with the Hash support it is necessary to determine the algorithm of choice.
I will be using SHA-512 enencryption.
For this lesson, I will stick to simply accessing an unprotected text file in /Documents for both the Username hash-table and the Password has-table.

Within a Command Line interface we should first prompt the user for desired activity.

Code
System.Console.WriteLine("Welcome!");
           System.Console.WriteLine("Are You A New User Or Returning?");
           string input1 = System.Console.ReadLine();

After we have set for the input, we can handle a new User Key-Pairing and output the pair to a text document.

First we need to recieve out UTF-8 Username input and Password input from our new User.
Code
               byte[] UsrData = Encoding.UTF8.GetBytes(Usr);
               byte[] shaHash;
               byte[] shaPwdHash;
For encryption of the SHA512 algorithm, we will create a new SHA512 instance and name the method shaWoah to be follow a new Managed function call.

Now convert the bytecode to a SHA512 hash.


         
Code
  SHA512 shaWoah = new SHA512Managed();
               shaHash = shaWoah.ComputeHash(UsrData);
       
 
Having completed the first encryption hash, we can implement the same process for our password.

Code
byte[] PwdData = Encoding.UTF8.GetBytes(Pwd);
               SHA512 shaYou = new SHA512Managed();
               shaPwdHash = shaYou.ComputeHash(PwdData);

After we have encrypted both the Username and Password, we may now transfer them to desired text file locations.

You can determine any path for your text file.

I will be converting my SHA 512 hash to a base64 string to provide a textual value.
             
               
Code
string InMy64 = Convert.ToBase64String(shaHash);
               System.Console.WriteLine(InMy64);
               string OnThat11 = Convert.ToBase64String(shaPwdHash);
               System.Console.WriteLine(OnThat11);
               using (StreamWriter outputUsr = new StreamWriter(@"C:\Users\Naye\Documents\Usrnames.txt", true))
               {
                   outputUsr.WriteLine(InMy64);
               }
               using (StreamWriter outputPass = new StreamWriter(@"C:\Users\Naye\Documents\Pwds.txt", true))
               {
                   outputPass.WriteLine(OnThat11);
               }

Now, we can simply say nice words to the User.

Code
System.Console.WriteLine("Your Information Has Been Written To The File System!");


For returning Users we will need to parse their input and authenticate.
               
             
Code
System.Console.WriteLine("Welcome! Please Input Your Username: ");
               string Usr = Console.ReadLine();
               System.Console.WriteLine("Thank You! Please Input Your Passkey Information: ");
               string Pwd = Console.ReadLine();

       Once again, to hash the encryption we must convert to bytecode from our UTF-8 input string.
 
               
Code
byte[] UsrData = Encoding.UTF8.GetBytes(Usr);
               byte[] shaHash;
               byte[] shaPwdHash;
               SHA512 shaWoah = new SHA512Managed();
               shaHash = shaWoah.ComputeHash(UsrData);
               byte[] PwdData = Encoding.UTF8.GetBytes(Pwd);
               SHA512 shaYou = new SHA512Managed();
               shaPwdHash = shaYou.ComputeHash(PwdData);
               string InMy64 = Convert.ToBase64String(shaHash);
               string OnThat11 = Convert.ToBase64String(shaPwdHash);
     

To check the index of our key-pair, we will implement the StreamReader method and load our text file of Hashes to it.
With a boolean operation we can check the relation of the indexes.

 

Code
int userindexglobal = 0;
               using (StreamReader usrread = new StreamReader(@"C:\Users\Naye\Documents\Usrnames.txt", true))

               {
                   string contents = usrread.ReadToEnd();
                   if (contents.Contains(InMy64))
                   {
                       System.Console.WriteLine("Thank You!");
                       goto UsrIndex;
                   }
                   else
              {
    Console.WriteLine("We're Sorry, Your Username Was Not Found To Be Valid.");
                       goto LogIn;
                   }

At this point, we will save the index location of the individual input credentials.
     
Code
UsrIndex:

                   userindexglobal = contents.IndexOf(InMy64);
                   goto Pass;
               }

               Pass:

               int passindexglobal = 0;
               bool usrAuth = true;
           using (StreamReader pwdread = new StreamReader(@"C:\Users\Naye\Documents\Pwds.txt", true))
               {
                string contents = pwdread.ReadToEnd();      
               if(contents.Contains(OnThat11))
                   {
                       System.Console.WriteLine("We Appreciate You Following These Security Formalities. Thank You, Once Again!");
                       goto PassIndex;
                   }
                   else
                   {
                       Console.WriteLine("We're Sorry Your Passkey Did Not Authorize.");
                       goto Start;
                   }

                   PassIndex:

                   passindexglobal = contents.IndexOf(OnThat11);
                   goto Auth;
               }
Now we get to our final Authentication.
By passing the boolean we can validate the key-pair.

               
Code
Auth:

               bool passAuth = true;
               bool doesit = usrAuth == passAuth;
               if (doesit == true && userindexglobal==passindexglobal)
               {
                   goto MainSubLoop;
               }
       
If we are unable to verify the Username and Password combination, we will need to alert the User and determine a new life-cycle path.
For such a task, I will display an error message to the console and relay back to the program's initialization.
 
Code
else

               {
                   Console.WriteLine("Authorization Did Not Occur. Looping Cycle.");
                   goto Start;
               }


With Authentication completed we get to deeper and more amusing things.