Skip to content
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

Merged
merged 21 commits into from
Mar 12, 2019
Merged

Conversation

Andrew-Lees11
Copy link
Contributor

@Andrew-Lees11 Andrew-Lees11 commented Mar 7, 2019

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:

let p256PrivateKey = try ECPrivateKey(for: .prime256v1)
let privatePem = p256PrivateKey.decodeToPEM()

README.md Outdated
You can then view this key by decoding it to the PEM format:

```swift
let privateKeyPEM = try p256PrivateKey.decodeToPEM()
Copy link
Contributor

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)
Copy link
Contributor

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 {
Copy link
Contributor

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.

Sources/CryptorECC/ECPrivateKey.swift Show resolved Hide resolved
README.md Outdated
You can then view the key in it's PEM format as follows:

```swift
let privateKeyPEM = try p256PrivateKey.pemString
Copy link
Contributor

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...
Copy link
Contributor

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] {
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants