FtpHash.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. namespace System.Net.FtpClient {
  5. /// <summary>
  6. /// Represents a computed hash of an object
  7. /// on the FTP server. See the following link
  8. /// for more information:
  9. /// http://tools.ietf.org/html/draft-bryan-ftpext-hash-02
  10. /// </summary>
  11. public class FtpHash {
  12. FtpHashAlgorithm m_algorithm = FtpHashAlgorithm.NONE;
  13. /// <summary>
  14. /// Gets the algorithm that was used to compute the hash
  15. /// </summary>
  16. public FtpHashAlgorithm Algorithm {
  17. get { return m_algorithm; }
  18. internal set { m_algorithm = value; }
  19. }
  20. string m_value = null;
  21. /// <summary>
  22. /// Gets the computed hash returned by the server
  23. /// </summary>
  24. public string Value {
  25. get { return m_value; }
  26. internal set { m_value = value; }
  27. }
  28. /// <summary>
  29. /// Gets a value indicating if this object represents a
  30. /// valid hash response from the server.
  31. /// </summary>
  32. public bool IsValid {
  33. get { return m_algorithm != FtpHashAlgorithm.NONE && !string.IsNullOrEmpty(m_value); }
  34. }
  35. /// <summary>
  36. /// Computes the hash for the specified file and compares
  37. /// it to the value in this object. CRC hashes are not supported
  38. /// because there is no built-in support in the .net framework and
  39. /// a CRC implementation exceeds the scope of this project. If you
  40. /// attempt to Verify() a CRC hash a NotImplemented() exception will
  41. /// be thrown.
  42. /// </summary>
  43. /// <param name="file">The file to compute the hash for</param>
  44. /// <returns>True if the computed hash matches what's stored in this object.</returns>
  45. public bool Verify(string file) {
  46. using (FileStream istream = new FileStream(file, FileMode.Open, FileAccess.Read)) {
  47. return Verify(istream);
  48. }
  49. }
  50. /// <summary>
  51. /// Computes the hash for the specified stream and compares
  52. /// it to the value in this object. CRC hashes are not supported
  53. /// because there is no built-in support in the .net framework and
  54. /// a CRC implementation exceeds the scope of this project. If you
  55. /// attempt to Verify() a CRC hash a NotImplemented() exception will
  56. /// be thrown.
  57. /// </summary>
  58. /// <param name="istream">The stream to compute the hash for</param>
  59. /// <returns>True if the computed hash matches what's stored in this object.</returns>
  60. public bool Verify(Stream istream) {
  61. if (IsValid) {
  62. HashAlgorithm hashAlg = null;
  63. switch (m_algorithm) {
  64. case FtpHashAlgorithm.SHA1:
  65. hashAlg = new SHA1CryptoServiceProvider();
  66. break;
  67. #if !NET2
  68. case FtpHashAlgorithm.SHA256:
  69. hashAlg = new SHA256CryptoServiceProvider();
  70. break;
  71. case FtpHashAlgorithm.SHA512:
  72. hashAlg = new SHA512CryptoServiceProvider();
  73. break;
  74. #endif
  75. case FtpHashAlgorithm.MD5:
  76. hashAlg = new MD5CryptoServiceProvider();
  77. break;
  78. case FtpHashAlgorithm.CRC:
  79. throw new NotImplementedException("There is no built in support for computing CRC hashes.");
  80. default:
  81. throw new NotImplementedException("Unknown hash algorithm: " + m_algorithm.ToString());
  82. }
  83. try {
  84. byte[] data = null;
  85. string hash = "";
  86. data = hashAlg.ComputeHash(istream);
  87. if (data != null) {
  88. foreach (byte b in data) {
  89. hash += b.ToString("x2");
  90. }
  91. return (hash.ToUpper() == m_value.ToUpper());
  92. }
  93. }
  94. finally {
  95. #if !NET2 // .NET 2.0 doesn't provide access to Dispose() for HashAlgorithm
  96. if (hashAlg != null)
  97. hashAlg.Dispose();
  98. #endif
  99. }
  100. }
  101. return false;
  102. }
  103. /// <summary>
  104. /// Creates an empty instance.
  105. /// </summary>
  106. internal FtpHash() {
  107. }
  108. }
  109. }