I am messing about with Apple's new CryptoKit framework on Xcode 11.0 beta 2. I want to create a SymmetricKey, then obtain the raw bytes of the key. I would like to use those bytes to create the same key, then check to make sure the keys are equal. From what I can understand in the documentation, the only way to get access to the raw bytes of a key is by using the withUnsafeBytes(_:) method. I have a Playground with the following code:
import Foundation
import CryptoKit
let key1 = SymmetricKey(size: .bits256)
key1.withUnsafeBytes { body in
let rawKeyBytes = body.load(as: Data.self)
let key2 = SymmetricKey(data: rawKeyBytes)
print("Are they equal? \(key1 == key2)")
}
The output of this is Are they equal? false
, so unfortunately the keys do not match. Assuming I could get these keys to match, I'm also not sure how to convert rawKeyBytes
into a string in order to view it in my Playground output. Overall I'm just not very familiar with UnsafeRawBufferPointer or ContiguousBytes.
Are they equal? true
– Conceptacle