-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppContext.cs
94 lines (79 loc) · 2.81 KB
/
AppContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Diagnostics;
using System.IO.Hashing;
using System.Security.Cryptography;
using Humanizer.Bytes;
using Prism.Mvvm;
namespace F2Checker.Models;
public class AppContext : BindableBase
{
private readonly int _bufferSize = 1024 * 1024 * 10;
public AppContext()
{
Buffer = new byte[_bufferSize];
FirstFilePath = string.Empty;
Status = string.Empty;
}
/// <summary>
/// ハッシュ値計算の実行
/// </summary>
public async Task CheckSum(CancellationToken token)
{
// TODO: 仮実装で1つ目のファイルのみ計算。本来はパスの空チェックとかやる。
FirstFileHash = await GetHashAsync(FirstFilePath, token).ConfigureAwait(false);
}
private async Task<string> GetHashAsync(string filename, CancellationToken token)
{
if (token.IsCancellationRequested)
return string.Empty;
using (var entryStream = File.OpenRead(filename))
{
var xxhash = new XxHash128();
long totalBytesRead = 0;
var bytesRead = await entryStream.ReadAsync(Buffer, 0, Buffer.Length, token)
.ConfigureAwait(false);
var startTime = DateTime.Now;
var speed = new ByteSize();
var hash = SHA256.Create();
while (bytesRead > 0)
{
token.ThrowIfCancellationRequested();
//hash.TransformBlock(Buffer, 0, bytesRead, null, 0);
xxhash.Append(Buffer.AsSpan(0, bytesRead));
totalBytesRead += bytesRead;
speed = ByteSize.FromBytes(totalBytesRead / (DateTime.Now - startTime).TotalSeconds);
Status = $"{speed.LargestWholeNumberValue:#.##}{speed.LargestWholeNumberSymbol}/s";
bytesRead = await entryStream.ReadAsync(Buffer, 0, Buffer.Length, token)
.ConfigureAwait(false);
}
// hash.TransformFinalBlock(Buffer, 0, bytesRead);
// return Convert.ToHexString(hash.Hash).ToLower();
Array.Clear(Buffer, 0, Buffer.Length);
return Convert.ToHexString(xxhash.GetHashAndReset()).ToLower();
}
}
private byte[] Buffer { get; }
private string _firstFilePath;
/// <summary>
/// 解析対象のファイルパス
/// </summary>
public string FirstFilePath
{
get => _firstFilePath;
set => SetProperty(ref _firstFilePath, value);
}
private string _firstFileHash;
public string FirstFileHash
{
get => _firstFileHash;
set => SetProperty(ref _firstFileHash, value);
}
private string _status;
/// <summary>
/// ステータス
/// </summary>
public string Status
{
get => _status;
set => SetProperty(ref _status, value);
}
}