ECPublicKey
@available(macOS 10.13, iOS 11, watchOS 4.0, tvOS 11.0, *)
public class ECPublicKey
Represents an elliptic curve public 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 PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEikc5m6C2xtDWeeAeT18WElO37zvF
Oz8p4kAlhvgIHN23XIClNESgKVmLgSSq2asqiwdrU5YHbcHFkgdABM1SPA==
-----END PUBLIC KEY-----
"""
let publicKey = try ECPublicKey(key: pemKey)
let base64Sig = "MEYCIQCvgBLn+tQoBDBR3D2G3485GloYGNxuk6PqR4qjr5GDqAIhAKNvsqvesVBD/MLub/KAyzLLNGtUZyQDxYZj/4vmHwWF"
let signature = try ECSignature(asn1: Data(base64Encoded: base64Sig))
let verified = signature.verify(plaintext: "Hello world", using: publicKey)
-
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 public key represented as a PEM String.
Declaration
Swift
public let pemString: String
-
Initialize an ECPublicKey from a
.pem
file format.Usage Example:
let publicKeyString = """ -----BEGIN PUBLIC KEY----- MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEikc5m6C2xtDWeeAeT18WElO37zvF Oz8p4kAlhvgIHN23XIClNESgKVmLgSSq2asqiwdrU5YHbcHFkgdABM1SPA== -----END PUBLIC KEY----- """ let pemKey = try ECPublicKey(key: publicKeyString)
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 public key as a PEM string.
Return Value
An ECPublicKey.
-
Initialize an ECPublicKey from
.der
file data.
This is equivalent to a PEM String that has had the “—–BEGIN PUBLIC 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(der: Data) throws
Parameters
der
The elliptic curve public key Data.
Return Value
An ECPublicKey.