RSA

public class RSA

The RSA class provides a set of tools for working with RSA cryptographic algorithm.

  • Generates RSA keys with specified key size.

    Throws

    An RSAError if an error occurs.

    Declaration

    Swift

    public static func generateKeyPair(withSize keySize: Int) throws -> (publicKey: SecKey, privateKey: SecKey)

    Parameters

    keySize

    This value defines key size in bits.

    Return Value

    A tuple with public and private key.

  • Encrypts data.

    Throws

    An RSAError if an error occurs.

    Declaration

    Swift

    static public func encrypt(data: Data, using publicKey: SecKey, padding: SecPadding = .PKCS1) throws -> Data

    Parameters

    data

    The data to be encrypted.

    publicKey

    The public key with which to encrypt the data.

    padding

    The type of padding to use. Default is PKCS1.

    Return Value

    The encrypted data.

  • Decrypts data.

    Throws

    An RSAError if an error occurs.

    Declaration

    Swift

    static public func decrypt(data: Data, using privateKey: SecKey, padding: SecPadding = .PKCS1) throws -> Data

    Parameters

    data

    The data to be decrypted.

    privateKey

    The private key with which to decrypt the data.

    padding

    The type of padding to use. Default is PKCS1.

    Return Value

    The encrypted data.

  • Signs a data (digest) using private key and returns a digital signature. The data will be hashed using specified digest algorithm and then signed with private key.

    Throws

    An RSAError if an error occurs.

    Declaration

    Swift

    public static func sign(_ data: Data, using privateKey: SecKey, digestAlgorithm: SecPadding) throws -> Data

    Parameters

    data

    The data to be signed.

    privateKey

    The private key with which to sign the data.

    digestAlgorithm

    The digest algorithm. Available values: PKCS1SHA1, PKCS1SHA224, PKCS1SHA256, PKCS1SHA384 or PKCS1SHA512.

    Return Value

    The digital signature of data.

  • Verifies a data (digest) using public key and digital signature. The data will be hashed using specified digest algorithm, and then digest will be verified with public key and signature.

    Throws

    An RSAError if an error occurs.

    Declaration

    Swift

    public static func verify(_ data: Data, using publicKey: SecKey, digestAlgorithm: SecPadding, signature: Data) throws -> Bool

    Parameters

    data

    The data for which the signature is being verified

    publicKey

    The public key with which to verify the data.

    digestAlgorithm

    The digest algorithm, which is used to produce a data digest for verifying. Available values: PKCS1SHA1, PKCS1SHA224, PKCS1SHA256, PKCS1SHA384 or PKCS1SHA512.

    signature

    The digital signature to be verified.

    Return Value

    Result of data verification.