Skip to content

Commit

Permalink
Normalise line endings to LF before hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Sep 15, 2019
1 parent 85c11d9 commit 27c3fb6
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,33 @@ select content.Open())
.ToStreamable();

string hash;
using (var sha = SHA1.Create())
using (var sha = IncrementalHash.CreateHash(HashAlgorithmName.SHA1))
using (var stream = hashSource.Open())
{
hash = BitConverter.ToString(sha.ComputeHash(stream))
for (var buffer = new byte[4096]; ;)
{
var length = stream.Read(buffer, 0, buffer.Length);
if (length == 0)
break;

// Normalize line endings by removing CR, assuming only LF remain

var span = buffer.AsSpan(0, length);

while (span.Length > 0)
{
var si = span.IndexOf((byte)'\r');
if (si < 0)
{
sha.AppendData(span);
break;
}
sha.AppendData(span.Slice(0, si));
span = span.Slice(si + 1);
}
}

hash = BitConverter.ToString(sha.GetHashAndReset())
.Replace("-", string.Empty)
.ToLowerInvariant();
}
Expand Down

0 comments on commit 27c3fb6

Please sign in to comment.