|
| 1 | +// DesignAlgorithmsKit |
| 2 | +// Hash Algorithm Types |
1 | 3 | // |
2 | | -// HashAlgorithm.swift |
3 | | -// DesignAlgorithmsKit |
4 | | -// |
5 | | -// Hash Algorithm Protocol - Base protocol for hash algorithms |
6 | | -// |
| 4 | +// Hash Algorithm Policy: |
| 5 | +// - SHA-256: Recommended default for new hash generation |
| 6 | +// - SHA-1: Legacy support for existing systems |
| 7 | +// - MD5: Read-only legacy support (validation against companion files, existing checksums) |
| 8 | +// - CRC32: Fast checksum for quick integrity checks |
7 | 9 |
|
8 | | -#if !os(WASI) |
9 | 10 | import Foundation |
10 | 11 |
|
11 | | -#if canImport(CryptoKit) |
12 | | -import CryptoKit |
13 | | -#endif |
14 | | - |
15 | | -/// Protocol for hash algorithms |
16 | | -public protocol HashAlgorithm { |
17 | | - /// Algorithm name |
18 | | - static var name: String { get } |
| 12 | +/// Hash algorithms supported for file and disk image hashing |
| 13 | +public enum HashAlgorithm: String, CaseIterable, Codable, Sendable { |
| 14 | + /// SHA-256: Recommended default for new hash generation (cryptographically secure) |
| 15 | + case sha256 = "sha256" |
19 | 16 |
|
20 | | - /// Hash data using this algorithm |
21 | | - /// - Parameter data: Data to hash |
22 | | - /// - Returns: Hash value as Data |
23 | | - static func hash(data: Data) -> Data |
| 17 | + /// SHA-1: Legacy support for existing systems (deprecated but collision-resistant) |
| 18 | + case sha1 = "sha1" |
24 | 19 |
|
25 | | - /// Hash a string using this algorithm |
26 | | - /// - Parameter string: String to hash |
27 | | - /// - Returns: Hash value as Data |
28 | | - static func hash(string: String) -> Data |
29 | | -} |
30 | | - |
31 | | -extension HashAlgorithm { |
32 | | - /// Default implementation for string hashing |
33 | | - /// - Parameter string: String to hash |
34 | | - /// - Returns: Hash value as Data, or empty Data if UTF-8 conversion fails |
35 | | - /// - Note: UTF-8 conversion failure returns empty Data, which will hash to a valid hash value. |
36 | | - /// This path is testable by creating strings that fail UTF-8 conversion (rare but possible). |
37 | | - public static func hash(string: String) -> Data { |
38 | | - guard let data = string.data(using: .utf8) else { |
39 | | - // UTF-8 conversion failed - return hash of empty data |
40 | | - // This is a valid fallback that ensures we always return a hash |
41 | | - return hash(data: Data()) |
| 20 | + /// MD5: Read-only legacy support for validation against companion files and existing checksums |
| 21 | + /// ⚠️ Do not use for new hash generation - use SHA-256 instead |
| 22 | + /// ✅ Supported for: reading companion checksum files (.md5, .md5sum), validating against existing MD5 hashes |
| 23 | + case md5 = "md5" |
| 24 | + |
| 25 | + /// CRC32: Fast checksum for quick integrity checks (not cryptographic) |
| 26 | + case crc32 = "crc32" |
| 27 | + |
| 28 | + public var displayName: String { |
| 29 | + rawValue.uppercased() |
| 30 | + } |
| 31 | + |
| 32 | + /// Whether this algorithm is recommended for new hash generation |
| 33 | + public var isRecommendedForNewHashes: Bool { |
| 34 | + switch self { |
| 35 | + case .sha256: |
| 36 | + return true |
| 37 | + case .sha1, .md5, .crc32: |
| 38 | + return false |
42 | 39 | } |
43 | | - return hash(data: data) |
44 | 40 | } |
45 | | -} |
46 | | - |
47 | | -/// SHA-256 hash algorithm |
48 | | -public enum SHA256: HashAlgorithm { |
49 | | - public static let name = "SHA-256" |
50 | 41 |
|
51 | | - public static func hash(data: Data) -> Data { |
52 | | - #if canImport(CryptoKit) |
53 | | - let digest = CryptoKit.SHA256.hash(data: data) |
54 | | - return Data(digest) |
55 | | - #else |
56 | | - // Fallback implementation |
57 | | - // In production, use CommonCrypto or another crypto library |
58 | | - return fallbackHash(data: data) |
59 | | - #endif |
| 42 | + /// Whether this algorithm is suitable for read-only validation (companion files, existing checksums) |
| 43 | + public var isSuitableForValidation: Bool { |
| 44 | + // All algorithms can be used for validation |
| 45 | + return true |
60 | 46 | } |
61 | 47 |
|
62 | | - #if !canImport(CryptoKit) |
63 | | - /// Fallback hash implementation (simple, not cryptographically secure) |
64 | | - /// For production use, import CryptoKit or CommonCrypto |
65 | | - /// - Note: This path is conditionally compiled and only available when CryptoKit is not available. |
66 | | - /// It cannot be tested in environments where CryptoKit is available (like macOS/iOS test environments). |
67 | | - /// The fallback implementation is intentionally simple and not cryptographically secure. |
68 | | - private static func fallbackHash(data: Data) -> Data { |
69 | | - var hash = Data(count: 32) |
70 | | - data.withUnsafeBytes { dataBytes in |
71 | | - hash.withUnsafeMutableBytes { hashBytes in |
72 | | - // Simple hash (NOT cryptographically secure) |
73 | | - // This is a placeholder - use CryptoKit in production |
74 | | - for i in 0..<32 { |
75 | | - var value: UInt8 = 0 |
76 | | - for j in 0..<dataBytes.count { |
77 | | - value ^= dataBytes[j] &+ UInt8(i) |
78 | | - } |
79 | | - hashBytes[i] = value |
80 | | - } |
81 | | - } |
| 48 | + /// Hash size in bytes |
| 49 | + public var hashSize: Int { |
| 50 | + switch self { |
| 51 | + case .crc32: |
| 52 | + return 4 |
| 53 | + case .md5: |
| 54 | + return 16 |
| 55 | + case .sha1: |
| 56 | + return 20 |
| 57 | + case .sha256: |
| 58 | + return 32 |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// Whether this algorithm is suitable for small files (< 1MB) |
| 63 | + /// All algorithms are fast enough for small files, but SHA-256 is recommended |
| 64 | + public var isSuitableForSmallFiles: Bool { |
| 65 | + // All algorithms are suitable, but SHA-256 is recommended |
| 66 | + return true |
| 67 | + } |
| 68 | + |
| 69 | + /// Recommended algorithm for small files |
| 70 | + public static var recommendedForSmallFiles: HashAlgorithm { |
| 71 | + return .sha256 |
| 72 | + } |
| 73 | + |
| 74 | + /// Recommended algorithm for millions of files |
| 75 | + /// SHA-256 is recommended up to 100M files (3.2 GB storage overhead) |
| 76 | + /// For > 100M files, consider two-stage approach (CRC32 filter + SHA-256 verification) |
| 77 | + public static var recommendedForMillionsOfFiles: HashAlgorithm { |
| 78 | + return .sha256 |
| 79 | + } |
| 80 | + |
| 81 | + /// Storage overhead in MB for N files |
| 82 | + public func storageOverheadMB(for fileCount: Int) -> Double { |
| 83 | + return (Double(hashSize) * Double(fileCount)) / (1024.0 * 1024.0) |
| 84 | + } |
| 85 | + |
| 86 | + /// Whether this algorithm is suitable for millions of files |
| 87 | + /// SHA-256 is suitable up to ~100M files (3.2 GB storage) |
| 88 | + /// CRC32 has high collision risk for millions of files |
| 89 | + public var isSuitableForMillionsOfFiles: Bool { |
| 90 | + switch self { |
| 91 | + case .sha256, .sha1: |
| 92 | + return true // Secure, negligible collision risk |
| 93 | + case .md5: |
| 94 | + return true // Negligible collision risk, but cryptographically broken |
| 95 | + case .crc32: |
| 96 | + return false // High collision risk for millions of files |
82 | 97 | } |
83 | | - return hash |
84 | 98 | } |
85 | | - #endif |
86 | 99 | } |
87 | | -#endif |
88 | | - |
|
0 commit comments