|
1 | 1 | #if canImport(CryptoKit) |
2 | | -import CryptoKit |
3 | | -import Foundation |
| 2 | + import CryptoKit |
| 3 | + import Foundation |
4 | 4 |
|
5 | | -extension Data { |
6 | | - /// Encrypts this plain `Data` using AES.GCM with the provided key. |
7 | | - /// This method is useful for encrypting data before securely storing or transmitting it. |
8 | | - /// Ensure that the `SymmetricKey` used for encryption is securely managed and stored. |
9 | | - /// |
10 | | - /// Example: |
11 | | - /// ```swift |
12 | | - /// let key = SymmetricKey(size: .bits256) |
13 | | - /// let plainData = "Harry Potter is a 🧙".data(using: .utf8)! |
14 | | - /// do { |
15 | | - /// let encryptedData = try plainData.encrypted(key: key) |
16 | | - /// // Use encryptedData as needed |
17 | | - /// } catch { |
18 | | - /// print("Encryption failed: \(error)") |
19 | | - /// } |
20 | | - /// ``` |
21 | | - /// |
22 | | - /// - Parameters: |
23 | | - /// - key: The symmetric key to use for encryption. |
24 | | - /// - Returns: The encrypted `Data`. |
25 | | - /// - Throws: An error if encryption fails. |
26 | | - /// - Note: Available on iOS 13, macOS 10.15, tvOS 13, visionOS 1, watchOS 6, and later. |
27 | | - @available(iOS 13, macOS 10.15, tvOS 13, visionOS 1, watchOS 6, *) |
28 | | - public func encrypted(key: SymmetricKey) throws -> Data { |
29 | | - try AES.GCM.seal(self, using: key).combined! |
30 | | - } |
| 5 | + extension Data { |
| 6 | + /// Encrypts this plain `Data` using AES.GCM with the provided key. |
| 7 | + /// This method is useful for encrypting data before securely storing or transmitting it. |
| 8 | + /// Ensure that the `SymmetricKey` used for encryption is securely managed and stored. |
| 9 | + /// |
| 10 | + /// Example: |
| 11 | + /// ```swift |
| 12 | + /// let key = SymmetricKey(size: .bits256) |
| 13 | + /// let plainData = "Harry Potter is a 🧙".data(using: .utf8)! |
| 14 | + /// do { |
| 15 | + /// let encryptedData = try plainData.encrypted(key: key) |
| 16 | + /// // Use encryptedData as needed |
| 17 | + /// } catch { |
| 18 | + /// print("Encryption failed: \(error)") |
| 19 | + /// } |
| 20 | + /// ``` |
| 21 | + /// |
| 22 | + /// - Parameters: |
| 23 | + /// - key: The symmetric key to use for encryption. |
| 24 | + /// - Returns: The encrypted `Data`. |
| 25 | + /// - Throws: An error if encryption fails. |
| 26 | + /// - Note: Available on iOS 13, macOS 10.15, tvOS 13, visionOS 1, watchOS 6, and later. |
| 27 | + @available(iOS 13, macOS 10.15, tvOS 13, visionOS 1, watchOS 6, *) |
| 28 | + public func encrypted(key: SymmetricKey) throws -> Data { |
| 29 | + try AES.GCM.seal(self, using: key).combined! |
| 30 | + } |
31 | 31 |
|
32 | | - /// Decrypts this encrypted data using AES.GCM with the provided key. |
33 | | - /// This method is crucial for converting encrypted data back to its original form securely. |
34 | | - /// Ensure the `SymmetricKey` used matches the one used for encryption. |
35 | | - /// |
36 | | - /// Example: |
37 | | - /// ```swift |
38 | | - /// let key = SymmetricKey(size: .bits256) |
39 | | - /// // Assuming encryptedData is the Data we previously encrypted |
40 | | - /// do { |
41 | | - /// let decryptedData = try encryptedData.decrypted(key: key) |
42 | | - /// let decryptedString = String(data: decryptedData, encoding: .utf8)! |
43 | | - /// // Use decryptedString or decryptedData as needed |
44 | | - /// } catch { |
45 | | - /// print("Decryption failed: \(error)") |
46 | | - /// } |
47 | | - /// ``` |
48 | | - /// |
49 | | - /// - Parameters: |
50 | | - /// - key: The symmetric key to use for decryption. |
51 | | - /// - Returns: The decrypted `Data`. |
52 | | - /// - Throws: An error if decryption fails. |
53 | | - /// - Note: Available on iOS 13, macOS 10.15, tvOS 13, visionOS 1, watchOS 6, and later. |
54 | | - @available(iOS 13, macOS 10.15, tvOS 13, visionOS 1, watchOS 6, *) |
55 | | - public func decrypted(key: SymmetricKey) throws -> Data { |
56 | | - let sealedBox = try AES.GCM.SealedBox(combined: self) |
57 | | - return try AES.GCM.open(sealedBox, using: key) |
| 32 | + /// Decrypts this encrypted data using AES.GCM with the provided key. |
| 33 | + /// This method is crucial for converting encrypted data back to its original form securely. |
| 34 | + /// Ensure the `SymmetricKey` used matches the one used for encryption. |
| 35 | + /// |
| 36 | + /// Example: |
| 37 | + /// ```swift |
| 38 | + /// let key = SymmetricKey(size: .bits256) |
| 39 | + /// // Assuming encryptedData is the Data we previously encrypted |
| 40 | + /// do { |
| 41 | + /// let decryptedData = try encryptedData.decrypted(key: key) |
| 42 | + /// let decryptedString = String(data: decryptedData, encoding: .utf8)! |
| 43 | + /// // Use decryptedString or decryptedData as needed |
| 44 | + /// } catch { |
| 45 | + /// print("Decryption failed: \(error)") |
| 46 | + /// } |
| 47 | + /// ``` |
| 48 | + /// |
| 49 | + /// - Parameters: |
| 50 | + /// - key: The symmetric key to use for decryption. |
| 51 | + /// - Returns: The decrypted `Data`. |
| 52 | + /// - Throws: An error if decryption fails. |
| 53 | + /// - Note: Available on iOS 13, macOS 10.15, tvOS 13, visionOS 1, watchOS 6, and later. |
| 54 | + @available(iOS 13, macOS 10.15, tvOS 13, visionOS 1, watchOS 6, *) |
| 55 | + public func decrypted(key: SymmetricKey) throws -> Data { |
| 56 | + let sealedBox = try AES.GCM.SealedBox(combined: self) |
| 57 | + return try AES.GCM.open(sealedBox, using: key) |
| 58 | + } |
58 | 59 | } |
59 | | -} |
60 | 60 | #endif |
0 commit comments