-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
64?
/// | ||
/// - Returns: `[String]` containing each string. | ||
/// | ||
func split(to length: Int) -> [String] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: