Skip to content

Instantly share code, notes, and snippets.

@davidsylvestre
Last active January 13, 2020 02:50
Show Gist options
  • Select an option

  • Save davidsylvestre/88a1d0f21e45f0d41b5c4e7d7f4d5ff1 to your computer and use it in GitHub Desktop.

Select an option

Save davidsylvestre/88a1d0f21e45f0d41b5c4e7d7f4d5ff1 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace demo_read
{
class Program
{
static void Main(string[] args)
{
string pathSource = @"arquivo.txt";
SHA1 algorithm = new SHA1CryptoServiceProvider();
foreach (byte[] buffer in ReadLineByte(pathSource))
{
algorithm.TransformBlock(buffer, 0, buffer.Length, buffer, 0);
}
algorithm.TransformFinalBlock(new byte[0], 0, 0);
string hash = ConvertByteArrayToString(algorithm.Hash);
Console.WriteLine(hash);
}
public static IEnumerable<byte[]> ReadLineByte(string pathSource)
{
using (FileStream fsSource = new FileStream(pathSource, FileMode.Open, FileAccess.Read))
{
const byte LineBreak = 10;
int read;
List<byte> linha = new List<byte>();
byte[] buffer = new byte[1];
while ((read = fsSource.Read(buffer, 0, buffer.Length)) > 0)
{
linha.Add(buffer[0]);
if (buffer[0] == LineBreak)
{
yield return linha.ToArray();
linha.Clear();
}
}
yield return linha.ToArray();
}
}
private static string ConvertByteArrayToString(byte[] bytes)
{
var sb = new StringBuilder();
foreach (var b in bytes)
{
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment