您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

160 行
6.5 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System.Globalization;
  22. using System.IO;
  23. using System.Security;
  24. using System.Text;
  25. using Alphaleonis.Win32.Security;
  26. namespace Alphaleonis.Win32.Filesystem
  27. {
  28. public static partial class File
  29. {
  30. /// <summary>[AlphaFS] Calculates the hash/checksum for the given <paramref name="fileFullPath"/>.</summary>
  31. /// <param name="fileFullPath">The name of the file.</param>
  32. /// <param name="hashType">One of the <see cref="HashType"/> values.</param>
  33. [SecurityCritical]
  34. public static string GetHash(string fileFullPath, HashType hashType)
  35. {
  36. return GetHashCore(null, fileFullPath, hashType, PathFormat.RelativePath);
  37. }
  38. /// <summary>[AlphaFS] Calculates the hash/checksum for the given <paramref name="fileFullPath"/>.</summary>
  39. /// <param name="fileFullPath">The name of the file.</param>
  40. /// <param name="hashType">One of the <see cref="HashType"/> values.</param>
  41. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  42. [SecurityCritical]
  43. public static string GetHash(string fileFullPath, HashType hashType, PathFormat pathFormat)
  44. {
  45. return GetHashCore(null, fileFullPath, hashType, pathFormat);
  46. }
  47. /// <summary>[AlphaFS] Calculates the hash/checksum for the given <paramref name="fileFullPath"/>.</summary>
  48. /// <param name="transaction">The transaction.</param>
  49. /// <param name="fileFullPath">The name of the file.</param>
  50. /// <param name="hashType">One of the <see cref="HashType"/> values.</param>
  51. [SecurityCritical]
  52. public static string GetHash(KernelTransaction transaction, string fileFullPath, HashType hashType)
  53. {
  54. return GetHashCore(transaction, fileFullPath, hashType, PathFormat.RelativePath);
  55. }
  56. /// <summary>[AlphaFS] Calculates the hash/checksum for the given <paramref name="fileFullPath"/>.</summary>
  57. /// <param name="transaction">The transaction.</param>
  58. /// <param name="fileFullPath">The name of the file.</param>
  59. /// <param name="hashType">One of the <see cref="HashType"/> values.</param>
  60. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  61. [SecurityCritical]
  62. public static string GetHash(KernelTransaction transaction, string fileFullPath, HashType hashType, PathFormat pathFormat)
  63. {
  64. return GetHashCore(transaction, fileFullPath, hashType, pathFormat);
  65. }
  66. /// <summary>[AlphaFS] Calculates the hash/checksum for the given <paramref name="fileFullPath"/>.</summary>
  67. /// <param name="transaction">The transaction.</param>
  68. /// <param name="hashType">One of the <see cref="HashType"/> values.</param>
  69. /// <param name="fileFullPath">The name of the file.</param>
  70. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  71. [SecurityCritical]
  72. internal static string GetHashCore(KernelTransaction transaction, string fileFullPath, HashType hashType, PathFormat pathFormat)
  73. {
  74. const GetFullPathOptions options = GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck;
  75. var fileNameLp = Path.GetExtendedLengthPathCore(transaction, fileFullPath, pathFormat, options);
  76. byte[] hash = null;
  77. using (var fs = OpenCore(transaction, fileNameLp, FileMode.Open, FileAccess.Read, FileShare.Read, ExtendedFileAttributes.Normal, null, null, PathFormat.LongFullPath))
  78. {
  79. switch (hashType)
  80. {
  81. case HashType.CRC32:
  82. using (var hType = new Crc32())
  83. hash = hType.ComputeHash(fs);
  84. break;
  85. case HashType.CRC64ISO3309:
  86. using (var hType = new Crc64())
  87. hash = hType.ComputeHash(fs);
  88. break;
  89. case HashType.MD5:
  90. using (var hType = System.Security.Cryptography.MD5.Create())
  91. hash = hType.ComputeHash(fs);
  92. break;
  93. case HashType.RIPEMD160:
  94. using (var hType = System.Security.Cryptography.RIPEMD160.Create())
  95. hash = hType.ComputeHash(fs);
  96. break;
  97. case HashType.SHA1:
  98. using (var hType = System.Security.Cryptography.SHA1.Create())
  99. hash = hType.ComputeHash(fs);
  100. break;
  101. case HashType.SHA256:
  102. using (var hType = System.Security.Cryptography.SHA256.Create())
  103. hash = hType.ComputeHash(fs);
  104. break;
  105. case HashType.SHA384:
  106. using (var hType = System.Security.Cryptography.SHA384.Create())
  107. hash = hType.ComputeHash(fs);
  108. break;
  109. case HashType.SHA512:
  110. using (var hType = System.Security.Cryptography.SHA512.Create())
  111. hash = hType.ComputeHash(fs);
  112. break;
  113. }
  114. }
  115. if (null != hash)
  116. {
  117. var sb = new StringBuilder(hash.Length);
  118. foreach (byte b in hash)
  119. sb.Append(b.ToString("X2", CultureInfo.InvariantCulture));
  120. return sb.ToString().ToUpperInvariant();
  121. }
  122. return string.Empty;
  123. }
  124. }
  125. }