CryptorRSA

@available(macOS 10.12, iOS 10.3, watchOS 3.3, tvOS 12.0, *)
public class CryptorRSA

RSA Encryption/Decryption, Signing/Verification

Class Functions

  • Create a plaintext data container.

    Declaration

    Swift

    public class func createPlaintext(with data: Data) -> PlaintextData

    Parameters

    data

    Data containing the key data.

    Return Value

    Newly initialized PlaintextData.

  • Creates a message from a plaintext string, with the specified encoding.

    Declaration

    Swift

    public class func createPlaintext(with string: String, using encoding: String.Encoding) throws -> PlaintextData

    Parameters

    string

    String value of the plaintext message

    encoding

    Encoding to use to generate the clear data

    Return Value

    Newly initialized PlaintextData.

  • Create an encrypted data container.

    Declaration

    Swift

    public class func createEncrypted(with data: Data) -> EncryptedData

    Parameters

    data

    Data containing the encrypted data.

    Return Value

    Newly initialized EncryptedData.

  • Creates a message with a encrypted base64-encoded string.

    Declaration

    Swift

    public class func createEncrypted(with base64String: String) throws -> EncryptedData

    Parameters

    base64String

    Base64-encoded data of an encrypted message

    Return Value

    Newly initialized EncryptedData.

  • Create an signed data container.

    Declaration

    Swift

    public class func createSigned(with data: Data) -> SignedData

    Parameters

    data

    Data containing the signed data.

    Return Value

    Newly initialized SignedData.

  • RSA Data Object: Allows for RSA Encryption/Decryption, Signing/Verification and various utility functions.

    See more

    Declaration

    Swift

    public class RSAData
  • Plaintext Data - Represents data not encrypted or signed.

    Declaration

    Swift

    public class PlaintextData : RSAData
  • Encrypted Data - Represents data encrypted.

    See more

    Declaration

    Swift

    public class EncryptedData : RSAData
  • Signed Data - Represents data that is signed.

    See more

    Declaration

    Swift

    public class SignedData : RSAData

Certificate Suffixes

  • X509 Certificate Extension

    Declaration

    Swift

    static let CER_SUFFIX: String
  • PEM Suffix

    Declaration

    Swift

    static let PEM_SUFFIX: String
  • DER Suffix

    Declaration

    Swift

    static let DER_SUFFIX: String

PEM Certificate Markers

  • PEM Begin Marker

    Declaration

    Swift

    static let PEM_BEGIN_MARKER: String
  • PEM End Marker

    Declaration

    Swift

    static let PEM_END_MARKER: String

Public Key Markers

  • PK Begin Marker

    Declaration

    Swift

    static let PK_BEGIN_MARKER: String
  • PK End Marker

    Declaration

    Swift

    static let PK_END_MARKER: String

Private Key Markers

  • SK Begin Marker

    Declaration

    Swift

    static let SK_BEGIN_MARKER: String
  • SK End Marker

    Declaration

    Swift

    static let SK_END_MARKER: String

Generic Key Markers

  • Generic Begin Marker

    Declaration

    Swift

    static let GENERIC_BEGIN_MARKER: String
  • Generic End Marker

    Declaration

    Swift

    static let GENERIC_END_MARKER: String

– Errors: Domain and Codes

– Error

  • RSA specific error structure.

    See more

    Declaration

    Swift

    public struct Error : Swift.Error, CustomStringConvertible

Type Aliases

– Public Key Creation

  • Creates a public key with DER data.

    Declaration

    Swift

    public class func createPublicKey(with data: Data) throws -> PublicKey

    Parameters

    data

    Key data

    Return Value

    New PublicKey instance.

  • Creates a public key by extracting it from a certificate.

    Declaration

    Swift

    public class func createPublicKey(extractingFrom data: Data) throws -> PublicKey

    Parameters

    data

    Data representing the certificate.

    Return Value

    New PublicKey instance.

  • Creates a key with a base64-encoded string.

    Declaration

    Swift

    public class func createPublicKey(withBase64 base64String: String) throws -> PublicKey

    Parameters

    base64String

    Base64-encoded key data

    Return Value

    New PublicKey instance.

  • Creates a key with a PEM string.

    Declaration

    Swift

    public class func createPublicKey(withPEM pemString: String) throws -> PublicKey

    Parameters

    pemString

    PEM-encoded key string

    Return Value

    New PublicKey instance.

  • Creates a key with a PEM file.

    Declaration

    Swift

    public class func createPublicKey(withPEMNamed pemName: String, onPath path: String) throws -> PublicKey

    Parameters

    pemName

    Name of the PEM file

    path

    Path where the file is located.

    Return Value

    New PublicKey instance.

  • Creates a key with a DER file.

    Declaration

    Swift

    public class func createPublicKey(withDERNamed derName: String, onPath path: String) throws -> PublicKey

    Parameters

    derName

    Name of the DER file

    path

    Path where the file is located.

    Return Value

    New PublicKey instance.

  • Creates a public key by extracting it from a certificate.

    Declaration

    Swift

    public class func createPublicKey(extractingFrom certName: String, onPath path: String) throws -> PublicKey

    Parameters

    certName

    Name of the certificate file.

    path

    Path where the file is located.

    Return Value

    New PublicKey instance.

  • Creates a key with a PEM file.

    Declaration

    Swift

    public class func createPublicKey(withPEMNamed pemName: String, in bundle: Bundle = Bundle.main) throws -> PublicKey

    Parameters

    pemName

    Name of the PEM file

    bundle

    Bundle in which to look for the PEM file. Defaults to the main bundle.

    Return Value

    New PublicKey instance.

  • Creates a key with a DER file.

    Declaration

    Swift

    public class func createPublicKey(withDERNamed derName: String, in bundle: Bundle = Bundle.main) throws -> PublicKey

    Parameters

    derName

    Name of the DER file

    bundle

    Bundle in which to look for the DER file. Defaults to the main bundle.

    Return Value

    New PublicKey instance.

  • Creates a public key by extracting it from a certificate.

    Declaration

    Swift

    public class func createPublicKey(extractingFrom certName: String, in bundle: Bundle = Bundle.main) throws -> PublicKey

    Parameters

    certName

    Name of the certificate file.

    bundle

    Bundle in which to look for the DER file. Defaults to the main bundle.

    Return Value

    New PublicKey instance.

