ECPrivateKey

public class ECPrivateKey

Represents an elliptic curve private key.
Supported curves are:

Usage Example:

let pemKey = """
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIJX+87WJ7Gh19sohyZnhxZeXYNOcuGv4Q+8MLge4UkaZoAoGCCqGSM49
AwEHoUQDQgAEikc5m6C2xtDWeeAeT18WElO37zvFOz8p4kAlhvgIHN23XIClNESg
KVmLgSSq2asqiwdrU5YHbcHFkgdABM1SPA==
-----END EC PRIVATE KEY-----
"""
let privateKey = try ECPrivateKey(key: pemKey)
let signature = "Hello world".sign(with: privateKey)
  • A String description of the curve this key was generated from.

    Declaration

    Swift

    public let curveId: String
  • The EllipticCurve this key was generated from.

    Declaration

    Swift

    public let curve: EllipticCurve
  • The private key represented as a PEM String.

    Declaration

    Swift

    public let pemString: String
  • Initialize an ECPrivateKey from a PEM String. This can either be from a .p8 file with the header —–BEGIN PRIVATE KEY—–, or from a .pem file with the header —–BEGIN EC PRIVATE KEY—–.

    Usage Example:

    let privateKeyString = """
    -----BEGIN EC PRIVATE KEY-----
    MHcCAQEEIJX+87WJ7Gh19sohyZnhxZeXYNOcuGv4Q+8MLge4UkaZoAoGCCqGSM49
    AwEHoUQDQgAEikc5m6C2xtDWeeAeT18WElO37zvFOz8p4kAlhvgIHN23XIClNESg
    KVmLgSSq2asqiwdrU5YHbcHFkgdABM1SPA==
    -----END EC PRIVATE KEY-----
    """
    let key = try ECPrivateKey(key: privateKeyString)
    

    Throws

    An ECError if the PEM string can’t be decoded or is not a valid key.

    Declaration

    Swift

    public convenience init(key: String) throws

    Parameters

    key

    The elliptic curve private key as a PEM string.

    Return Value

    An ECPrivateKey.

  • Initialize an ECPrivateKey from a PKCS8 .der file data.
    This is equivalent to a PEM String that has had the —–BEGIN PRIVATE KEY—– header and footer stripped and been base64 encoded to ASN1 Data.

    Throws

    An ECError if the Data can’t be decoded or is not a valid key.

    Declaration

    Swift

    public init(pkcs8DER: Data) throws

    Parameters

    pkcs8DER

    The elliptic curve private key Data.

    Return Value

    An ECPrivateKey.

  • Initialize an ECPrivateKey from a SEC1 .der file data.
    This is equivalent to a PEM String that has had the —–BEGIN EC PRIVATE KEY—– header and footer stripped and been base64 encoded to ASN1 Data.

    Throws

    An ECError if the Data can’t be decoded or is not a valid key.

    Declaration

    Swift

    public init(sec1DER: Data) throws

    Parameters

    sec1DER

    The elliptic curve private key Data.

    Return Value

    An ECPrivateKey.

  • Initialize the ECPublicKeyfor this private key by extracting the public key bytes.

    Throws

    An ECError if the public key fails to be initialized from this private key.

    Declaration

    Swift

    public func extractPublicKey() throws -> ECPublicKey

    Return Value

    An ECPublicKey.

  • Make an new ECPrivate key from a supported EllipticCurve.

    Throws

    An ECError if the key fails to be created.

    Declaration

    Swift

    public static func make(for curve: EllipticCurve) throws -> ECPrivateKey

    Return Value

    An ECPrivateKey.