Swift JOSE

Swift JOSE 是一个 Swift 编程语言的软件包,它提供了对 JSON 对象签名和加密 (JOSE) 系列规范的实现。

概述

注意 与上述规范中的签名相关的代码仍在开发中。 欢迎提交 PR。

支持的 Swift 版本

此库支持 Swift 5.7 或更高版本,并将支持最新的稳定 Swift 版本以及之前的两个版本。

入门

要使用 Swift JOSE,请将以下依赖项添加到您的 Package.swift

dependencies: [
    .package(url: "https://github.com/proxyco/swift-jose.git", upToNextMinor(from: "0.1.0"))
]

请注意,此存储库尚未有 1.0 标签,因此 API 尚不稳定。

然后,将特定的 product dependency 添加到你的 target 中

dependencies: [
    .product(name: "JOSE", package: "swift-jose"),
]

软件包依赖项

Swift JOSE 具有以下软件包依赖项

用法

JWE 示例

// Generate recipient key pair
let recipientPrivateKey = Curve25519.KeyAgreement.PrivateKey()
let recipientPublicKey = recipientPrivateKey.publicKey

// Encrypt plaintext using JWE
let plaintext = "Hello, World!".data(using: .utf8)!
let serialization = try JWE.encrypt(
    plaintext: plaintext,
    to: recipientPublicKey.jwkRepresentation,
    protectedHeader: .init(
        algorithm: .ecdhESA256KW,
        encryptionAlgorithm: .a256GCM,
        compressionAlgorithm: .deflate
    )
)
// Sender sends JWE serialization to recipient...
// ...

// Decrypt ciphertext
let receivedPlaintext = try JWE.decrypt(
    serialization: serialization,
    using: recipientPrivateKey.jwkRepresentation
)

有关如何使用 Swift JOSE 的更多信息,请参阅文档