bovender framework
C# framework that implements MVVM and more
FileHelpers.cs
1 /* FileHelpers.cs
2  * part of Daniel's XL Toolbox NG
3  *
4  * Copyright 2014-2018 Daniel Kraus
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 using System.IO;
23 using System.Security.Cryptography;
24 
25 namespace Bovender
26 {
27  public static class FileHelpers
28  {
29  #region Public methods
30 
36  public static string Sha256Hash(string file)
37  {
38  using (FileStream fs = SafeFileStream(file))
39  {
40  using (BufferedStream bs = new BufferedStream(fs))
41  {
42  using (SHA256Managed sha = new SHA256Managed())
43  {
44  string hash = Checksum2Hash(sha.ComputeHash(bs));
45  Logger.Info("Sha256Hash: {0}", file);
46  Logger.Info("Sha256Hash: {0}", hash);
47  return hash;
48  }
49  }
50  }
51  }
57  public static string Sha256Hash(Exception exception)
58  {
59  byte[] bytes = Encoding.UTF8.GetBytes(exception.ToString());
60  using (SHA256Managed sha = new SHA256Managed())
61  {
62  string hash = Checksum2Hash(sha.ComputeHash(bytes));
63  Logger.Info("Sha256Hash: {0}", exception.GetType().ToString());
64  Logger.Info("Sha256Hash: {0}", hash);
65  return hash;
66  }
67  }
68 
74  public static string Sha1Hash(string file)
75  {
76  using (FileStream fs = SafeFileStream(file))
77  {
78  using (BufferedStream bs = new BufferedStream(fs))
79  {
80  using (SHA1Managed sha = new SHA1Managed())
81  {
82  string hash = Checksum2Hash(sha.ComputeHash(bs));
83  Logger.Info("Sha1Hash: {0}", file);
84  Logger.Info("Sha1Hash: {0}", hash);
85  return hash;
86  }
87  }
88  }
89  }
90 
91  private static string Checksum2Hash(byte[] bytes)
92  {
93  StringBuilder formatted = new StringBuilder(2 * bytes.Length);
94  foreach (byte b in bytes)
95  {
96  formatted.AppendFormat("{0:x2}", b);
97  }
98  return formatted.ToString();
99  }
100 
101  #endregion
102 
103  #region Private methods
104 
110  private static FileStream SafeFileStream(string file)
111  {
112  int tries = 1;
113  FileStream fs = null;
114  Exception ex = null;
115  Logger.Info("SaveFileStream: Attempting to open \"{0}\"", file);
116  while (tries <= 3 && fs == null)
117  {
118  try
119  {
120  fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
121  }
122  catch (Exception e)
123  {
124  Logger.Warn("SafeFileStream: Failed to open stream on attempt #{0}", tries);
125  Logger.Warn(e);
126  ex = e;
127  System.Threading.Thread.Sleep(333);
128  }
129  tries++;
130  }
131  if (fs == null)
132  {
133  Logger.Fatal("SafeFileStream: Unable to open stream in #{0} attempts!");
134  throw new IOException("Unable to access file", ex);
135  }
136  return fs;
137  }
138 
139  #endregion
140 
141  #region Class logger
142 
143  private static NLog.Logger Logger { get { return _logger.Value; } }
144 
145  private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
146 
147  #endregion
148  }
149 }
static string Sha1Hash(string file)
Computes the Sha1 hash of a given file.
Definition: FileHelpers.cs:74
static string Sha256Hash(Exception exception)
Computes the Sha256 hash of an exception.
Definition: FileHelpers.cs:57
static string Sha256Hash(string file)
Computes the Sha256 hash of a given file.
Definition: FileHelpers.cs:36