static void Main(string[] args) { String Filename = @"D:\teul jung\visualStudioProjects\Sha256_Hash_File_IO\test.txt"; //read file FileStream fs = new FileStream(Filename, FileMode.Open, FileAccess.Read); //make filestream byte[] getHashValByte = new byte[32]; HHeader head = new HHeader(); HHeader.HashFile(fs, ref getHashValByte); for (int i = 0; i < 32; ++i) { Console.Write("{0:x}", getHashValByte[i]); } Console.WriteLine(); }
//private static ReadOnlyCollection<byte> HashFile(Stream fs) public static void HashFile(Stream fs, ref byte[] dest)//ref 예약어 사용한 것. { HHeader sha = new HHeader(); byte[] buf = new byte[8196]; uint bytes_read; do { bytes_read = (uint)fs.Read(buf, 0, buf.Length); if (bytes_read == 0) { break; } sha.AddData(buf, 0, bytes_read); //얘는 패딩하는애가 아니라 64바이트만큼 입력된것들을 우선 해쉬해놓는다. //정확히 말하면 pending_block에 원하는 데이터를 붙이면서 패딩조건을 업데이트하고 만약 64바이트가 되면 해쉬를 한다. }while (bytes_read > 0); //여기까지 64바이트씩 해쉬. 마지막 패딩과 나머지 값들은 아래 GetHash를 통해 마무리 한다. //sha클래스에 현재까지 해쉬한정보 그리고 pending_block에 나머지 해쉬할 것들이 들어있다. dest = (sha.GetHash()).ToArray <byte>(); //새로 할당한 메모리를 넣는건 call by value로 된다. }