– Private Key Creation

  • Creates a private key with data.

    Declaration

    Swift

    public class func createPrivateKey(with data: Data) throws -> PrivateKey

    Parameters

    data

    Key data

    Return Value

    New PrivateKey instance.

  • Creates a key with a base64-encoded string.

    Declaration

    Swift

    public class func createPrivateKey(withBase64 base64String: String) throws -> PrivateKey

    Parameters

    base64String

    Base64-encoded key data

    Return Value

    New PrivateKey instance.

  • Creates a key with a PEM string.

    Declaration

    Swift

    public class func createPrivateKey(withPEM pemString: String) throws -> PrivateKey

    Parameters

    pemString

    PEM-encoded key string

    Return Value

    New PrivateKey instance.

  • Creates a key with a PEM file.

    Declaration

    Swift

    public class func createPrivateKey(withPEMNamed pemName: String, onPath path: String) throws -> PrivateKey

    Parameters

    pemName

    Name of the PEM file

    path

    Path where the file is located.

    Return Value

    New PrivateKey instance.

  • Creates a key with a DER file.

    Declaration

    Swift

    public class func createPrivateKey(withDERNamed derName: String, onPath path: String) throws -> PrivateKey

    Parameters

    derName

    Name of the DER file

    path

    Path where the file is located.

    Return Value

    New PrivateKey instance.

  • Creates a key with a PEM file.

    Declaration

    Swift

    public class func createPrivateKey(withPEMNamed pemName: String, in bundle: Bundle = Bundle.main) throws -> PrivateKey

    Parameters

    pemName

    Name of the PEM file

    bundle

    Bundle in which to look for the PEM file. Defaults to the main bundle.

    Return Value

    New PrivateKey instance.

  • Creates a key with a DER file.

    Declaration

    Swift

    public class func createPrivateKey(withDERNamed derName: String, in bundle: Bundle = Bundle.main) throws -> PrivateKey

    Parameters

    derName

    Name of the DER file

    bundle

    Bundle in which to look for the DER file. Defaults to the main bundle.

    Return Value

    New PrivateKey instance.

  • Create a new RSA public/private key pair.

    Declaration

    Swift

    public class func makeKeyPair(_ keySize: RSAKey.KeySize) throws -> (PrivateKey, PublicKey)

    Parameters

    keySize

    The size of the generated RSA keys in bits.

    Return Value

    A tuple containing the (PrivateKey, PublicKey) instances.

  • RSA Key Creation and Handling

    See more

    Declaration

    Swift

    public class RSAKey
  • Public Key - Represents public key data.

    See more

    Declaration

    Swift

    public class PublicKey : RSAKey
  • Private Key - Represents private key data.

    See more

    Declaration

    Swift

    public class PrivateKey : RSAKey

– RSAUtilities

  • Create a key from key data.

  • Retrieve the OpenSSL error and text.

  • Create a key from key data.

    Declaration

    Swift

    static func createKey(from keyData: Data, type: CryptorRSA.RSAKey.KeyType) throws -> NativeKey

    Parameters

    keyData

    Data representation of the key.

    type

    Type of key data.

    Return Value

    SecKey representation of the key.

  • Convert DER data to PEM data.

    Declaration

    Swift

    static func convertDerToPem(from derData: Data, type: CryptorRSA.RSAKey.KeyType) -> String

    Parameters

    derData

    Data in DER format.

    type

    Type of key data.

    Return Value

    PEM Data representation.

  • Get the Base64 representation of a PEM encoded string after stripping off the PEM markers.

    Declaration

    Swift

    static func base64String(for pemString: String) throws -> String

    Parameters

    pemString

    String containing PEM formatted data.

    Return Value

    Base64 encoded String containing the data.

  • This function strips the x509 from a provided ASN.1 DER public key. If the key doesn’t contain a header, the DER data is returned as is.

    Declaration

    Swift

    static func stripX509CertificateHeader(for keyData: Data) throws -> Data

    Parameters

    keyData

    Data containing the public key with or without the x509 header.

    Return Value

    Data containing the public with header (if present) removed.

  • Add an X509 certificate header to key data.

    Declaration

    Swift

    static func addX509CertificateHeader(for keyData: Data) -> Data

    Parameters

    keyData

    Data to add the header to.

    Return Value

    The modified key data.