Lines 15-133text
16#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
24 Represents an elliptic curve private key.
29 You can generate an elliptic curve Key using OpenSSL:
30 https://wiki.openssl.org/index.php/Command_Line_Elliptic_Curve_Operations#Generating_EC_Keys_and_Parameters
35 -----BEGIN EC PRIVATE KEY-----
CriticalCritical Secret
Package contains a critical-looking secret pattern.
ios/ECPrivateKey.swiftView on unpkg · L35 CriticalSecret Pattern
EC private key in ios/ECPrivateKey.swift
ios/ECPrivateKey.swiftView on unpkg · L35 36 MHcCAQEEIJX+87WJ7Gh19sohyZnhxZeXYNOcuGv4Q+8MLge4UkaZoAoGCCqGSM49
37 AwEHoUQDQgAEikc5m6C2xtDWeeAeT18WElO37zvFOz8p4kAlhvgIHN23XIClNESg
38 KVmLgSSq2asqiwdrU5YHbcHFkgdABM1SPA==
39 -----END EC PRIVATE KEY-----
41 let privateKey = try ECPrivateKey(key: pemKey)
42 let signature = "Hello world".sign(with: privateKey)
45@available(macOS 10.13, iOS 11, watchOS 4.0, tvOS 11.0, *)
46public class ECPrivateKey {
47 /// A String description of the curve this key was generated from.
48 public let curveId: String
50 /// The `EllipticCurve` this key was generated from.
51 public let curve: EllipticCurve
53 /// The private key represented as a PEM String.
54 public let pemString: String
57 typealias NativeKey = OpaquePointer?
58 deinit { EC_KEY_free(.make(optional: self.nativeKey)) }
60 typealias NativeKey = SecKey
62 public var nativeKey: SecKey?
64 private var stripped: Bool = false
68 Initialize an ECPrivateKey from a PEM String.
69 This can either be from a `.p8` file with the header "-----BEGIN PRIVATE KEY-----",
CriticalSecret Pattern
RSA private key in ios/ECPrivateKey.swift
ios/ECPrivateKey.swiftView on unpkg · L69 70 or from a `.pem` file with the header "-----BEGIN EC PRIVATE KEY-----".
CriticalSecret Pattern
EC private key in ios/ECPrivateKey.swift
ios/ECPrivateKey.swiftView on unpkg · L70 71 ### Usage Example: ###
73 let privateKeyString = """
74 -----BEGIN EC PRIVATE KEY-----
CriticalSecret Pattern
EC private key in ios/ECPrivateKey.swift
ios/ECPrivateKey.swiftView on unpkg · L74 75 MHcCAQEEIJX+87WJ7Gh19sohyZnhxZeXYNOcuGv4Q+8MLge4UkaZoAoGCCqGSM49
76 AwEHoUQDQgAEikc5m6C2xtDWeeAeT18WElO37zvFOz8p4kAlhvgIHN23XIClNESg
77 KVmLgSSq2asqiwdrU5YHbcHFkgdABM1SPA==
78 -----END EC PRIVATE KEY-----
80 let key = try ECPrivateKey(key: privateKeyString)
82 - Parameter key: The elliptic curve private key as a PEM string.
83 - Returns: An ECPrivateKey.
84 - Throws: An ECError if the PEM string can't be decoded or is not a valid key.
86 public convenience init(key: String) throws {
87 // Strip whitespace characters
88 let strippedKey = String(key.filter { !" \n\t\r".contains($0) })
89 var pemComponents = strippedKey.components(separatedBy: "-----")
90 guard pemComponents.count >= 5 else {
91 throw ECError.invalidPEMString
93 // Remove any EC parameters since Curve is determined by OID
94 if pemComponents[1] == "BEGINECPARAMETERS" {
95 pemComponents.removeFirst(5)
96 guard pemComponents.count >= 5 else {
97 throw ECError.invalidPEMString
100 guard let der = Data(base64Encoded: pemComponents[2]) else {
101 throw ECError.failedBase64Encoding
103 if pemComponents[1] == "BEGINECPRIVATEKEY" {
104 try self.init(sec1DER: der)
105 } else if pemComponents[1] == "BEGINPRIVATEKEY" {
106 try self.init(pkcs8DER: der)
108 throw ECError.unknownPEMHeader
112 /// Initialize an ECPrivateKey from a PKCS8 `.der` file data.
113 /// This is equivalent to a PEM String that has had the "-----BEGIN PRIVATE KEY-----"
114 /// header and footer stripped and been base64 encoded to ASN1 Data.
115 /// - Parameter pkcs8DER: The elliptic curve private key Data.
116 /// - Returns: An ECPrivateKey.
117 /// - Throws: An ECError if the Data can't be decoded or is not a valid key.
118 public init(pkcs8DER: Data) throws {
119 let (result, _) = ASN1.toASN1Element(data: pkcs8DER)
120 guard case let ASN1.ASN1Element.seq(elements: es) = result,
122 case let ASN1.ASN1Element.seq(elements: ids) = es[1],
124 case let ASN1.ASN1Element.bytes(data: privateKeyID) = ids[1]
126 throw ECError.failedASN1Decoding
128 self.curve = try EllipticCurve.objectToCurve(ObjectIdentifier: privateKeyID)
129 guard case let ASN1.ASN1Element.bytes(data: privateOctest) = es[2] else {
130 throw ECError.failedASN1Decoding
132 let (octest, _) = ASN1.toASN1Element(data: privateOctest)
133 guard case let ASN1.ASN1Element.seq(elements: seq) = octest,