Use the below code to find the SHA256 value of a file. When the user clicks the btn_Generate_Click button the SHA256 value will be generated and displayed in a RichTextBox.
C# Code:
private void btn_Generate_Click(object sender, EventArgs e) { hashOfFile("ENTER THE FILE PATH TO FIND SHA256"); }
public void hashOfFile(string fileToHash) { FileStream rdr = default(FileStream); SHA256CryptoServiceProvider sha = new SHA256CryptoServiceProvider(); byte[] bytes = null; string rtn = ""; if (File.Exists(fileToHash)) { rdr = new FileStream(fileToHash, FileMode.Open, FileAccess.Read); bytes = sha.ComputeHash(rdr); rtn = ByteArrayToString(bytes); } richTextBox1.Text=rtn; }
private string ByteArrayToString(byte[] arrInput) { System.Text.StringBuilder sb = new System.Text.StringBuilder(arrInput.Length * 2); for (int i = 0; i <= arrInput.Length - 1; i++) { sb.Append(arrInput[i].ToString("X2")); } return sb.ToString().ToLower(); }
No comments:
Post a Comment