Skip to content

Commit

Permalink
Add support for +4GB files
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeliux committed Aug 17, 2024
1 parent fd1f3f6 commit ad5063f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
20 changes: 16 additions & 4 deletions cli/Kryptor.Cli.Shared/Wordlist/CompileSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class CompileSession : Session
public WordlistIndexEntryV2 IndexEntry;
private readonly bool Indexing;
private readonly bool Importing;
private bool LowMemory;

public CompileSession(string path, string destination, WordlistIndexEntryV2 entry, bool indexing, bool importing)
{
Expand All @@ -42,6 +43,8 @@ public CompileSession(string path, string destination, WordlistIndexEntryV2 entr

Indexing = indexing;
Importing = importing;

LowMemory = false;
}

protected override async Task<bool> RunAsync(ISessionHost sessionHost, CancellationToken cancellationToken)
Expand All @@ -65,6 +68,10 @@ protected override async Task<bool> RunAsync(ISessionHost sessionHost, Cancellat
installer.FinalizeInstallation(IndexEntry);

Description = Indexing ? $"{IndexEntry.Id} Indexed" : $"{IndexEntry.Id} Installed";
if (LowMemory)
{
Description += " in low-memory mode";
}
}
catch
{
Expand All @@ -79,6 +86,11 @@ protected override async Task<bool> RunAsync(ISessionHost sessionHost, Cancellat
return true;
}

private bool TryAddLine(string line)
{
return LowMemory || uniqueLines.Add(line);
}

private void Cleanup(bool deleteInstallation)
{
if (cleaned) return;
Expand Down Expand Up @@ -107,17 +119,19 @@ private async Task Compile(CancellationToken cancellationToken)
{
double steps = 1.0 / streamReader.BaseStream.Length * 100;

int readChars = 0;
long readChars = 0;
string line;
while ((line = await streamReader.ReadLineAsync()) != null)
{
cancellationToken.ThrowIfCancellationRequested();
readChars += line.Length;
lines++;

Progress = readChars * steps;

line = line.Trim();

if (string.IsNullOrEmpty(line) || line.Length < 4 && uniqueLines.Add(line) && !regex.IsMatch(line)) continue;
if (string.IsNullOrEmpty(line) || line.Length < 4 || !TryAddLine(line) || regex.IsMatch(line)) continue;

byte[] data = Encoding.UTF8.GetBytes(line + "\n");
words++;
Expand All @@ -129,8 +143,6 @@ private async Task Compile(CancellationToken cancellationToken)
}

fileStreams[c].Write(data, 0, data.Length);

Progress = readChars * steps;
}
}

Expand Down
8 changes: 3 additions & 5 deletions cli/Kryptor.Cli.Shared/Wordlist/DownloadSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void SetEndStatus(object sender, System.ComponentModel.AsyncCompletedEve

Description = $"{IndexEntry.Id}: Verifying file";

VerifyHash(File.OpenRead(Downloader.Package.FileName), CancellationToken).Wait();
VerifyHash(File.OpenRead(Downloader.Package.FileName), CancellationToken);

if (IndexEntry.Compressed)
{
Expand Down Expand Up @@ -190,13 +190,11 @@ public void DeleteCache()
}
}

private async Task VerifyHash(Stream stream, CancellationToken cancellationToken)
private void VerifyHash(Stream stream, CancellationToken cancellationToken)
{
try
{
byte[] buffer = new byte[stream.Length];
await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
byte[] hash = buffer.Sha256();
byte[] hash = stream.Sha256();

if (IndexEntry.Hash == null)
{
Expand Down
5 changes: 1 addition & 4 deletions cli/Kryptor.Cli.Shared/Wordlist/ImportSessionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public override void Start(ClientContext context)
{
IndexEntry.Size = f.Length;

byte[] buffer = new byte[f.Length];
f.Read(buffer, 0, buffer.Length);

IndexEntry.Hash = buffer.Sha256();
IndexEntry.Hash = f.Sha256();
}

if (GetInstallationPermission(IndexEntry))
Expand Down
35 changes: 33 additions & 2 deletions src/Kryptor/Extensions/CryptoExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Cryptography;
using System.IO;
using System.Security.Cryptography;

namespace SAPTeam.Kryptor.Extensions
{
Expand All @@ -10,7 +11,7 @@ public static class CryptoExtensions
/// <summary>
/// Computes the SHA256 hash value for the specified byte array.
/// </summary>
/// <param name="buffer">The input to compute the hash code for.</param>
/// <param name="buffer">The input to compute the hash code for it.</param>
/// <returns>The computed hash array.</returns>
public static byte[] Sha256(this byte[] buffer)
{
Expand All @@ -20,6 +21,36 @@ public static byte[] Sha256(this byte[] buffer)
}
}

/// <summary>
/// Computes the SHA256 hash value for the specified stream.
/// </summary>
/// <param name="stream">The input to compute the hash code for it.</param>
/// <param name="startFromOrigin">If set to <see langword="true"/>, the stream will be moved to the beginning and at the end will be moved to the previous position.</param>
/// <returns>The computed hash array.</returns>
public static byte[] Sha256(this Stream stream, bool startFromOrigin = true)
{
var curPos = stream.Position;

if (startFromOrigin && stream.CanSeek)
{
stream.Seek(0, SeekOrigin.Begin);
}

byte[] hash;

using (SHA256 sha256 = SHA256.Create())
{
hash = sha256.ComputeHash(stream);
}

if (startFromOrigin && stream.CanSeek)
{
stream.Seek(curPos, SeekOrigin.Begin);
}

return hash;
}

/// <summary>
/// Computes the SHA384 hash value for the specified byte array.
/// </summary>
Expand Down

0 comments on commit ad5063f

Please sign in to comment.