public static byte[] ComputeHash(System.Security.Cryptography.HashAlgorithm algorithm, byte[] inputBytes) { // calculate hash from inputBytes using provided algorithm byte[] hash = algorithm.ComputeHash(inputBytes); return hash; }
public static string ComputeFileHash(string filename, System.Security.Cryptography.HashAlgorithm alg) { StringBuilder sb = new StringBuilder(); FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); using (fs) { byte[] actualHash = alg.ComputeHash(fs); foreach (Byte b in actualHash) sb.Append(b.ToString("X2")); } return sb.ToString(); }
public ETextingDomainModel.AuthToken CreateAuthToken(ETextingDomainModel.ApiUser user, System.Security.Cryptography.HMACSHA256 provider) { var rawTokenInfo = string.Concat(user.AppId + DateTime.UtcNow.ToString("d")); var rawTokenByte = Encoding.UTF8.GetBytes(rawTokenInfo); var token = provider.ComputeHash(rawTokenByte); var authToken = new AuthToken() { Token = Convert.ToBase64String(token), Expiration = DateTime.UtcNow.AddDays(7), ApiUser = user }; return authToken; }
private static string GetMd5Hash(System.Security.Cryptography.MD5 md5Hash, string input) { // Convert the input string to a byte array and compute the hash. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // Return the hexadecimal string. return sBuilder.ToString(); }
private static void ScanForExistingTargetBlocks(LocalRestoreDatabase database, byte[] blockbuffer, System.Security.Cryptography.HashAlgorithm blockhasher, System.Security.Cryptography.HashAlgorithm filehasher, Options options, RestoreResults result) { // Scan existing files for existing BLOCKS using(var blockmarker = database.CreateBlockMarker()) { var updateCount = 0L; foreach(var restorelist in database.GetExistingFilesWithBlocks()) { var rename = !options.Overwrite; var targetpath = restorelist.TargetPath; var targetfileid = restorelist.TargetFileID; var targetfilehash = restorelist.TargetHash; if (m_systemIO.FileExists(targetpath)) { try { if (result.TaskControlRendevouz() == TaskControlState.Stop) return; if (rename) filehasher.Initialize(); using(var file = m_systemIO.FileOpenReadWrite(targetpath)) using(var block = new Blockprocessor(file, blockbuffer)) foreach(var targetblock in restorelist.Blocks) { var size = block.Readblock(); if (size <= 0) break; if (size == targetblock.Size) { var key = Convert.ToBase64String(blockhasher.ComputeHash(blockbuffer, 0, size)); if (key == targetblock.Hash) { blockmarker.SetBlockRestored(targetfileid, targetblock.Index, key, size); } } if (rename) filehasher.TransformBlock(blockbuffer, 0, size, blockbuffer, 0); } if (rename) { filehasher.TransformFinalBlock(blockbuffer, 0, 0); var filekey = Convert.ToBase64String(filehasher.Hash); if (filekey == targetfilehash) { result.AddVerboseMessage("Target file exists and is correct version: {0}", targetpath); rename = false; } else { // The new file will have none of the correct blocks, // even if the scanned file had some blockmarker.SetAllBlocksMissing(targetfileid); } } if (updateCount++ % 20 == 0) { blockmarker.UpdateProcessed(result.OperationProgressUpdater); if (result.TaskControlRendevouz() == TaskControlState.Stop) return; } } catch (Exception ex) { result.AddWarning(string.Format("Failed to read target file: \"{0}\", message: {1}", targetpath, ex.Message), ex); if (ex is System.Threading.ThreadAbortException) throw; } } else { result.AddVerboseMessage("Target file does not exist: {0}", targetpath); rename = false; } if (rename) { //Select a new filename var ext = m_systemIO.PathGetExtension(targetpath) ?? ""; if (!string.IsNullOrEmpty(ext) && !ext.StartsWith(".")) ext = "." + ext; // First we try with a simple date append, assuming that there are not many conflicts there var newname = m_systemIO.PathChangeExtension(targetpath, null) + "." + database.RestoreTime.ToLocalTime().ToString("yyyy-MM-dd"); var tr = newname + ext; var c = 0; while (m_systemIO.FileExists(tr) && c < 1000) { try { // If we have a file with the correct name, // it is most likely the file we want filehasher.Initialize(); string key; using(var file = m_systemIO.FileOpenReadWrite(tr)) key = Convert.ToBase64String(filehasher.ComputeHash(file)); if (key == targetfilehash) { blockmarker.SetAllBlocksRestored(targetfileid); break; } } catch(Exception ex) { result.AddWarning(string.Format("Failed to read candidate restore target {0}", tr), ex); } tr = newname + " (" + (c++).ToString() + ")" + ext; } newname = tr; result.AddVerboseMessage("Target file exists and will be restored to: {0}", newname); database.UpdateTargetPath(targetfileid, newname); } } blockmarker.UpdateProcessed(result.OperationProgressUpdater); blockmarker.Commit(result); } }
private static void ScanForExistingSourceBlocks(LocalRestoreDatabase database, Options options, byte[] blockbuffer, System.Security.Cryptography.HashAlgorithm hasher, RestoreResults result) { // Fill BLOCKS with data from known local source files using (var blockmarker = database.CreateBlockMarker()) { var updateCount = 0L; foreach (var restorelist in database.GetFilesAndSourceBlocks()) { var targetpath = restorelist.TargetPath; var targetfileid = restorelist.TargetFileID; var patched = false; try { if (result.TaskControlRendevouz() == TaskControlState.Stop) return; var folderpath = m_systemIO.PathGetDirectoryName(targetpath); if (!options.Dryrun && !m_systemIO.DirectoryExists(folderpath)) { result.AddWarning(string.Format("Creating missing folder {0} for file {1}", folderpath, targetpath), null); m_systemIO.DirectoryCreate(folderpath); } using (var file = options.Dryrun ? null : m_systemIO.FileOpenReadWrite(targetpath)) using (var block = new Blockprocessor(file, blockbuffer)) foreach (var targetblock in restorelist.Blocks) { if (!options.Dryrun) file.Position = targetblock.Offset; foreach (var source in targetblock.Blocksources) { try { if (result.TaskControlRendevouz() == TaskControlState.Stop) return; if (m_systemIO.FileExists(source.Path)) using (var sourcefile = m_systemIO.FileOpenRead(source.Path)) { sourcefile.Position = source.Offset; var size = sourcefile.Read(blockbuffer, 0, blockbuffer.Length); if (size == targetblock.Size) { var key = Convert.ToBase64String(hasher.ComputeHash(blockbuffer, 0, size)); if (key == targetblock.Hash) { patched = true; if (!options.Dryrun) file.Write(blockbuffer, 0, size); blockmarker.SetBlockRestored(targetfileid, targetblock.Index, key, targetblock.Size); break; } } } } catch (Exception ex) { result.AddWarning(string.Format("Failed to patch file: \"{0}\" with data from local file \"{1}\", message: {2}", targetpath, source.Path, ex.Message), ex); if (ex is System.Threading.ThreadAbortException) throw; } } } if (updateCount++ % 20 == 0) blockmarker.UpdateProcessed(result.OperationProgressUpdater); } catch (Exception ex) { result.AddWarning(string.Format("Failed to patch file: \"{0}\" with local data, message: {1}", targetpath, ex.Message), ex); } if (patched) result.AddVerboseMessage("Target file is patched with some local data: {0}", targetpath); else result.AddVerboseMessage("Target file is not patched any local data: {0}", targetpath); if (patched && options.Dryrun) result.AddDryrunMessage(string.Format("Would patch file with local data: {0}", targetpath)); } blockmarker.UpdateProcessed(result.OperationProgressUpdater); blockmarker.Commit(result); } }
private static void ScanForExistingTargetBlocks(LocalRestoreDatabase database, byte[] blockbuffer, System.Security.Cryptography.HashAlgorithm blockhasher, System.Security.Cryptography.HashAlgorithm filehasher, Options options, RestoreResults result) { // Scan existing files for existing BLOCKS using(var blockmarker = database.CreateBlockMarker()) { var updateCount = 0L; foreach(var restorelist in database.GetExistingFilesWithBlocks()) { var rename = !options.Overwrite; var targetpath = restorelist.TargetPath; var targetfileid = restorelist.TargetFileID; var targetfilehash = restorelist.TargetHash; var targetfilelength = restorelist.Length; if (m_systemIO.FileExists(targetpath)) { try { if (result.TaskControlRendevouz() == TaskControlState.Stop) return; var currentfilelength = m_systemIO.FileLength(targetpath); var wasTruncated = false; // Adjust file length in overwrite mode if necessary (smaller is ok, will be extended during restore) // We do it before scanning for blocks. This allows full verification on files that only needs to // be truncated (i.e. forthwritten log files). if (!rename && currentfilelength > targetfilelength) { var currentAttr = m_systemIO.GetFileAttributes(targetpath); if ((currentAttr & System.IO.FileAttributes.ReadOnly) != 0) // clear readonly attribute { if (options.Dryrun) result.AddDryrunMessage(string.Format("Would reset read-only attribute on file: {0}", targetpath)); else m_systemIO.SetFileAttributes(targetpath, currentAttr & ~System.IO.FileAttributes.ReadOnly); } if (options.Dryrun) result.AddDryrunMessage(string.Format("Would truncate file '{0}' to length of {1:N0} bytes", targetpath, targetfilelength)); else { using (var file = m_systemIO.FileOpenWrite(targetpath)) file.SetLength(targetfilelength); currentfilelength = targetfilelength; } wasTruncated = true; } // If file size does not match and we have to rename on conflict, // the whole scan can be skipped here because all blocks have to be restored anyway. // For the other cases, we will check block and and file hashes and look for blocks // to be restored and files that can already be verified. if (!rename || currentfilelength == targetfilelength) { // a file hash for verification will only be necessary if the file has exactly // the wanted size so we have a chance to already mark the file as data-verified. bool calcFileHash = (currentfilelength == targetfilelength); if (calcFileHash) filehasher.Initialize(); using (var file = m_systemIO.FileOpenRead(targetpath)) using (var block = new Blockprocessor(file, blockbuffer)) foreach (var targetblock in restorelist.Blocks) { var size = block.Readblock(); if (size <= 0) break; //TODO: Handle Metadata bool blockhashmatch = false; if (size == targetblock.Size) { // Parallelize file hash calculation on rename. Running read-only on same array should not cause conflicts or races. // Actually, in future always calculate the file hash and mark the file data as already verified. System.Threading.Tasks.Task calcFileHashTask = null; if (calcFileHash) calcFileHashTask = System.Threading.Tasks.Task.Run( () => filehasher.TransformBlock(blockbuffer, 0, size, blockbuffer, 0)); var key = Convert.ToBase64String(blockhasher.ComputeHash(blockbuffer, 0, size)); if (calcFileHashTask != null) calcFileHashTask.Wait(); // wait because blockbuffer will be overwritten. if (key == targetblock.Hash) { blockmarker.SetBlockRestored(targetfileid, targetblock.Index, key, size, false); blockhashmatch = true; } } if (calcFileHash && !blockhashmatch) // will not be necessary anymore { filehasher.TransformFinalBlock(blockbuffer, 0, 0); // So a new initialize will not throw calcFileHash = false; if (rename) // file does not match. So break. break; } } bool fullfilehashmatch = false; if (calcFileHash) // now check if files are identical { filehasher.TransformFinalBlock(blockbuffer, 0, 0); var filekey = Convert.ToBase64String(filehasher.Hash); fullfilehashmatch = (filekey == targetfilehash); } if (!rename && !fullfilehashmatch && !wasTruncated) // Reset read-only attribute (if set) to overwrite { var currentAttr = m_systemIO.GetFileAttributes(targetpath); if ((currentAttr & System.IO.FileAttributes.ReadOnly) != 0) { if (options.Dryrun) result.AddDryrunMessage(string.Format("Would reset read-only attribute on file: {0}", targetpath)); else m_systemIO.SetFileAttributes(targetpath, currentAttr & ~System.IO.FileAttributes.ReadOnly); } } if (fullfilehashmatch) { //TODO: Check metadata to trigger rename? If metadata changed, it will still be restored for the file in-place. blockmarker.SetFileDataVerified(targetfileid); result.AddVerboseMessage("Target file exists{1} and is correct version: {0}", targetpath, wasTruncated ? " (but was truncated)" : ""); rename = false; } else if (rename) { // The new file will have none of the correct blocks, // even if the scanned file had some blockmarker.SetAllBlocksMissing(targetfileid); } } if ((++updateCount) % 20 == 0) { blockmarker.UpdateProcessed(result.OperationProgressUpdater); if (result.TaskControlRendevouz() == TaskControlState.Stop) return; } } catch (Exception ex) { result.AddWarning(string.Format("Failed to read target file: \"{0}\", message: {1}", targetpath, ex.Message), ex); if (ex is System.Threading.ThreadAbortException) throw; } } else { result.AddVerboseMessage("Target file does not exist: {0}", targetpath); rename = false; } if (rename) { //Select a new filename var ext = m_systemIO.PathGetExtension(targetpath) ?? ""; if (!string.IsNullOrEmpty(ext) && !ext.StartsWith(".")) ext = "." + ext; // First we try with a simple date append, assuming that there are not many conflicts there var newname = m_systemIO.PathChangeExtension(targetpath, null) + "." + database.RestoreTime.ToLocalTime().ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); var tr = newname + ext; var c = 0; while (m_systemIO.FileExists(tr) && c < 1000) { try { // If we have a file with the correct name, // it is most likely the file we want filehasher.Initialize(); string key; using(var file = m_systemIO.FileOpenRead(tr)) key = Convert.ToBase64String(filehasher.ComputeHash(file)); if (key == targetfilehash) { //TODO: Also needs metadata check to make correct decision. // We stick to the policy to restore metadata in place, if data ok. So, metadata block may be restored. blockmarker.SetAllBlocksRestored(targetfileid, false); blockmarker.SetFileDataVerified(targetfileid); break; } } catch(Exception ex) { result.AddWarning(string.Format("Failed to read candidate restore target {0}", tr), ex); } tr = newname + " (" + (c++).ToString() + ")" + ext; } newname = tr; result.AddVerboseMessage("Target file exists and will be restored to: {0}", newname); database.UpdateTargetPath(targetfileid, newname); } } blockmarker.UpdateProcessed(result.OperationProgressUpdater); blockmarker.Commit(result); } }
// Hashes // public static byte[] HashTable(Table Data, System.Security.Cryptography.HashAlgorithm Hasher) { byte[] b = new byte[Hasher.HashSize / 8]; bool Begining = true; foreach (RecordSet rs in Data.Extents) { using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { // Write the value of the current hash if it is not on the first run // if (Begining) bw.Write(b); Begining = false; // Write the value of the data // BinarySerializer.WriteRecords(bw, rs._Cache); b = Hasher.ComputeHash(ms.ToArray()); } } } return b; }
private string _HashStandard(string file, System.Security.Cryptography.HashAlgorithm algorithm) { string ret; using (var fs = new FileStream(file, FileMode.Open)) { using (algorithm) { var hash = algorithm.ComputeHash(fs); ret = _FormatBytes(hash); } } return ret; }