VirgilSDKPFS 1.2.1

VirgilSDKPFS 1.2.1

Sergey SeroshtanSanjoDeundiak 维护。



  • Oleksandr Deundiak

Virgil SWIFT PFS SDK

Build Status Carthage compatible GitHub license

简介 | SDK 功能 | 安装 | 初始化 | 聊天示例 | 注册用户 | 文档 | 支持

简介

Virgil 安全 为添加安全功能提供了适用于任何应用的 API。

Virgil PFS SDK 允许开发者迅速使用 Virgil PFS 服务 并将 完美前向安全 (PFS) 技术添加到其数字解决方案中,以保护之前拦截的流量,即使主私钥被破坏,也无法解密。

Virgil SWIFT PFS SDK 包含依赖的 Virgil SWIFT SDK 包。

SDK 功能

安装

Virgil SWIFT PFS SDK仅适用于客户端。

Virgil PFS 以软件包的形式提供。

COCOAPODS

CocoaPods 是一个用于 Cocoa 项目的依赖管理工具。您可以使用以下命令安装它

$ gem install cocoapods

要使用 CocoaPods 将 VirgilSDK PFS 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

target '<Your Target Name>' do
  use_frameworks!

  pod 'VirgilSDKPFS', '~> 1.2.1'
end

然后,运行以下命令

$ pod install

Carthage

Carthage 是一个去中心化的依赖管理工具,它构建您的依赖并提供二进制框架。您可以使用以下命令通过 Homebrew 安装 Carthage

$ brew update
$ brew install carthage

要使用 Carthage 将 VirgilSDK PFS 集成到您的 Xcode 项目中,请按照以下步骤执行

  • 在项目根目录下创建一个名为 Cartfile 的空文件,列出您项目中要使用的框架。
  • 将以下行添加到您的 Cartfile 中
github "VirgilSecurity/virgil-sdk-pfs-x" ~> 1.2.1
  • 运行 carthage update。这将把依赖项抓取到项目文件夹中的 Carthage/Checkouts 文件夹中,然后为每个依赖项进行编译或下载已编译的框架。
  • 在您的应用程序目标的“通用”设置选项卡中,在“链接框架和库”部分中,从您的项目文件夹中的 Carthage/Build 文件夹中添加您想要使用的每个框架。
  • 在您的应用程序目标的“构建阶段”设置选项卡中,点击“+”图标并选择“新建运行脚本阶段”。创建一个运行脚本,其中指定您的 shell(例如,/bin/sh),将以下内容添加到下面的脚本区域中 shell 之下
/usr/local/bin/carthage copy-frameworks

并将您想使用的框架的路径添加到“输入文件”下,“例如”

$(SRCROOT)/Carthage/Build/iOS/VSCCrypto.framework
$(SRCROOT)/Carthage/Build/iOS/VirgilCrypto.framework
$(SRCROOT)/Carthage/Build/iOS/VirgilSDK.framework
$(SRCROOT)/Carthage/Build/iOS/VirgilSDKPFS.framework

初始化

Virgil SWIFT PFS SDK仅适用于客户端。

请确保您已经在大纲 开发者仪表板 上注册并创建您的应用程序。

要初始化客户端的 SWIFT PFS SDK,您只需要在 仪表板 为客户端创建的 Access Token。访问令牌有助于对客户请求进行验证。

let virgil = VSSVirgilApi(token: "[YOUR_ACCESS_TOKEN_HERE]")

聊天示例

在初始化聊天之前,每个用户都必须在Virgil Card服务上拥有一个Virgil Card。如果您还没有Virgil Card,可以使用我们的指南轻松创建它。

要开始使用PFS技术进行沟通,每个用户都必须运行初始化

// initialize Virgil crypto instance
// enter User's credentials to create OTC and LTC Cards
let secureChatPreferences = SecureChatPreferences (
    crypto: "[CRYPTO]", // (e.g. VSSCrypto())
    identityPrivateKey: bobKey.privateKey,
    identityCard: bobCard.card!,
    accessToken: "[YOUR_ACCESS_TOKEN_HERE]")

