Swiftpack.co - VirgilSecurity/virgil-crypto-x as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by VirgilSecurity.
VirgilSecurity/virgil-crypto-x foundation-2.0.0
Virgil Crypto stack Objective-C/Swift
⭐️ 29
🕓 2 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/VirgilSecurity/virgil-crypto-x.git", from: "foundation-2.0.0")

Virgil Crypto Library Objective-C/Swift

Build Status CocoaPods Compatible Carthage compatible Platform GitHub license

Introduction | Library purposes | Installation | Usage examples | Docs | License | Contacts


Virgil Crypto Library Objective-C/Swift is a small, flexible and convenient wrapper for a variety of crypto algorithms. It can be used in a small microcontroller as well as in a high load server application. Also, it provides a bunch of custom hybrid algorithms that combine different crypto algorithms to solve common complex cryptographic problems in an easy way. That eliminates a requirement for developers to have strong cryptographic skills.

Virgil Security Objective-C/Swift Crypto Library uses Swift wrapper Virgil Security Crypto Library Wrapper over C library Virgil Security Crypto Library.

Library purposes

  • Asymmetric Key Generation
  • Encryption/Decryption of data and streams
  • Generation/Verification of digital signatures
  • Double Ratchet algorithm support
  • Post-quantum algorithms support: Round5 (encryption) and Falcon (signature)
  • Crypto for using Virgil Core SDK


VirgilCrypto is provided as a set of frameworks. These frameworks are distributed via Carthage and CocoaPods.

All frameworks are available for:

  • iOS 9.0+
  • macOS 10.10+
  • tvOS 9.0+
  • watchOS 2.0+


CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate VirgilCrypto into your Xcode project using CocoaPods, specify it in your Podfile:

target '<Your Target Name>' do

  pod 'VirgilCrypto', '~> 6.0.0'

Then, run the following command:

$ pod install


Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate VirgilCrypto into your Xcode project using Carthage, create an empty file with name Cartfile in your project's root folder and add following lines to your Cartfile

github "VirgilSecurity/virgil-crypto-x" ~> 6.0.0

Linking against prebuilt binaries

To link prebuilt frameworks to your app, run following command:

$ carthage update --use-xcframeworks

This will build each dependency or download a pre-compiled framework from github Releases.

Building for iOS/tvOS/watchOS

On your application target's “General” settings tab, in the “Linked Frameworks and Libraries” section, add following frameworks from the Carthage/Build folder inside your project's folder:

  • VirgilCrypto
  • VirgilCryptoFoundation
  • VSCCommon
  • VSCFoundation

Check Embed & sign for each.

Building for macOS

On your application target's “General” settings tab, in the “Embedded Binaries” section, drag and drop following frameworks from the Carthage/Build folder on disk:

  • VirgilCrypto
  • VirgilCryptoFoundation
  • VSCCommon
  • VSCFoundation

Additionally, you'll need to copy debug symbols for debugging and crash reporting on macOS.

On your application target’s “Build Phases” settings tab, click the “+” icon and choose “New Copy Files Phase”. Click the “Destination” drop-down menu and select “Products Directory”. For each framework, drag and drop the corresponding dSYM file.

Swift Package Manager

Swift Package Manager is an official Apple tool for managing the distribution of Swift code.

The Apple documentation can be used to add frameworks to an Xcode project.

Usage examples

Generate a key pair

Generate a private key using the default algorithm (EC_X25519):

import VirgilCrypto

let crypto = try! VirgilCrypto()
let keyPair = try! crypto.generateKeyPair()

Generate and verify a signature

Generate signature and sign data with a private key:

import VirgilCrypto

let crypto = try! VirgilCrypto()

// prepare a message
let messageToSign = "Hello, Bob!"
let dataToSign = messageToSign.data(using: .utf8)!

// generate a signature
let signature = try! crypto.generateSignature(of: dataToSign, using: senderPrivateKey)

Verify a signature with a public key:

import VirgilCrypto

let crypto = try! VirgilCrypto()

// verify a signature
let verified = try! crypto.verifySignature(signature, of: dataToSign, with: senderPublicKey)

Encrypt and decrypt data

Encrypt data with a public key:

import VirgilCrypto

let crypto = try! VirgilCrypto()

// prepare a message
let messageToEncrypt = "Hello, Bob!"
let dataToEncrypt = messageToEncrypt.data(using: .utf8)!

// encrypt the message
let encryptedData = try! crypto.encrypt(dataToEncrypt, for: [receiverPublicKey])

Decrypt the encrypted data with a Private Key:

import VirgilCrypto

let crypto = try! VirgilCrypto()

// prepare data to be decrypted
let decryptedData = try! crypto.decrypt(encryptedData, with: receiverPrivateKey)

// decrypt the encrypted data using a private key
let decryptedMessage = String(data: decryptedData, encoding: .utf8)!

Import and export keys

Export keys:

import VirgilCrypto

// generate a Key Pair
let crypto = VirgilCrypto()
let keyPair = try! crypto.generateKeyPair()

// export a Private key
let privateKeyData = try! crypto.exportPrivateKey(keyPair.privateKey, password: "YOUR_PASSWORD")
let privateKeyStr = privateKeyData.base64EncodedString()

// export a Public key
let publicKeyData = crypto.exportPublicKey(keyPair.publicKey)
let publicKeyStr = publicKeyData.base64EncodedString()

Import keys:

import VirgilCrypto

let crypto = VirgilCrypto()


let privateKeyData = Data(base64Encoded: privateKeyStr)!

// import a Private key
let privateKey = try! crypto.importPrivateKey(from: privateKeyData, password: "YOUR_PASSWORD")


let publicKeyStr = "MCowBQYDK2VwAyEA9IVUzsQENtRVzhzraTiEZZy7YLq5LDQOXGQG/q0t0kE="

let publicKeyData = Data(base64Encoded: publicKeyStr)!

// import a Public key
let publicKey = try! crypto.importPublicKey(from: publicKeyData)



This library is released under the 3-clause BSD License.


Our developer support team is here to help you.

You can find us on Twitter or send us email support@VirgilSecurity.com.

Also, get extra help from our support team on Slack.


Stars: 29
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

2 weeks ago

Added SPM support

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics