Encrypt your data like using other Swift APIs
SwiftlyCrypto is based on SwCrypt from @soyersoyer
SwiftlyCrypto is a non-abstract crypto library. Developers can benefit from SwiftlyCrypto's figurative API.
SwiftlyCrypto will never define a type as its original value. (Like Data
or String
.) This feature can be very useful in large projects, because it makes the code more readable.
SwiftlyCrypto is repackaged from SwCryt. Thanks @soyersoyer's project SwCrypt!
You should import SwiftlyCrypto to your code at the beginning.
import SwiftlyCrypto
let (privateKey, publicKey) = RSA.generateKeyPair()
let pemPrivateKey = privateKey.toPKCS1()
let pemPublicKey = publicKey.toPKCS8()
let encryptedPrivateKey = try privateKey.encryptedKey("password")
let decryptedPrivateKey = try RSAPrivateKey(encryptedPrivateKey, password: "password")
let publicKey = try privateKey.getPublicKey()
let text = "Hello, world!"
let raw = RSARawValue(text)
let encrypted = try raw.encrypt(publicKey: publicKey)
let decrypted = try encrypted.decrypt(privateKey: privateKey)
let text = "Hello, world!"
let raw = RSARawValue(text)
let signed = try raw.sign(privateKey: privateKey)
let verifyResult = try signed.verify(message: raw, publicKey: publicKey) // true for success, otherwise false
let randomKey = AES.generateRandomKey()
let randomIV = AES.generateRandomIV()
let key = AESKey("password")
let iv = try AESIV(fromHexString: "12345678901234567890123456789012")
let text = "Hello, world!"
let raw = AESRawValue(text)
let encrypted = try raw.encrypt(key: key, iv: iv)
let decrypted = try encrypted.decrypt(key: key, iv: iv)
// generate both key and iv
let raw = AESRawValue("Hello, world!")
let key = AESKey()
let iv = AESIV()
let encrypted = try raw.encrypt(randomKey: &key, randomIV: &iv)
// generate key
let raw = AESRawValue("Hello, world!")
let key = AESKey()
let iv = try AESIV(fromHexString: "12345678901234567890123456789012")
let encrypted = try raw.encrypt(randomKey: &key, iv: iv)
// generate iv
let raw = AESRawValue("Hello, world!")
let key = AESKey("password")
let iv = AESIV()
let encrypted = try raw.encrypt(key: key, randomIV: &iv)
// true for empty, otherwise false
key.isEmpty
iv.isEmpty