Revision 3906933e trunk/Pithos.Network/Signature.cs
b/trunk/Pithos.Network/Signature.cs | ||
---|---|---|
61 | 61 |
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
62 | 62 |
public const int BufferSize = 16384; |
63 | 63 |
|
64 |
public const string MD5_EMPTY = "d41d8cd98f00b204e9800998ecf8427e"; |
|
64 |
// public const string MD5_EMPTY = "d41d8cd98f00b204e9800998ecf8427e";
|
|
65 | 65 |
public const string MERKLE_EMPTY = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; |
66 | 66 |
|
67 | 67 |
|
68 |
public static string CalculateMD5(FileSystemInfo info) |
|
69 |
{ |
|
70 |
if (info==null) |
|
71 |
throw new ArgumentNullException("info"); |
|
72 |
if (String.IsNullOrWhiteSpace(info.FullName)) |
|
73 |
throw new ArgumentException("info.FullName is empty","info"); |
|
74 |
Contract.EndContractBlock(); |
|
75 |
|
|
76 |
if (info is DirectoryInfo) |
|
77 |
return MD5_EMPTY; |
|
78 |
|
|
79 |
return CalculateMD5(info.FullName); |
|
80 |
} |
|
81 |
|
|
82 |
public static string CalculateMD5(string path) |
|
83 |
{ |
|
84 |
if (String.IsNullOrWhiteSpace(path)) |
|
85 |
throw new ArgumentNullException("path"); |
|
86 |
Contract.EndContractBlock(); |
|
87 |
|
|
88 |
//DON'T calculate hashes for folders |
|
89 |
if (Directory.Exists(path)) |
|
90 |
return ""; |
|
91 |
//TODO: Replace with MD5BlockCalculator |
|
92 |
string hash; |
|
93 |
using (var hasher = new MessageDigestContext(MessageDigest.CreateByName("md5"))) |
|
94 |
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize, true)) |
|
95 |
{ |
|
96 |
var buffer = new byte[BufferSize]; |
|
97 |
hasher.Init(); |
|
98 |
int read; |
|
99 |
do |
|
100 |
{ |
|
101 |
read = stream.Read(buffer, 0, buffer.Length); |
|
102 |
if (read==buffer.Length) |
|
103 |
{ |
|
104 |
hasher.Update(buffer); |
|
105 |
} |
|
106 |
else |
|
107 |
{ |
|
108 |
var block = new byte[read]; |
|
109 |
Buffer.BlockCopy(buffer, 0, block, 0, read); |
|
110 |
hasher.Update(block); |
|
111 |
} |
|
112 |
} while (read>0); |
|
113 |
var hashBytes = hasher.DigestFinal(); |
|
114 |
hash = hashBytes.ToHashString(); |
|
115 |
} |
|
116 |
return hash; |
|
117 |
} |
|
68 |
//public static string CalculateMD5(FileSystemInfo info)
|
|
69 |
//{
|
|
70 |
// if (info==null)
|
|
71 |
// throw new ArgumentNullException("info");
|
|
72 |
// if (String.IsNullOrWhiteSpace(info.FullName))
|
|
73 |
// throw new ArgumentException("info.FullName is empty","info");
|
|
74 |
// Contract.EndContractBlock();
|
|
75 |
|
|
76 |
// if (info is DirectoryInfo)
|
|
77 |
// return MD5_EMPTY;
|
|
78 |
|
|
79 |
// return CalculateMD5(info.FullName);
|
|
80 |
//}
|
|
81 |
|
|
82 |
//public static string CalculateMD5(string path)
|
|
83 |
//{
|
|
84 |
// if (String.IsNullOrWhiteSpace(path))
|
|
85 |
// throw new ArgumentNullException("path");
|
|
86 |
// Contract.EndContractBlock();
|
|
87 |
|
|
88 |
// //DON'T calculate hashes for folders
|
|
89 |
// if (Directory.Exists(path))
|
|
90 |
// return "";
|
|
91 |
// //TODO: Replace with MD5BlockCalculator
|
|
92 |
// string hash;
|
|
93 |
// using (var hasher = new MessageDigestContext(MessageDigest.CreateByName("md5")))
|
|
94 |
// using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize, true))
|
|
95 |
// {
|
|
96 |
// var buffer = new byte[BufferSize];
|
|
97 |
// hasher.Init();
|
|
98 |
// int read;
|
|
99 |
// do
|
|
100 |
// {
|
|
101 |
// read = stream.Read(buffer, 0, buffer.Length);
|
|
102 |
// if (read==buffer.Length)
|
|
103 |
// {
|
|
104 |
// hasher.Update(buffer);
|
|
105 |
// }
|
|
106 |
// else
|
|
107 |
// {
|
|
108 |
// var block = new byte[read];
|
|
109 |
// Buffer.BlockCopy(buffer, 0, block, 0, read);
|
|
110 |
// hasher.Update(block);
|
|
111 |
// }
|
|
112 |
// } while (read>0);
|
|
113 |
// var hashBytes = hasher.DigestFinal();
|
|
114 |
// hash = hashBytes.ToHashString();
|
|
115 |
// }
|
|
116 |
// return hash;
|
|
117 |
//}
|
|
118 | 118 |
|
119 | 119 |
|
120 | 120 |
/* |
... | ... | |
233 | 233 |
//Calculate the hash of all blocks using a blockhash iterator |
234 | 234 |
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, BufferSize, true)) |
235 | 235 |
{ |
236 |
var md5 = new MD5BlockCalculator(); |
|
237 |
Action<long, byte[], int> postAction = md5.PostBlock; |
|
236 |
//var md5 = new MD5BlockCalculator();
|
|
237 |
//Action<long, byte[], int> postAction = md5.PostBlock;
|
|
238 | 238 |
//Calculate the blocks asyncrhonously |
239 |
var hashes = BlockHashAlgorithms.CalculateBlockHashesInPlacePFor(stream, blockSize, algorithm, parallelism,postAction,token, progress).Result; |
|
239 |
var hashes = BlockHashAlgorithms.CalculateBlockHashesInPlacePFor(stream, blockSize, algorithm, parallelism,token, progress).Result; |
|
240 |
//var hashes = BlockHashAlgorithms.CalculateBlockHashesInPlacePFor(stream, blockSize, algorithm, parallelism, postAction, token, progress).Result; |
|
240 | 241 |
|
241 | 242 |
//And then proceed with creating and returning a TreeHash |
242 | 243 |
var length = stream.Length; |
... | ... | |
249 | 250 |
Hashes = list, |
250 | 251 |
}; |
251 | 252 |
|
252 |
string fileHash; |
|
253 |
//string fileHash;
|
|
253 | 254 |
|
254 |
var md5Hash=md5.GetHash().Result; |
|
255 |
treeHash.MD5= md5Hash; |
|
255 |
//var md5Hash=md5.GetHash().Result;
|
|
256 |
//treeHash.MD5= md5Hash;
|
|
256 | 257 |
|
257 | 258 |
return treeHash; |
258 | 259 |
} |
Also available in: Unified diff