/// <summary> /// パスワードファイルとして、ファイルからSHA-256ハッシュを取得してバイト列にする /// Get a string of the SHA-256 hash from a file such as the password file /// </summary> /// <param name="FilePath"></param> /// <returns></returns> private byte[] GetPasswordFileHash3(string FilePath) { byte[] result = new byte[32]; using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read)) { ReadOnlyCollection <byte> hash = Sha256.HashFile(fs); for (int i = 0; i < 32; i++) { result[i] = hash[i]; } return(result); } /* * byte[] buffer = new byte[255]; * byte[] result = new byte[32]; * * using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read)) * { * //SHA1CryptoServiceProviderオブジェクト * using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider()) * { * byte[] array_bytes = sha256.ComputeHash(fs); * for (int i = 0; i < 32; i++) * { * result[i] = array_bytes[i]; * } * } * } * //string text = System.Text.Encoding.ASCII.GetString(result); * return (result); */ }
/// <summary> /// 計算してチェックサム(SHA-256)を得る /// Get a check sum (SHA-256) to calculate /// </summary> /// <param name="dataToCalculate"></param> /// <returns></returns> public static string GetSha256FromFile(string FilePath) { using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read)) { ReadOnlyCollection <byte> hash = Sha256.HashFile(fs); StringBuilder result = new StringBuilder(); result.Capacity = 32; foreach (byte b in hash) { result.Append(b.ToString()); } return(result.ToString()); } }
private async void button1_Click(object sender, EventArgs e) { hashTextbox.Text = ""; DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) { //Wyłączenie przycisków button1.Enabled = false; hashStringTexBox.Enabled = false; hashing.Visible = true; fileNameLabel.Text = openFileDialog1.FileName; progressBar1.Maximum = 1000; // Aby zrobić progress bar płynny FileStream fs = await Task.Run(() => File.OpenRead(openFileDialog1.FileName)); hashResult = await Task <string> .Run(() => Sha256.ByteArrayToString(Sha256.HashFile(fs, (pr) => { if (this.InvokeRequired) { this.BeginInvoke(new Action(() => { progressBar1.Value = (int)pr; })); //Synchronizacja wątku } }))); hashTextbox.Text = hashResult ?? null; hashTextbox.SelectionStart = hashTextbox.Text.Length; //Włączenie przycików button1.Enabled = true; hashStringTexBox.Enabled = true; hashing.Visible = false; } }
static void Main(string[] args) { ReadOnlyCollection <byte> hash = Sha256.HashFile(File.OpenRead(@"foo.bin")); System.Console.Out.WriteLine("{0}", Util.ArrayToString(hash)); }
static void Main(string[] args) { string s; FileStream fs = new FileStream(@"C:\Users\aayum\Source\Repos\SHA2-Csharp\creation.txt", FileMode.Open, FileAccess.Read); ReadOnlyCollection <byte> hash = Sha256.HashFile(fs); long len = fs.Length; System.Console.WriteLine("length : {0}", fs.Length); fs.Close(); byte[] buff = new byte[len]; using (BinaryReader reader = new BinaryReader(new FileStream(@"C:\Users\aayum\Source\Repos\SHA2-Csharp\creation.txt", FileMode.Open))) { reader.BaseStream.Seek(0, SeekOrigin.Begin); reader.Read(buff, 0, (int)len); } foreach (byte t in buff) { System.Console.WriteLine(t); } System.Console.Out.WriteLine("{0}", Util.ArrayToString(hash)); byte[] initialDump = File.ReadAllBytes(@"C:\Users\aayum\Source\Repos\SHA2-Csharp\creation.txt"); foreach (byte m in initialDump) { System.Console.WriteLine(m); } System.Console.WriteLine("Initial dump ends here!!!"); // System.Console.WriteLine(BitConverter.ToString(send)); //byte buffer = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; var buffer = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; // AppendAllBytes(@"C:\Users\aayum\Source\Repos\SHA2-Csharp\test.txt",buffer); // using(StreamWriter sw = File.AppendText(@"C:\Users\aayum\Source\Repos\SHA2-Csharp\test.txt")) // { // sw.WriteLine("extra boyyyy!!!"); //} //getInitialPadding n = new Sha2.getInitialPadding(); int[] l = new int[2]; l[0] = (int)len / 100; l[1] = (int)len % 100; buffer[63] = (byte)l[1]; buffer[62] = (byte)l[0]; byte[] x = new byte[512]; x = getInitialPadding.padding; Array.Resize(ref x, getInitialPadding.padding.Length); byte[] final = new byte[1024]; x.CopyTo(final, 0); buffer.CopyTo(final, x.Length); int i = x.Length + buffer.Length; Array.Resize(ref final, x.Length + buffer.Length); System.Console.WriteLine(BitConverter.ToString(x)); System.Console.WriteLine(BitConverter.ToString(final)); byte[] y = new byte[] { 0x1 }; AppendAllBytes(@"C:\Users\aayum\Source\Repos\SHA2-Csharp\creation.txt", y); AppendAllBytes(@"C:\Users\aayum\Source\Repos\SHA2-Csharp\creation.txt", final); System.Console.WriteLine("Appending done"); byte[] dump = File.ReadAllBytes(@"C:\Users\aayum\Source\Repos\SHA2-Csharp\creation.txt"); foreach (byte c in dump) { System.Console.WriteLine(c); } //System.Console.WriteLine(BitConverter.ToString(x)); //FileStream fs2 = new FileStream(@"C:\Users\aayum\Source\Repos\SHA2-Csharp\test.txt", FileMode.Open, FileAccess.Read); //ReadOnlyCollection<byte> hash2 = Sha256.HashFile(fs); //fs.Close(); //System.Console.Out.WriteLine("{0}", Util.ArrayToString(hash2)); //byte[] x2 = new byte[512]; //x2 = getInitialPadding.padding; //Array.Resize(ref x, getInitialPadding.padding.Length); //System.Console.WriteLine(BitConverter.ToString(x2)); System.Console.Read(); }