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
RSAErrorif an error occurs.Declaration
Swift
public static func generateKeyPair(withSize keySize: Int) throws -> (publicKey: SecKey, privateKey: SecKey)Parameters
keySizeThis value defines key size in bits.
Return Value
A tuple with public and private key.
-
Encrypts data.
Throws
An
RSAErrorif an error occurs.Declaration
Swift
static public func encrypt(data: Data, using publicKey: SecKey, padding: SecPadding = .PKCS1) throws -> DataParameters
dataThe data to be encrypted.
publicKeyThe public key with which to encrypt the data.
paddingThe type of padding to use. Default is PKCS1.
Return Value
The encrypted data.
-
Decrypts data.
Throws
An
RSAErrorif an error occurs.Declaration
Swift
static public func decrypt(data: Data, using privateKey: SecKey, padding: SecPadding = .PKCS1) throws -> DataParameters
dataThe data to be decrypted.
privateKeyThe private key with which to decrypt the data.
paddingThe 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
RSAErrorif an error occurs.Declaration
Swift
public static func sign(_ data: Data, using privateKey: SecKey, digestAlgorithm: SecPadding) throws -> DataParameters
dataThe data to be signed.
privateKeyThe private key with which to sign the data.
digestAlgorithmThe 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
RSAErrorif an error occurs.Declaration
Swift
public static func verify(_ data: Data, using publicKey: SecKey, digestAlgorithm: SecPadding, signature: Data) throws -> BoolParameters
dataThe data for which the signature is being verified
publicKeyThe public key with which to verify the data.
digestAlgorithmThe digest algorithm, which is used to produce a data digest for verifying. Available values: PKCS1SHA1, PKCS1SHA224, PKCS1SHA256, PKCS1SHA384 or PKCS1SHA512.
signatureThe digital signature to be verified.
Return Value
Result of data verification.
View on GitHub
RSA Class Reference