// this class performs all PFS-technology logic: creates LTC and OTL Cards, publishes them, etc.
self.secureChat = SecureChat(preferences: secureChatPreferences)

try self.secureChat.initialize()

// the method is periodically called to:
// - check availability of user's OTC Cards on the service
// - add new Cards till their quantity reaches the number (100) noted in current method
self.secureChat.rotateKeys(desiredNumberOfCards: 100) { error in
    //...
}

然后发送者与接收者建立一个安全的PFS对话,加密并发送消息

func sendMessage(forReceiver receiver: User, message: String) {
    guard let session = self.chat.activeSession(
        withParticipantWithCardId: receiver.card.identifier) else {
        // start new session with recipient if session wasn't initialized yet
        self.chat.startNewSession(
            withRecipientWithCard: receiver.card) { session, error in

            guard error == nil, let session = session else {
                // Error handling
                return
            }

            // get an active session by recipient's Card ID
            self.sendMessage(forReceiver: receiver,
                usingSession: session, message: message)
        }
        return
    }

    self.sendMessage(forReceiver: receiver,
        usingSession: session, message: message)
}

func sendMessage(forReceiver receiver: User,
    usingSession session: SecureSession, message: String) {
    let ciphertext: String
    do {
        // encrypt the message using previously initialized session
        ciphertext = try session.encrypt(message)
    }
    catch {
        // Error handling
        return
    }

    // send a cipher message to recipient using your messaging service
    self.messenger.sendMessage(
        forReceiverWithName: receiver.name, text: ciphertext)
}

接收者使用刚刚创建的对话解密收到的消息

func messageReceived(fromSenderWithName senderName: String, message: String) {
    guard let sender = self.users.first(where: { $0.name == senderName }) else {
        // User not found
        return
    }

    self.receiveMessage(fromSender: sender, message: message)
}

func receiveMessage(fromSender sender: User, message: String) {
    do {
        let session = try self.chat.loadUpSession(
            withParticipantWithCard: sender.card, message: message)

        // decrypt message using established session
        let plaintext = try session.decrypt(message)

        // show a message to the user
        print(plaintext)
    }
    catch {
        // Error handling
    }
}

通过这个双向的开放会话,发送者和接收者可以继续使用PFS加密通信。

请参阅我们的用例,详细了解PFS加密通信的全过程。

注册用户

在Virgil中,每个用户都有一个私钥,用一张Virgil Card(身份卡)表示,其中包含公钥和用户的身份信息。

使用身份卡,我们生成具有自己生命周期的特殊卡

  • 一次性卡(OTC)
  • 长期卡(LTC)

对于每个会话,您都可以使用新的OTC,并在会话结束后删除它。

要创建用户的身份Virgil Card,请使用以下代码

// generate a new Virgil Key
let aliceKey = virgil.keys.generateKey()

// save the Virgil Key into storage
try! aliceKey.store(withName: @"[KEY_NAME]",
  password: @"[KEY_PASSWORD]")

// create identity for Alice
let aliceIdentity = virgil.identities.
  createUserIdentity(withValue: "alice", type: "name")

// create a Virgil Card
var aliceCard = try! virgil.cards.
  createCard(with: aliceIdentity, ownerKey:aliceKey)

// export a Virgil Card to string
let exportedCard = aliceCard.exportData()

// transmit the Virgil Card to the server and receive response
let cardData = TransmitToServer(exportedCard)

当Virgil Card创建时,在服务器端使用应用私钥对其进行签名和发布。

SWIFT不支持在Virgil服务上发布Virgil Card。我们建议使用以下指南中支持的任何一种语言。

文档

Virgil Security提供强大的API和文档,帮助您开始使用

要查找更多关于如何使用Virgil产品的示例,请参阅SWIFT SDK文档

许可证

此库是根据3-clause BSD许可证发布的。

支持

我们的开发者支持团队在此为您提供帮助。了解更多关于我们的帮助中心信息。

您可以在Twitter上找到我们,或者发送邮件至[email protected]

此外,您可以在Slack上获得我们支持团队的额外帮助。