ECPrivateKey
public class ECPrivateKey
Represents an elliptic curve private key.
Supported curves are:
- prime256v1
- secp384r1
- NID_secp521r1
You can generate an elliptic curve Key using OpenSSL:
https://wiki.openssl.org/index.php/Command_Line_Elliptic_Curve_Operations#Generating_EC_Keys_and_Parameters
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
EllipticCurvethis 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
.p8file with the header—–BEGIN PRIVATE KEY—–
, or from a.pemfile 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) throwsParameters
keyThe elliptic curve private key as a PEM string.
Return Value
An ECPrivateKey.
-
Initialize an ECPrivateKey from a PKCS8
.derfile 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) throwsParameters
pkcs8DERThe elliptic curve private key Data.
Return Value
An ECPrivateKey.
-
Initialize an ECPrivateKey from a SEC1
.derfile 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) throwsParameters
sec1DERThe 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 -> ECPublicKeyReturn 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 -> ECPrivateKeyReturn Value
An ECPrivateKey.
ECPrivateKey Class Reference