Swift JWE is a Swift package that provides a convenient way to encrypt and decrypt data using JSON Web Encryption (JWE) RFC7516.
The package supports the following key management algorithms:
ECDH-ES
ECDH-ES+A128KW
ECDH-ES+A192KW
ECDH-ES+A256KW
ECDH-1PU
draft-madden-jose-ecdh-1pu-04The package supports the following content encryption algorithms:
A128GCM
A192GCM
A256GCM
The package supports the following elliptic curves:
P-256
P-384
P-521
X25519
X448
secp256k1
RFC8812 Section 3.1To use Swift JWE, follow these simple steps:
JWE
instance with the appropriate protected header.encrypt
method to encrypt the plaintext using the recipient's public key.decrypt
method to decrypt the ciphertext using the recipient's private key.Here's an example:
// Generate recipient key pair
let recipientPrivateKey = Curve25519.KeyAgreement.PrivateKey()
let recipientPublicKey = recipientPrivateKey.publicKey
// Encrypt plaintext using JWE
let plaintext = "Hello, World!".data(using: .utf8)!
var jwe = JWE(
protectedHeader: .init(
alg: .ecdhESA256KW,
enc: .a256GCM,
zip: .deflate
)
)
try jwe.encrypt(
plaintext: plaintext,
to: recipientPublicKey.jwkRepresentation
)
let compactSerialization = jwe.compactSerialization
// Sender sends JWE compact serialization to recipient...
// ...
// Decrypt ciphertext
let receivedJWE = try JWE(compactSerialization: compactSerialization)
let receivedPlaintext = try receivedJWE.decrypt(
using: recipientPrivateKey.jwkRepresentation
)
For more information on how to use Swift JWE, please refer to the documentation.
link |
Stars: 0 |
Last commit: 2 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics