-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashDirectory.cs
More file actions
69 lines (61 loc) · 2.23 KB
/
Copy pathHashDirectory.cs
File metadata and controls
69 lines (61 loc) · 2.23 KB
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
using System.Security.Cryptography;
using static WuHaoHash.FrmMain;
namespace WuHaoHash
{
public class HashDirectory
{
public static TheFile[]? Main(string directory)
{
int i = 0;
if (Directory.Exists(directory))
{
var dir = new DirectoryInfo(directory);
FileInfo[] files = dir.GetFiles();
TheFile[] theFile = new TheFile[files.Length];
using (SHA256 mySHA256 = SHA256.Create())
{
foreach (FileInfo fInfo in files)
{
using (FileStream fileStream = fInfo.Open(FileMode.Open))
{
try
{
fileStream.Position = 0;
byte[] hashValue = mySHA256.ComputeHash(fileStream);
theFile[i].Name = fInfo.Name;
theFile[i].HashValue = PrintByteArray(hashValue);
theFile[i].FilePath = directory + "\\" + fInfo.Name;
}
catch (IOException e)
{
Console.WriteLine($"I/O Exception: {e.Message}");
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine($"Access Exception: {e.Message}");
}
}
i++;
}
return theFile;
}
}
else
{
return null;
}
}
public static string PrintByteArray(byte[] array)
{
string? res = null;
for (int i = 0; i < array.Length; i++)
{
res += $"{array[i]:X2}";
//if ((i % 4) == 3)
// res += " ";
}
res += "\n";
return res;
}
}
}