YYFileHash.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // YYFileHash.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/11/2.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <Foundation/Foundation.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /// File hash algorithm type
  14. typedef NS_OPTIONS (NSUInteger, YYFileHashType) {
  15. YYFileHashTypeMD2 = 1 << 0, ///< MD2 hash
  16. YYFileHashTypeMD4 = 1 << 1, ///< MD4 hash
  17. YYFileHashTypeMD5 = 1 << 2, ///< MD5 hash
  18. YYFileHashTypeSHA1 = 1 << 3, ///< SHA1 hash
  19. YYFileHashTypeSHA224 = 1 << 4, ///< SHA224 hash
  20. YYFileHashTypeSHA256 = 1 << 5, ///< SHA256 hash
  21. YYFileHashTypeSHA384 = 1 << 6, ///< SHA384 hash
  22. YYFileHashTypeSHA512 = 1 << 7, ///< SHA512 hash
  23. YYFileHashTypeCRC32 = 1 << 8, ///< crc32 checksum
  24. YYFileHashTypeAdler32 = 1 << 9, ///< adler32 checksum
  25. };
  26. /**
  27. Utility for computing hashes of file with high performance and low memory usage.
  28. See `YYFileHashType` for all supported hash (checksum) type.
  29. Sample Code:
  30. YYFileHash *hash = [YYFileHash hashForFile:@"/tmp/Xcode6.dmg" types:YYFileHashTypeMD5 | YYFileHashTypeSHA1];
  31. NSLog(@"md5:%@ sha1:%@", hash.md5String, hash.sha1String);
  32. */
  33. @interface YYFileHash : NSObject
  34. /**
  35. Start calculate file hash and return the result.
  36. @discussion The calling thread is blocked until the asynchronous hash progress
  37. finished.
  38. @param filePath The path to the file to access.
  39. @param types File hash algorithm types.
  40. @return File hash result, or nil when an error occurs.
  41. */
  42. + (nullable YYFileHash *)hashForFile:(NSString *)filePath types:(YYFileHashType)types;
  43. /**
  44. Start calculate file hash and return the result.
  45. @discussion The calling thread is blocked until the asynchronous hash progress
  46. finished or cancelled.
  47. @param filePath The path to the file to access.
  48. @param types File hash algorithm types.
  49. @param block A block which is called in progress. The block takes 3 arguments:
  50. `totalSize` is the total file size in bytes;
  51. `processedSize` is the processed file size in bytes;
  52. `stop` is a reference to a Boolean value, which can be set to YES to stop
  53. further processing. If the block stop the processing, it just returns nil.
  54. @return File hash result, or nil when an error occurs.
  55. */
  56. + (nullable YYFileHash *)hashForFile:(NSString *)filePath
  57. types:(YYFileHashType)types
  58. usingBlock:(nullable void (^)(UInt64 totalSize, UInt64 processedSize, BOOL *stop))block;
  59. @property (nonatomic, readonly) YYFileHashType types; ///< hash type
  60. @property (nullable, nonatomic, strong, readonly) NSString *md2String; ///< md2 hash string in lowercase
  61. @property (nullable, nonatomic, strong, readonly) NSString *md4String; ///< md4 hash string in lowercase
  62. @property (nullable, nonatomic, strong, readonly) NSString *md5String; ///< md5 hash string in lowercase
  63. @property (nullable, nonatomic, strong, readonly) NSString *sha1String; ///< sha1 hash string in lowercase
  64. @property (nullable, nonatomic, strong, readonly) NSString *sha224String; ///< sha224 hash string in lowercase
  65. @property (nullable, nonatomic, strong, readonly) NSString *sha256String; ///< sha256 hash string in lowercase
  66. @property (nullable, nonatomic, strong, readonly) NSString *sha384String; ///< sha384 hash string in lowercase
  67. @property (nullable, nonatomic, strong, readonly) NSString *sha512String; ///< sha512 hash string in lowercase
  68. @property (nullable, nonatomic, strong, readonly) NSString *crc32String; ///< crc32 checksum string in lowercase
  69. @property (nullable, nonatomic, strong, readonly) NSString *adler32String; ///< adler32 checksum string in lowercase
  70. @property (nullable, nonatomic, strong, readonly) NSData *md2Data; ///< md2 hash
  71. @property (nullable, nonatomic, strong, readonly) NSData *md4Data; ///< md4 hash
  72. @property (nullable, nonatomic, strong, readonly) NSData *md5Data; ///< md5 hash
  73. @property (nullable, nonatomic, strong, readonly) NSData *sha1Data; ///< sha1 hash
  74. @property (nullable, nonatomic, strong, readonly) NSData *sha224Data; ///< sha224 hash
  75. @property (nullable, nonatomic, strong, readonly) NSData *sha256Data; ///< sha256 hash
  76. @property (nullable, nonatomic, strong, readonly) NSData *sha384Data; ///< sha384 hash
  77. @property (nullable, nonatomic, strong, readonly) NSData *sha512Data; ///< sha512 hash
  78. @property (nonatomic, readonly) uint32_t crc32; ///< crc32 checksum
  79. @property (nonatomic, readonly) uint32_t adler32; ///< adler32 checksum
  80. @end
  81. NS_ASSUME_NONNULL_END