Swift JOSE is a package for the Swift programming language that provides implementation for the JSON Object Signing and Encryption (JOSE) family of specifications.
Note The code related to signatures from the above specifications is still a work in progress. PRs are welcome.
This library supports Swift 5.7 or later and will support the latest stable Swift version and the two versions prior.
To use Swift JOSE, add the following dependency to your Package.swift:
dependencies: [
.package(url: "https://github.com/proxyco/swift-jose.git", upToNextMinor(from: "0.1.0"))
]
Note that this repository does not have a 1.0 tag yet, so the API is not stable.
Then, add the specific product dependency to your target:
dependencies: [
.product(name: "JOSE", package: "swift-jose"),
]
Swift JOSE has the following package dependencies:
X448
support.secp256k1
support.AES_CBC_HMAC_SHA2
, PBES2
, and RSA DER encoding support.// 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
)
For more information on how to use Swift JOSE, please refer to the documentation.