DoubleRatchet
Swift 中的 Double Ratchet 协议实现。加密操作完全由 libsodium 提供。
安装
SPM
.package(url: "https://github.com/TICESoftware/DoubleRatchet.git", .upToNextMajor(from: "1.0.0"))
为了构建库,需要链接 libsodium。官方仓库中包含了为特定平台构建二进制文件的自定义脚本。
swift build -Xcc -I[头部搜索路径] -Xlinker -L[二进制路径]
在使用 Xcode 时,您可以通过手动设置包含 libsodium 标头文件的头部搜索路径来链接静态的 libsodium 库。
CodoaPods
pod 'DoubleRatchet'
这使用 Sodium
作为依赖,该依赖包含预编译的 libsodium 库。无需进一步配置。
使用
Alice和Bob通过安全通道计算共享密钥。之后,一旦某一方知道另一方的公钥,她就可以开始对话。
import DoubleRatchet
let sharedSecret: Bytes = ...
let info = "DoubleRatchetExample"
let bob = try DoubleRatchet(keyPair: nil, remotePublicKey: nil, sharedSecret: sharedSecret, maxSkip: 20, maxCache: 20, info: info)
// Bob sends his public key to Alice using another channel
// sendToAlice(bob.publicKey)
let alice = try DoubleRatchet(keyPair: nil, remotePublicKey: bob.publicKey, sharedSecret: sharedSecret, maxSkip: 20, maxCache: 20, info: info)
// Now the conversation begins
let message = "Hello, Bob!".bytes
let encryptedMessage = try alice.encrypt(plaintext: message)
let decryptedMessage = try bob.decrypt(message: encryptedMessage)
print(decryptedMessage.utf8String!) // Hello, Bob!