### Signing Input Example Source: https://github.com/airsidemobile/joseswift/wiki/JWS Example signing input for the header and payload described above. ```swift eyAiYWxnIjogIlJTNTEyIiB9.VHJ1bXBldHMgb2YgTWV4aWNvIPCfj5w ``` -------------------------------- ### JWE Example with Header and Payload Source: https://github.com/airsidemobile/joseswift/wiki/JWE Example header and payload used to generate the base64url-encoded parts and final compact serialization. ```swift // Header { "alg": "RSA1_5", "enc": "A256CBC-HS512" } // Payload "Trumpets of Mexico 🏜" ``` -------------------------------- ### Compact Serialization Example Source: https://github.com/airsidemobile/joseswift/wiki/JWS Given the header and payload, the base64url-encoded values and the resulting compact serialization. ```swift // Header { "alg": "RS512" } // Payload "Trumpets of Mexico 🏜" ``` ```swift // base64url(header) "eyAiYWxnIjogIlJTNTEyIiB9" // base64url(payload) "VHJ1bXBldHMgb2YgTWV4aWNvIPCfj5w" // base64url(signature) "TwJS6...YvlTQ" ``` ```swift """ eyAiYWxnIjogIlJTNTEyIiB9.VHJ1bXBldHMgb2YgTWV4aWNvIPCfj5w.TwJS6...YvlTQ """ ``` -------------------------------- ### JWS Payload Example Source: https://github.com/airsidemobile/joseswift/wiki/JWS A UTF-8 encoded string as an example payload. ```swift "Trumpets of Mexico 🏜" ``` -------------------------------- ### JWE Header Example Source: https://github.com/airsidemobile/joseswift/wiki/JWE Example of a JWE header specifying the algorithms RSA1_5 for key encryption and A256CBC-HS512 for content encryption. ```swift { "alg": "RSA1_5", "enc": "A256CBC-HS512" } ``` -------------------------------- ### Set, get, and remove public/private JWE header parameters Source: https://github.com/airsidemobile/joseswift/blob/master/README.md Public and private header parameters can be set and read with set(_:forParameter:), get(parameter:), and remove(parameter:). The example sets ```swift try header.set("rice", forParameter: "meal") let meal = header.get(parameter: "meal") // "rice" header.remove(parameter: "meal") ``` -------------------------------- ### Get compact serialization Source: https://github.com/airsidemobile/joseswift/wiki/JWE Shows how to get the compact serialized string or data from a JWE. ```swift jwe.compactSerializedString // or jwe.compactSerializedData ``` -------------------------------- ### JWK Set Example Source: https://github.com/airsidemobile/joseswift/wiki/JWK Example of a JWK Set, a JSON object with a 'keys' member containing an array of JWKs. ```javascript { "keys": [ { "kty":"RSA", "n": "0vx7a...qDKgw", "e":"AQAB", "alg":"RS256", "kid":"2011-04-29" }, { "kty":"RSA", "n": "agoeb...pJzKn", "e":"AQAB" } ] } ``` -------------------------------- ### JWS Header Example Source: https://github.com/airsidemobile/joseswift/wiki/JWS A JSON object specifying the RS512 algorithm for the JWS signature. ```swift { "alg": "RS512" } ``` -------------------------------- ### Get compact serialization Source: https://github.com/airsidemobile/joseswift/wiki/JWS Shows how to obtain the compact serialized string or data from a JWS. ```swift jws.compactSerializedString // or jws.compactSerializedData ``` -------------------------------- ### RSA Public Key JWK Example Source: https://github.com/airsidemobile/joseswift/wiki/JWK Example of a JSON Web Key representing an RSA public key, with fields kty, n, e, alg, and kid. ```javascript { "kty":"RSA", "n": "0vx7a...qDKgw", "e":"AQAB", "alg":"RS256", "kid":"2011-04-29" } ``` -------------------------------- ### Getting JWK JSON string from RSAPublicKey Source: https://github.com/airsidemobile/joseswift/wiki/JWK Shows how to get the JWK JSON string representation. ```swift let json = jwk.jsonString()! // "{"kty": "RSA", "n": "MHZ4L...uS2d3", "e": "QVFBQg"}" ``` -------------------------------- ### Getting JWK JSON data from RSAPublicKey Source: https://github.com/airsidemobile/joseswift/wiki/JWK Shows how to get the JWK JSON data representation. ```swift let json = jwk.jsonData()! ``` -------------------------------- ### CocoaPods Podfile configuration for JOSESwift Source: https://github.com/airsidemobile/joseswift/blob/master/README.md Add this Podfile entry to integrate JOSESwift version 3.0 into your Xcode project. Requires iOS 13.0 or later and uses the CocoaPods CDN source. Run `pod install` after adding it. ```ruby source 'https://cdn.cocoapods.org/' platform :ios, '13.0' use_frameworks! target '' do pod 'JOSESwift', '~> 3.0' end ``` -------------------------------- ### Decrypt with private key Source: https://github.com/airsidemobile/joseswift/wiki/JWE Example of decrypting a JWE using a private key. The key type must match the underlying crypto implementation (SecKey on iOS). ```swift let privateKey: SecKey = /* ... */ do { let payload = try jwe.decrypt(with: privateKey) } catch { // Something went wrong. } ``` -------------------------------- ### Base64url-Encoded JWE Parts Source: https://github.com/airsidemobile/joseswift/wiki/JWE The base64url-encoded values for the header, encrypted key, initialization vector, ciphertext, and authentication tag from the example. ```swift // base64url(header) "eyJhbGciOiJSU0ExXzUiLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIn0" // base64url(encrypted content encryption key) "b5Zbb...NglvQ" // base64url(initialization vector) "3vgaWb4EQbk_W1PHiBZBlg" // base64url(ciphertext) "5Hjsko9pPFpmkaNDLBYj9fUZp52FGVIpJo-jdAut3wk" // base64url(authentication tag) "hZXvBYxZEV4x7ACGhx3ky5pzNuf1X6UsmI3iEcYjHmM" ``` -------------------------------- ### Extract payload from JWS Source: https://github.com/airsidemobile/joseswift/wiki/JWS After validation, extract the payload data and convert it to a string. The example prints the message 'Trumpets of Mexico 🏜'. ```swift let payload = jws.payload let data = payload.data() let message = String(data: data, encoding: .utf8)! print(message) // "Trumpets of Mexico 🏜" ``` -------------------------------- ### Initialize JWE with do-catch Source: https://github.com/airsidemobile/joseswift/wiki/JWE Shows how to create a JWE and handle errors. ```swift do { let jwe = try JWE(header: header, payload: payload, encrypter: signer) } catch { // Encryption went wrong. } ``` -------------------------------- ### Initialize JWS with error handling Source: https://github.com/airsidemobile/joseswift/wiki/JWS Shows how to construct a JWS and handle signing errors. ```swift do { let jws = try JWS(header: header, payload: payload, signer: signer) } catch { // Signing went wrong. } ``` -------------------------------- ### init(compactSerialization:) Source: https://github.com/airsidemobile/joseswift/wiki/JWS Initializes a JWS instance from a compact serialization string or data. Throws an error if the serialization is invalid. ```APIDOC ## init(compactSerialization:) ### Description Constructs a JWS instance from an existing compact serialization. Throws an error if the serialization is invalid. ### Method Initializer ### Endpoint N/A (Swift initializer) ### Parameters - **compactSerialization** (String or Data) - Required - The compact serialization of the JWS. ### Request Example ```swift do { let jws = try JWS(compactSerialization: serialization) } catch { // Something went wrong. } ``` ### Response #### Success Response - **jws** (JWS) - The initialized JWS instance. #### Error Handling Throws an error if the serialization is invalid. ``` -------------------------------- ### Create Encrypter with guard Source: https://github.com/airsidemobile/joseswift/wiki/JWE Shows how to create an Encrypter with a SecKey and handle the nil case for wrong key type. ```swift let publicKey: SecKey = /* ... */ guard let encrypter = Encrypter(keyEncryptionAlgorithm: .RSA1_5, keyEncryptionKey: publicKey, contentEncyptionAlgorithm: .A256CBCHS512) else { // Wrong key type. } // You have a valid encrypter. ``` -------------------------------- ### Instantiate JWEHeader with algorithms Source: https://github.com/airsidemobile/joseswift/wiki/JWE Creates a JWEHeader with RSA1_5 and A256CBCHS512 algorithms. ```swift let header = JWEHeader(algorithm: .RSA1_5, encryptionAlgorithm: .A256CBCHS512) ``` -------------------------------- ### JWEHeader initializer for encryption algorithms Source: https://github.com/airsidemobile/joseswift/wiki/JWE Shows the initializer that takes the asymmetric key algorithm and symmetric encryption algorithm. ```swift init(algorithm: AsymmetricKeyAlgorithm, encryptionAlgorithm: SymmetricKeyAlgorithm) ``` -------------------------------- ### Instantiating RSAPublicKey from SecKey Source: https://github.com/airsidemobile/joseswift/wiki/JWK Shows how to create an RSAPublicKey from a SecKey instance. ```swift let publicKey: SecKey = /* ... */ let jwk = try! RSAPublicKey(publicKey: publicKey) ``` -------------------------------- ### Create a Signer with guard Source: https://github.com/airsidemobile/joseswift/wiki/JWS Shows how to safely create a Signer and handle the case of a wrong key type. ```swift let privateKey: SecKey = /* ... */ guard let signer = Signer(signatureAlgorithm: .RS512, key: privateKey) else { // Wrong key type. } // You have a valid signer. ``` -------------------------------- ### JWSHeader initializer for algorithm Source: https://github.com/airsidemobile/joseswift/wiki/JWS Shows the initializer that lets you provide the algorithm parameter. ```swift init(algorithm: SignatureAlgorithm) ``` -------------------------------- ### RSAPublicKey Initializer (from Existing RSA Key) Source: https://github.com/airsidemobile/joseswift/wiki/JWK Initializes an RSAPublicKey from an existing RSA public key that conforms to ExpressibleAsRSAPublicKeyComponents. Optionally accepts additional JWK parameters. ```APIDOC ## RSAPublicKey Initializer (from Existing RSA Key) ### Description Initializes an RSAPublicKey from an existing RSA public key that conforms to ExpressibleAsRSAPublicKeyComponents. Optionally accepts additional JWK parameters. ### Method Initializer ### Signature ```swift init(publicKey: ExpressibleAsRSAPublicKeyComponents, additionalParameters parameters: [String: String] = [:]) throws ``` ### Parameters - **publicKey** (ExpressibleAsRSAPublicKeyComponents) - The existing RSA public key. Must conform to ExpressibleAsRSAPublicKeyComponents. - **additionalParameters** ([String: String]) - Optional additional JWK parameters. Default is empty. ### Usage Example ```swift let publicKey: SecKey = /* ... */ let jwk = try! RSAPublicKey(publicKey: publicKey) ``` ### Related Protocol ```swift public protocol ExpressibleAsRSAPublicKeyComponents { // Constructs an instance of its type from the given components. static func representing(rsaPublicKeyComponents components: RSAPublicKeyComponents) throws -> Self // Converts the key to modulus and exponent. func rsaPublicKeyComponents() throws -> RSAPublicKeyComponents } ``` ``` -------------------------------- ### Converting RSAPublicKey to Data Source: https://github.com/airsidemobile/joseswift/wiki/JWK Shows how to convert a JWK to Data in PKCS#1 format. ```swift // Data will be in PKCS#1 format. let publicKey: Data = try! jwk.converted(to: Data.self) ``` -------------------------------- ### Set additional header parameters Source: https://github.com/airsidemobile/joseswift/wiki/JWS Shows how to set optional parameters like kid and typ on the header. ```swift header.kid = "2018-10-08" header.typ = "JWS" ``` -------------------------------- ### Signer initializer Source: https://github.com/airsidemobile/joseswift/wiki/JWS Shows the initializer for creating a Signer with a signature algorithm and key. ```swift init?(signatureAlgorithm: SignatureAlgorithm, key: KeyType) ``` -------------------------------- ### Encrypter initializer Source: https://github.com/airsidemobile/joseswift/wiki/JWE Shows the initializer for Encrypter that takes key encryption algorithm, key encryption key, and content encryption algorithm. ```swift init(keyEncryptionAlgorithm: AsymmetricKeyAlgorithm, keyEncryptionKey kek: KeyType, contentEncyptionAlgorithm: SymmetricKeyAlgorithm) ``` -------------------------------- ### Initializer for RSAPublicKey from existing key Source: https://github.com/airsidemobile/joseswift/wiki/JWK Initializer that accepts an existing public key conforming to ExpressibleAsRSAPublicKeyComponents and optional additional parameters. ```swift init(publicKey: ExpressibleAsRSAPublicKeyComponents, additionalParameters parameters: [String: String] = [:]) throws ``` -------------------------------- ### Resolve build tools with swift package resolve Source: https://github.com/airsidemobile/joseswift/blob/master/BuildTools/README.md Run this command in the BuildTools directory to resolve the build tools as specified by the Package.swift file. ```bash swift package resolve ``` -------------------------------- ### JWE initializer Source: https://github.com/airsidemobile/joseswift/wiki/JWE Shows the throwing initializer for JWE that takes header, payload, and encrypter. ```swift init(header: JWEHeader, payload: Payload, encrypter: Encrypter) throws ``` -------------------------------- ### Initialize JWE from compact serialization Source: https://github.com/airsidemobile/joseswift/wiki/JWE Use one of these initializers to create a JWE instance from a compact serialization string or data. Both throw errors on invalid input. ```swift init(compactSerialization: String) throws // or init(compactSerialization: Data) throws ``` -------------------------------- ### Initialize JWS from compact serialization Source: https://github.com/airsidemobile/joseswift/wiki/JWS Use these initializers to create a JWS instance from a compact serialization string or data. Check for errors during initialization. ```swift init(compactSerialization: String) throws // or init(compactSerialization: Data) throws ``` ```swift do { let jws = try JWS(compactSerialization: serialization) } catch { // Something went wrong. } ``` -------------------------------- ### Create JWEHeader with RSA1_5 and A256CBCHS512 Source: https://github.com/airsidemobile/joseswift/blob/master/README.md Initializes a JWE header with the RSA1_5 key management algorithm and A256CBCHS512 content encryption algorithm. Required before setting header parameters. ```swift var header = JWEHeader(keyManagementAlgorithm: .RSA1_5, contentEncryptionAlgorithm: .A256CBCHS512) ``` -------------------------------- ### JWE Usage Source: https://github.com/airsidemobile/joseswift/wiki/JWE The source describes how to construct JWE objects, initialize them, and serialize them to compact serialization. ```APIDOC ## JWE (struct) ### Description An immutable struct representing a JSON Web Encryption object, containing the header, encrypted key, initialization vector, ciphertext and authentication tag. ## init(header:payload:encrypter:) ### Description Constructs a new JWE from a header, payloadasi dan encrypter. Throws an error if encryption fails. ### Method Initializer (Swift) ### Endpoint JWE(header: payload: encrypter:) ### Parameters - header: JWEHeader – Required – The header containing algorithm parameters. - payload: Payload – Required – The data to be encrypted. - encrypter: Encrypter – Required – The encrypter that handles cryptographic operations. ### Request Example ```swift do { let jwe = try JWE(header: header, payload: payload, encrypter: signer) } catch { // Encryption went wrong. } ``` ### Response Returns a JWE instance or throws an error. ## Properties: compactSerializedString and compactSerializedData ### Description Gets the compact serialization of the JWE as a string or data. The serialization is a compact string representation of the JWE. ### Method Property (Swift) ### Endpoint jwe.compactSerializedString or jwe.compactSerializedData ### Parameters None ### Response Example ```swift jwe.compactSerializedString // or jwe.compactSerializedData ``` ``` -------------------------------- ### JWS initializer Source: https://github.com/airsidemobile/joseswift/wiki/JWS Shows the throwing initializer for constructing a JWS from header, payload, and signer. ```swift init(header: JWSHeader, payload: Payload, signer: Signer) throws ``` -------------------------------- ### Instantiate JWSHeader with RS512 Source: https://github.com/airsidemobile/joseswift/wiki/JWS Creates a JWSHeader with the RS512 signing algorithm. ```swift let header = JWSHeader(algorithm: .RS512) ``` -------------------------------- ### Update build tools with swift package update Source: https://github.com/airsidemobile/joseswift/blob/master/BuildTools/README.md Run this command in the BuildTools directory to update the build tools to the latest version matching the Package.swift file. ```bash swift package update ``` -------------------------------- ### Converting RSAPublicKey to SecKey Source: https://github.com/airsidemobile/joseswift/wiki/JWK Shows how to convert a JWK to a SecKey instance. ```swift let publicKey: SecKey = try! jwk.converted(to: SecKey.self) ``` -------------------------------- ### Set additional JWEHeader parameters Source: https://github.com/airsidemobile/joseswift/wiki/JWE Shows setting the kid and typ parameters on the header. ```swift header.kid = "2018-10-08" header.typ = "JWE" ``` -------------------------------- ### RSAPublicKey Initializer (from JWK Data) Source: https://github.com/airsidemobile/joseswift/wiki/JWK Initializes an RSAPublicKey from JSON data representing a JWK. ```APIDOC ## RSAPublicKey Initializer (from JWK Data) ### Description Initializes an RSAPublicKey from JSON data representing a JWK. ### Method Initializer ### Signature ```swift init(data: Data) throws ``` ### Parameters - **data** (Data) - JSON data representing a JWK. ### Usage Example ```swift let json: Data = /* ... */ let jwk = try! RSAPublicKey(data: json) ``` ### Codable Support ```swift let jwk = try JSONDecoder().decode(RSAPublicKey.self, from: json) ``` ``` -------------------------------- ### Create JWE Encrypter with Matching Algorithms Source: https://github.com/airsidemobile/joseswift/blob/master/README.md The encrypter algorithms must match the header algorithms. This snippet shows the creation of an encrypter using a public key. ```swift let publicKey: SecKey = ... ``` -------------------------------- ### Payload initializer Source: https://github.com/airsidemobile/joseswift/wiki/JWE Shows the initializer for Payload that takes Data. ```swift init(_ payload: Data) ``` -------------------------------- ### Shortcut: validate and extract payload in one step Source: https://github.com/airsidemobile/joseswift/wiki/JWS A pro tip that combines signature validation and payload extraction using optional binding. If validation fails, the guard exits. ```swift guard let payload = try? jws.validate(with: publicKey).payload else { // Something went wrong! } let message = String(data: payload.data(), encoding: .utf8)! print(message) // "Trumpets of Mexico 🏜" ``` -------------------------------- ### Instantiate Payload with data Source: https://github.com/airsidemobile/joseswift/wiki/JWE Creates a Payload from a UTF-8 encoded string. ```swift let message = "Trumpets of Mexico 🏜" let data = message.data(using: .utf8)! let payload = Payload(data) ``` -------------------------------- ### Initializer for RSAPublicKey from JWK data Source: https://github.com/airsidemobile/joseswift/wiki/JWK Initializer that accepts JSON data representing a JWK. ```swift init(data: Data) throws ``` -------------------------------- ### Parsing JWK data into RSAPublicKey Source: https://github.com/airsidemobile/joseswift/wiki/JWK Shows how to create an RSAPublicKey from JWK JSON data. ```swift let json: Data = /* ... */ let jwk = try! RSAPublicKey(data: json) ``` -------------------------------- ### Handle initialization errors Source: https://github.com/airsidemobile/joseswift/wiki/JWE Wrap the JWE initializer in a do-catch block to handle any errors that occur during initialization. ```swift do { let jwe = try JWE(compactSerialization: serialization) } catch { // Something went wrong. } ``` -------------------------------- ### Add JOSESwift dependency to Cartfile with Carthage Source: https://github.com/airsidemobile/joseswift/blob/master/README.md Add this line to your Cartfile to integrate JOSESwift via Carthage. Then run `carthage update` and drag the built framework into your Xcode project. ```plaintext github "airsidemobile/JOSESwift" ~> 3.0 ``` -------------------------------- ### JWEHeader struct definition Source: https://github.com/airsidemobile/joseswift/wiki/JWE Shows the JWEHeader struct holding a dictionary of parameters. ```swift struct JWEHeader { let parameters: [String: Any] } ``` -------------------------------- ### Final Compact Serialization Source: https://github.com/airsidemobile/joseswift/wiki/JWE The resulting compact serialization of the JWE, with line breaks added for readability. ```swift """ eyJhbGciOiJSU0ExXzUiLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIn0. b5Zbb...NglvQ. 3vgaWb4EQbk_W1PHiBZBlg. 5Hjsko9pPFpmkaNDLBYj9fUZp52FGVIpJo-jdAut3wk. hZXvBYxZEV4x7ACGhx3ky5pzNuf1X6UsmI3iEcYjHmM """ ``` -------------------------------- ### Encoding RSAPublicKey with JSONEncoder Source: https://github.com/airsidemobile/joseswift/wiki/JWK Shows how to encode a JWK using Codable. ```swift let json = try! JSONEncoder().encode(jwk) ``` -------------------------------- ### Decoding RSAPublicKey with JSONDecoder Source: https://github.com/airsidemobile/joseswift/wiki/JWK Shows how to decode a JWK using Codable. ```swift let jwk = try JSONDecoder().decode(RSAPublicKey.self, from: json) ``` -------------------------------- ### JWS Construction and Serialization Source: https://github.com/airsidemobile/joseswift/wiki/JWS This snippet documents the JWS construction and serialization APIs in JOSESwift. ```APIDOC ## JWSHeader(algorithm:) ### Description Initializes a JWSHeader with the specified signing algorithm. ### Method Initializer ### Parameters - **algorithm** (SignatureAlgorithm) - Required - The signing algorithm to be used. ### Request Example ```swift let header = JWSHeader(algorithm: .RS512) ``` ### Response Returns a JWSHeader instance. ``` ```APIDOC ## JWSHeader.parameters ### Description A dictionary of header parameters. ### Method Property ### Response Returns a dictionary of header parameters. ``` ```APIDOC ## JWSHeader.kid ### Description Sets the key ID parameter of the JWS header. ### Method Property ### Parameters - **kid** (String?) - Optional - The key ID. ### Request Example ```swift header.kid = "2018-10-08" ``` ``` ```APIDOC ## JWSHeader.typ ### Description Sets the type parameter of the JWS header. ### Method Property ### Parameters - **typ** (String?) - Optional - The type of the JWS. ### Request Example ```swift header.typ = "JWS" ``` ``` ```APIDOC ## Payload(_:) ### Description Initializes a Payload with the given data. ### Method Initializer ### Parameters - **payload** (Data) - Required - The data to be carried by the JWS. ### Request Example ```swift let payload = Payload(data) ``` ### Response Returns a Payload instance. ``` ```APIDOC ## Signer(signatureAlgorithm:key:) ### Description Initializes a Signer with the specified signing algorithm and private key. ### Method Initializer ### Parameters - **signatureAlgorithm** (SignatureAlgorithm) - Required - The signing algorithm to use. - **key** (KeyType) - Required - The private key for signing. ### Request Example ```swift let signer = Signer(signatureAlgorithm: .RS512, key: privateKey) ``` ### Response Returns an optional Signer instance; nil if the key type is incorrect. ``` ```APIDOC ## JWS(header:payload:signer:) ### Description Initializes a JWS with the given header, payload, and signer. ### Method Initializer ### Parameters - **header** (JWSHeader) - Required - The JWS header. - **payload** (Payload) - Required - The payload. - **signer** (Signer) - Required - The signer used to compute the signature. ### Request Example ```swift let jws = try JWS(header: header, payload: payload, signer: signer) ``` ### Response Returns a JWS instance. ``` ```APIDOC ## JWS.compactSerializedString ### Description Gets the compact serialization of the JWS as a string. ### Method Property ### Response Returns a String containing the compact serialization. ``` ```APIDOC ## JWS.compactSerializedData ### Description Gets the compact serialization of the JWS as data. ### Method Property ### Response Returns a Data object containing the compact serialization. ``` -------------------------------- ### Compact Serialization Format Source: https://github.com/airsidemobile/joseswift/wiki/JWE Shows the concatenation of base64url-encoded parts in compact serialization: header, encrypted key, initialization vector, ciphertext, and authentication tag. ```swift base64url(header) + "." + base64url(encrypted content encryption key) + "." + base64url(initialization vector) + "." + base64url(ciphertext) + "." + base64url(authentication tag) + "." ``` -------------------------------- ### ExpressibleAsRSAPublicKeyComponents protocol Source: https://github.com/airsidemobile/joseswift/wiki/JWK Protocol for types that can be represented as RSA public key components. ```swift public protocol ExpressibleAsRSAPublicKeyComponents { // Constucts an instance of it's type from the given components. static func representing(rsaPublicKeyComponents components: RSAPublicKeyComponents) throws -> Self // Converts the key to modulus and exponent. func rsaPublicKeyComponents() throws -> RSAPublicKeyComponents } ``` -------------------------------- ### Accessing JWK parameters via subscript and typed getters Source: https://github.com/airsidemobile/joseswift/wiki/JWK Shows how to access JWK parameters using the subscript and typed getters, with type differences. ```swift let publicKey: RSAPublicKey = /* ... */ publicKey["use"] // "sig" publicKey.use // "sig" type(of: publicKey["use"]) // Optional.Type type(of: publicKey.use) // Optional.Type ``` -------------------------------- ### JWSHeader struct definition Source: https://github.com/airsidemobile/joseswift/wiki/JWS Shows the JWSHeader struct holding a dictionary of parameters. ```swift struct JWSHeader { let parameters: [String: Any] } ``` -------------------------------- ### validate(with:) Source: https://github.com/airsidemobile/joseswift/wiki/JWS Verifies the JWS signature and returns the JWS instance if valid. Throws an error if the signature is invalid or if an error occurs during validation. ```APIDOC ## validate(with:) ### Description Verifies the JWS signature using the provided public key. Returns the JWS instance if the signature is valid. Throws an error if the signature is invalid or if an error occurs during validation. ### Method Function ### Endpoint N/A (Swift method) ### Parameters - **publicKey** (KeyType) - Required - The public key used for signature verification. The KeyType depends on the underlying crypto implementation (e.g., SecKey on iOS). ### Request Example ```swift let publicKey: SecKey = /* ... */ do { _ = try jws.validate(with: publicKey) } catch SwiftJOSEError.signatureInvalid { // The signature is invalid! } catch { // An error occurred during validation! } ``` ### Response #### Success Response - **JWS** - The validated JWS instance. #### Error Handling Throws `SwiftJOSEError.signatureInvalid` if the signature is invalid, or another error if validation fails. ``` -------------------------------- ### JWK protocol definition Source: https://github.com/airsidemobile/joseswift/wiki/JWK Defines the JWK protocol with keyType, parameters, subscript access, initializer, and JSON conversion methods. ```swift protocol JWK: Codable { var keyType: JWKKeyType { get } var parameters: [String: JWKParameterType] { get } subscript(parameter: String) -> Any? { get } init(data: Data) throws func jsonString() -> String? func jsonData() -> Data? } ``` -------------------------------- ### RSAPublicKey Conversion Source: https://github.com/airsidemobile/joseswift/wiki/JWK Converts an RSAPublicKey to any type that conforms to ExpressibleAsRSAPublicKeyComponents, such as SecKey or Data. ```APIDOC ## RSAPublicKey Conversion ### Description Converts an RSAPublicKey to any type that conforms to ExpressibleAsRSAPublicKeyComponents, such as SecKey or Data. ### Method Instance Method ### Signature ```swift func converted(to type: T.Type) throws -> T ``` ### Parameters - **type** (T.Type) - The target type that conforms to ExpressibleAsRSAPublicKeyComponents. ### Usage Examples ```swift let publicKey: SecKey = try! jwk.converted(to: SecKey.self) ``` ```swift // Data will be in PKCS#1 format. let publicKey: Data = try! jwk.converted(to: Data.self) ``` ``` -------------------------------- ### JWK Protocol Source: https://github.com/airsidemobile/joseswift/wiki/JWK The JWK protocol defines the requirements for JSON Web Key representation in JOSESwift. It includes properties for key type and parameters, a subscript for parameter access, and methods for initialization and serialization. ```APIDOC ## JWK Protocol ### Description The JWK protocol defines the requirements for JSON Web Key representation in JOSESwift. It includes properties for key type and parameters, a subscript for parameter access, and methods for initialization and serialization. ### Protocol Definition ```swift protocol JWK: Codable { var keyType: JWKKeyType { get } var parameters: [String: JWKParameterType] { get } subscript(parameter: String) -> Any? { get } init(data: Data) throws func jsonString() -> String? func jsonData() -> Data? } ``` ### Properties - **keyType** (JWKKeyType) - The key type (e.g., RSA). - **parameters** ([String: JWKParameterType]) - Dictionary of JWK parameters. ### Subscript - **subscript(parameter: String) -> Any?** - Directly access a parameter from the parameters dictionary. ### Methods - **init(data: Data) throws** - Initializes a JWK from JSON data. - **func jsonString() -> String?** - Returns the JWK as a JSON string. - **func jsonData() -> Data?** - Returns the JWK as JSON data. ### Usage Example ```swift let publicKey: RSAPublicKey = /* ... */ publicKey["use"] // "sig" publicKey.use // "sig" type(of: publicKey["use"]) // Optional.Type type(of: publicKey.use) // Optional.Type ``` ``` -------------------------------- ### Validate JWS signature with validate(with:) Source: https://github.com/airsidemobile/joseswift/wiki/JWS Option 2 for signature validation. Throws an error that can be caught to differentiate between an invalid signature and a general error. ```swift let publicKey: SecKey = /* ... */ do { _ = try jws.validate(with: publicKey) } catch SwiftJOSEError.signatureInvalid { // The signature is invalid! } catch { // An error occurred during validation! } // The signature is valid! ``` -------------------------------- ### Create JWE Payload from UTF-8 Data Source: https://github.com/airsidemobile/joseswift/blob/master/README.md Builds the payload for a JWE from a UTF-8 encoded message. The payload wraps the message data so it can be encrypted by the encrypter. ```swift let message = "Summer ⛱, Sun ☀️, Cactus 🌵".data(using: .utf8)! let payload = Payload(message) ``` -------------------------------- ### JWE struct definition Source: https://github.com/airsidemobile/joseswift/wiki/JWE Shows the immutable JWE struct with its properties: header, encryptedKey, initializationVector, ciphertext, and authenticationTag. ```swift struct JWE { let header: JWEHeader let encryptedKey: Data let initializationVector: Data let ciphertext: Data let authenticationTag: Data } ``` -------------------------------- ### Signing Input Concatenation Source: https://github.com/airsidemobile/joseswift/wiki/JWS The concatenation that forms the signing input for the JWS signature. ```swift ascii(base64URL(utf8(JWS Header)) + "." + base64url(payload)) ``` -------------------------------- ### RSAPublicKey struct definition Source: https://github.com/airsidemobile/joseswift/wiki/JWK Defines the RSAPublicKey struct conforming to JWK. ```swift struct RSAPublicKey: JWK { let keyType: JWKKeyType let parameters: [String: JWKParameterType] let modulus: String let exponent: String ``` -------------------------------- ### RSAPrivateKey struct definition Source: https://github.com/airsidemobile/joseswift/wiki/JWK Defines the RSAPrivateKey struct conforming to JWK. ```swift struct RSAPrivateKey: JWK { let keyType: JWKKeyType let parameters: [String: JWKParameterType] let modulus: String let exponent: String let privateExponent: String ``` -------------------------------- ### Compact Serialization Concatenation Source: https://github.com/airsidemobile/joseswift/wiki/JWS The concatenation that represents a JWS in compact serialization. ```swift base64url(header) + "." + base64url(payload) + "." + base64url(signature) ``` -------------------------------- ### JWS struct definition Source: https://github.com/airsidemobile/joseswift/wiki/JWS Shows the immutable JWS struct with header, payload, and signature properties. ```swift struct JWS { let header: JWSHeader let payload: Payload let signature: Data } ``` -------------------------------- ### Decrypt JWE ciphertext Source: https://github.com/airsidemobile/joseswift/wiki/JWE Use the decrypt function with the appropriate key type (e.g., SecKey on iOS). The decryption fails if the wrong key type is specified. ```swift func decrypt(with kdk: KeyType) throws -> Payload ``` -------------------------------- ### payload Source: https://github.com/airsidemobile/joseswift/wiki/JWS Extracts the payload from the JWS instance. The payload is a Payload object that can be converted to Data and then to a string. ```APIDOC ## payload ### Description Provides access to the payload of the JWS. The payload can be converted to Data using `data()` and then to a string. ### Method Property ### Endpoint N/A (Swift property) ### Parameters None ### Request Example ```swift let payload = jws.payload let data = payload.data() let message = String(data: data, encoding: .utf8)! print(message) // "Trumpets of Mexico 🏜" ``` ### Response #### Success Response - **Payload** - The payload object containing the JWS payload data. ``` -------------------------------- ### Validate JWS signature with isValid(for:) Source: https://github.com/airsidemobile/joseswift/wiki/JWS Option 1 for signature validation. Returns a boolean indicating if the signature is valid. Does not provide details on why validation failed. ```swift // Option 1 func isValid(for publicKey: KeyType) -> Bool // and // Option 2 func validate(with publicKey: KeyType) throws -> JWS ``` ```swift let publicKey: SecKey = /* ... */ guard jws.isValid(for: publicKey) else { // Either an error occurred or the signature is invalid. } // The signature is valid! ``` -------------------------------- ### isValid(for:) Source: https://github.com/airsidemobile/joseswift/wiki/JWS Checks whether the JWS signature is valid for the given public key. Returns a boolean indicating validity without providing error details. ```APIDOC ## isValid(for:) ### Description Verifies the JWS signature using the provided public key. Returns a boolean indicating whether the signature is valid. Does not provide details on why validation failed. ### Method Function ### Endpoint N/A (Swift method) ### Parameters - **publicKey** (KeyType) - Required - The public key used for signature verification. The KeyType depends on the underlying crypto implementation (e.g., SecKey on iOS). ### Request Example ```swift let publicKey: SecKey = /* ... */ if jws.isValid(for: publicKey) { // Signature is valid } else { // Signature is invalid or an error occurred } ``` ### Response #### Success Response - **Bool** - `true` if the signature is valid, `false` otherwise. ``` -------------------------------- ### Access decrypted payload data Source: https://github.com/airsidemobile/joseswift/wiki/JWE Convert the decrypted payload to data and then to a string. The decrypted payload is not stored in the JWE, so you must store it yourself or decrypt again when needed. ```swift let data = payload.data() let message = String(data: data, encoding: .utf8)! print(message) // "Trumpets of Mexico 🏜" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.