Revision 174bbb6e trunk/Pithos.Core/Agents/BlockExtensions.cs
b/trunk/Pithos.Core/Agents/BlockExtensions.cs | ||
---|---|---|
43 | 43 |
using System.Collections.Generic; |
44 | 44 |
using System.Diagnostics.Contracts; |
45 | 45 |
using System.Linq; |
46 |
using System.Security.Cryptography; |
|
46 | 47 |
using System.Text; |
47 | 48 |
using System.IO; |
48 | 49 |
using System.Text.RegularExpressions; |
... | ... | |
87 | 88 |
return Signature.CalculateTreeHash(info.FullName, blockSize, algorithm).TopHash.ToHashString(); |
88 | 89 |
|
89 | 90 |
} |
91 |
|
|
92 |
/// <summary> |
|
93 |
///Calculates a simple hash for an entire file |
|
94 |
/// </summary> |
|
95 |
/// <param name="info">The file to hash</param> |
|
96 |
/// <param name="hasher">The hash algorithm to use</param> |
|
97 |
/// <returns>A hash value for the entire file. An empty string if the file does not exist.</returns> |
|
98 |
public static string ComputeShortHash(this FileInfo info, HashAlgorithm hasher) |
|
99 |
{ |
|
100 |
Contract.Requires(info != null); |
|
101 |
Contract.Requires(hasher!= null); |
|
102 |
|
|
103 |
if (!info.Exists) |
|
104 |
return String.Empty; |
|
105 |
|
|
106 |
using (var stream = info.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) |
|
107 |
{ |
|
108 |
var hash = hasher.ComputeHash(stream); |
|
109 |
var hashString = hash.ToHashString(); |
|
110 |
return hashString; |
|
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
public static string ComputeShortHash(this FileInfo info) |
|
115 |
{ |
|
116 |
Contract.Requires(info != null); |
|
117 |
|
|
118 |
using (var hasher=HashAlgorithm.Create("sha1")) |
|
119 |
{ |
|
120 |
return ComputeShortHash(info,hasher); |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
90 | 124 |
} |
91 | 125 |
} |
Also available in: Unified diff