Created
July 4, 2025 11:57
-
-
Save omidgolparvar/be65eaf12c01f3d099c9b7b0849780c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension SymmetricKey { | |
| // MARK: Custom Initializers | |
| /// Creates a `SymmetricKey` from a Base64-encoded `String`. | |
| /// | |
| /// - Parameter base64EncodedString: The Base64-encoded string from which to generate the `SymmetricKey`. | |
| init?(base64EncodedString: String) { | |
| guard let data = Data(base64Encoded: base64EncodedString) else { | |
| return nil | |
| } | |
| self.init(data: data) | |
| } | |
| // MARK: - Instance Methods | |
| /// Serializes a `SymmetricKey` to a Base64-encoded `String`. | |
| func serialize() -> String { | |
| return self.withUnsafeBytes { body in | |
| Data(body).base64EncodedString() | |
| } | |
| } | |
| } | |
| func symmetricKeyTest() { | |
| let symmetricKey = SymmetricKey(size: .bits256) | |
| let serializedSymmetricKey = symmetricKey.serialize() | |
| guard let deserializedSymmetricKey = SymmetricKey(base64EncodedString: serializedSymmetricKey) else { | |
| print("deserializedSymmetricKey was nil.") | |
| return | |
| } | |
| print("Keys match: \(symmetricKey == deserializedSymmetricKey)") | |
| } | |
| symmetricKeyTest() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment