Files
wabbajack/Wabbajack.CLI/Verbs/HashFile.cs
2022-01-26 17:24:13 -07:00

42 lines
1.2 KiB
C#

using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Wabbajack.Hashing.xxHash64;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
namespace Wabbajack.CLI.Verbs;
public class HashFile : AVerb
{
private readonly ILogger<HashFile> _logger;
public HashFile(ILogger<HashFile> logger)
{
_logger = logger;
}
public static Command MakeCommand()
{
var command = new Command("hash-file");
command.Add(new Option<AbsolutePath>(new[] {"-i", "-input"}, "Path to the file to hash"));
command.Description = "Hashes a file with Wabbajack's xxHash64 implementation";
return command;
}
protected override ICommandHandler GetHandler()
{
return CommandHandler.Create(Run);
}
public async Task<int> Run(AbsolutePath input)
{
await using var istream = input.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
var hash = await istream.HashingCopy(Stream.Null, CancellationToken.None);
_logger.LogInformation("{Input} hash: {Hash} {HashAsHex} {HashAsLong}", input, hash, hash.ToHex(), (long)hash);
return 0;
}
}