feat: Add ability to generate keys within BlueECC#8
Conversation
README.md
Outdated
| You can then view this key by decoding it to the PEM format: | ||
|
|
||
| ```swift | ||
| let privateKeyPEM = try p256PrivateKey.decodeToPEM() |
There was a problem hiding this comment.
Could we write let privateKeyPEM: String so it's clear what type you get back?
Also, I think that this should be just ECPrivateKey.pemString (a cached property) rather than computing it. Since creating a new private key is expensive, it isn't a problem to pay the extra cost of generating and storing the PEM string up front. Then your private and public keys have a consistent API.
README.md
Outdated
|
|
||
| You can generate a `p-256` private key as a `.p8` file for Apple services from [https://developer.apple.com/account/ios/authkey](https://developer.apple.com/account/ios/authkey/). This will produce a key that should be formatted as follows: | ||
| ```swift | ||
| let p256PrivateKey = try ECPrivateKey(for: .prime256v1) |
There was a problem hiding this comment.
Rather than an initializer, I'd use a static factory function here to return a new instance.
The Swift API design guidelines suggest that make() is the appropriate keyword. So something like:
public static func make(for curve: EllipticCurve) throws -> ECPrivateKey| - Returns: An ECPrivateKey. | ||
| - Throws: An ECError if the key fails to be created. | ||
| */ | ||
| public init(for curve: EllipticCurve) throws { |
There was a problem hiding this comment.
As per previous comment, make this private and then supply a factory method that returns a new instance.
README.md
Outdated
| You can then view the key in it's PEM format as follows: | ||
|
|
||
| ```swift | ||
| let privateKeyPEM = try p256PrivateKey.pemString |
There was a problem hiding this comment.
I don't think you need the try any more here?
| private static func derToPrivatePEM(derData: Data) -> String { | ||
| // First convert the DER data to a base64 string... | ||
| let base64String = derData.base64EncodedString() | ||
| // Split the string into strings of length 65... |
| /// | ||
| /// - Returns: `[String]` containing each string. | ||
| /// | ||
| func split(to length: Int) -> [String] { |
There was a problem hiding this comment.
There's almost certainly a more efficient way of doing this using String.Index and String.insert, but that can wait for a future PR.
This pull request adds new API to generate ECPrivate keys for 256, 384 and 512 curves.
It also adds the ability to decode ECPrivate keys to PEM format.
Examples of The new API's: