forked from madewokherd/wine-mono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genfilehashes.cs
65 lines (56 loc) · 1.75 KB
/
genfilehashes.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
using Mono.Unix;
using System;
using System.IO;
using System.Security.Cryptography;
class GetFileHashes
{
static int ConvertInt32(byte[] buffer, int index) {
return(buffer[index] | (buffer[index+1] << 8) |
(buffer[index+2] << 16) | (buffer[index+3] << 24));
}
static string FormatMd5Hash(string path)
{
byte[] hashvalue;
using (MD5 hashalg = MD5.Create())
{
using (var stream = new FileStream(path, FileMode.Open))
{
hashvalue = hashalg.ComputeHash(stream);
}
}
return string.Format("{0}\t{1}\t{2}\t{3}",
ConvertInt32(hashvalue, 0),
ConvertInt32(hashvalue, 4),
ConvertInt32(hashvalue, 8),
ConvertInt32(hashvalue, 12));
}
static void ScanPath(UnixDirectoryInfo dirinfo, string prefix)
{
foreach (var fileinfo in dirinfo.GetFileSystemEntries())
{
string id = string.Concat(prefix, fileinfo.Name);
switch (fileinfo.FileType)
{
case FileTypes.RegularFile:
string hash;
if (fileinfo.Length == 0)
hash = "0\t0\t0\t0";
else
hash = FormatMd5Hash(fileinfo.FullName);
Console.WriteLine("{0}\t0\t{1}", id, hash);
break;
case FileTypes.Directory:
ScanPath((UnixDirectoryInfo)fileinfo, string.Concat(id, "\\"));
break;
default:
/* Do nothing for symlinks or other weird things. */
break;
}
}
}
static void Main()
{
var dirinfo = new UnixDirectoryInfo(".");
ScanPath(dirinfo, "");
}
}