ソースを参照

Added init(rawRepresentation:) to the DERDecodable protocol and a default, public level, implementation.

Brandon Toms 3 年 前
コミット
53a21a61bb
1 ファイル変更40 行追加4 行削除
  1. 40 4
      Sources/CryptoSwift/PEM/DER.swift

+ 40 - 4
Sources/CryptoSwift/PEM/DER.swift

@@ -25,10 +25,44 @@ internal protocol DERDecodable {
   /// The keys secondary ASN1 object identifier (ex: for RSA Keys --> 'null' --> nil)
   static var secondaryObjectIdentifier: Array<UInt8>? { get }
 
-  /// Instantiates an instance of your Public Key when given a DER representation of your Public Key
+  /// Attempts to instantiate an instance of your Public Key when given a DER representation of your Public Key
   init(publicDER: Array<UInt8>) throws
-  /// Instantiates an instance of your Private Key when given a DER representation of your Private Key
+  /// Attempts to instantiate an instance of your Private Key when given a DER representation of your Private Key
   init(privateDER: Array<UInt8>) throws
+  
+  /// Attempts to instantiate a Key when given the ASN1 DER encoded external representation of the Key
+  ///
+  /// An example of importing a SecKey RSA key (from Apple's `Security` framework) for use within CryptoSwift
+  /// ```
+  /// /// Starting with a SecKey RSA Key
+  /// let rsaSecKey:SecKey
+  ///
+  /// /// Copy the External Representation
+  /// var externalRepError:Unmanaged<CFError>?
+  /// guard let externalRep = SecKeyCopyExternalRepresentation(rsaSecKey, &externalRepError) as? Data else {
+  ///     /// Failed to copy external representation for RSA SecKey
+  ///     return
+  /// }
+  ///
+  /// /// Instantiate the RSA Key from the raw external representation
+  /// let rsaKey = try RSA(rawRepresentation: externalRep)
+  ///
+  /// /// You now have a CryptoSwift RSA Key
+  /// // rsaKey.encrypt(...)
+  /// // rsaKey.decrypt(...)
+  /// // rsaKey.sign(...)
+  /// // rsaKey.verify(...)
+  /// ```
+  init(rawRepresentation: Data) throws
+}
+
+extension DERDecodable {
+  public init(rawRepresentation raw:Data) throws {
+    /// The default implementation that makes the original internal initializer publicly available
+    do { try self.init(privateDER: raw.bytes) } catch {
+      try self.init(publicDER: raw.bytes)
+    }
+  }
 }
 
 /// Conform to this protocol if your type can be described in an ASN1 DER representation
@@ -43,23 +77,25 @@ internal protocol DEREncodable {
   /// Returns the DER encoded representation of the Private Key
   func privateKeyDER() throws -> Array<UInt8>
 
-  /// A semantically similar function that mimics the functionality of Apple's `Security` framework's `SecKeyCopyExternalRepresentation` function
+  /// A semantically similar function that mimics the `SecKeyCopyExternalRepresentation` function from Apple's `Security` framework
   /// - Note: If called on a Private Key, this method will return the Private Keys DER Representation. Likewise, if called on a Public Key, this method will return the Public Keys DER Representation
   /// - Note: If you'd like to export the Public Keys DER from a Private Key, use the `publicKeyExternalRepresentation()` function
   func externalRepresentation() throws -> Data
-  /// A semantically similar function that mimics the functionality of Apple's `Security` framework's `SecKeyCopyExternalRepresentation` function
+  /// A semantically similar function that mimics the `SecKeyCopyExternalRepresentation` function from Apple's `Security` framework
   /// - Note: This function only ever exports the Public Key's DER representation. If called on a Private Key, the corresponding Public Key will be extracted and exported.
   func publicKeyExternalRepresentation() throws -> Data
 }
 
 extension DEREncodable {
   public func externalRepresentation() throws -> Data {
+    // The default implementation that makes the original internal function publicly available
     do { return try Data(self.privateKeyDER()) } catch {
       return try Data(self.publicKeyDER())
     }
   }
 
   public func publicKeyExternalRepresentation() throws -> Data {
+    // The default implementation that makes the original internal function publicly available
     try Data(self.publicKeyDER())
   }
 }