Skip to content

Instantly share code, notes, and snippets.

@NSEGeorge
Created April 20, 2020 14:07
Show Gist options
  • Save NSEGeorge/33b4b402c73b1a451b3a4830d19e900c to your computer and use it in GitHub Desktop.
Save NSEGeorge/33b4b402c73b1a451b3a4830d19e900c to your computer and use it in GitHub Desktop.
extension SymmetricKey {
init(string keyString: String, size: SymmetricKeySize = .bits256) throws {
guard var keyData = keyString.data(using: .utf8) else {
print("Could not create base64 encoded Data from String.")
throw CryptoKitError.incorrectParameterSize
}
let keySizeBytes = size.bitCount / 8
keyData = keyData.subdata(in: 0..<keySizeBytes)
guard keyData.count >= keySizeBytes else { throw CryptoKitError.incorrectKeySize }
self.init(data: keyData)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment