Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) JWS Using RSASSA-PSS using SHA-256 and MGF1 with SHA-256Creates and validates a JSON Web Signature (JWS) using RSASSA-PSS using SHA-256 and MGF1 with SHA-256. Chilkat supports all of the following JWS algorithms: +--------------+-------------------------------+--------------------+ | "alg" Param | Digital Signature or MAC | Implementation | | Value | Algorithm | Requirements | +--------------+-------------------------------+--------------------+ | HS256 | HMAC using SHA-256 | Required | | HS384 | HMAC using SHA-384 | Optional | | HS512 | HMAC using SHA-512 | Optional | | RS256 | RSASSA-PKCS1-v1_5 using | Recommended | | | SHA-256 | | | RS384 | RSASSA-PKCS1-v1_5 using | Optional | | | SHA-384 | | | RS512 | RSASSA-PKCS1-v1_5 using | Optional | | | SHA-512 | | | ES256 | ECDSA using P-256 and SHA-256 | Recommended+ | | ES384 | ECDSA using P-384 and SHA-384 | Optional | | ES512 | ECDSA using P-521 and SHA-512 | Optional | | PS256 | RSASSA-PSS using SHA-256 and | Optional | | | MGF1 with SHA-256 | | | PS384 | RSASSA-PSS using SHA-384 and | Optional | | | MGF1 with SHA-384 | | | PS512 | RSASSA-PSS using SHA-512 and | Optional | | | MGF1 with SHA-512 | | +--------------+-------------------------------+--------------------+ Note: This example requires Chilkat v9.5.0.66 or greater.
Use ChilkatAx-win32.pkg Procedure Test Handle hoSbJwk Boolean iSuccess Variant vRsaKey Handle hoRsaKey Boolean iSuccess ProtHdr Handle hoJwsProtHdr Handle hoJws Integer iSignatureIndex Boolean iBIncludeBom String sPayloadStr String sJwsCompact Handle hoJws2 Variant vRsaPubKey Handle hoRsaPubKey Integer v Variant vJoseHeader Handle hoJoseHeader String sTemp1 Boolean bTemp1 // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // Note: This example requires Chilkat v9.5.0.66 or greater. // Use the following RSA key loaded from JWK format. Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJwk If (Not(IsComObjectCreated(hoSbJwk))) Begin Send CreateComObject of hoSbJwk End Get ComAppend Of hoSbJwk '{"kty":"RSA",' To iSuccess Get ComAppend Of hoSbJwk '"n":"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx' To iSuccess Get ComAppend Of hoSbJwk "HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs" To iSuccess Get ComAppend Of hoSbJwk "D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH" To iSuccess Get ComAppend Of hoSbJwk "SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV" To iSuccess Get ComAppend Of hoSbJwk "MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8" To iSuccess Get ComAppend Of hoSbJwk 'NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ",' To iSuccess Get ComAppend Of hoSbJwk '"e":"AQAB",' To iSuccess Get ComAppend Of hoSbJwk '"d":"Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97I' To iSuccess Get ComAppend Of hoSbJwk "jlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0" To iSuccess Get ComAppend Of hoSbJwk "BkTGoVEn2bKVRUCgu-GjBVaYLU6f3l9kJfFNS3E0QbVdxzubSu3Mkqzjkn" To iSuccess Get ComAppend Of hoSbJwk "439X0M_V51gfpRLI9JYanrC4D4qAdGcopV_0ZHHzQlBjudU2QvXt4ehNYT" To iSuccess Get ComAppend Of hoSbJwk "CBr6XCLQUShb1juUO1ZdiYoFaFQT5Tw8bGUl_x_jTj3ccPDVZFD9pIuhLh" To iSuccess Get ComAppend Of hoSbJwk 'BOneufuBiB4cS98l2SR_RQyGWSeWjnczT0QU91p1DhOVRuOopznQ",' To iSuccess Get ComAppend Of hoSbJwk '"p":"4BzEEOtIpmVdVEZNCqS7baC4crd0pqnRH_5IB3jw3bcxGn6QLvnEtfdUdi' To iSuccess Get ComAppend Of hoSbJwk "YrqBdss1l58BQ3KhooKeQTa9AB0Hw_Py5PJdTJNPY8cQn7ouZ2KKDcmnPG" To iSuccess Get ComAppend Of hoSbJwk 'BY5t7yLc1QlQ5xHdwW1VhvKn-nXqhJTBgIPgtldC-KDV5z-y2XDwGUc",' To iSuccess Get ComAppend Of hoSbJwk '"q":"uQPEfgmVtjL0Uyyx88GZFF1fOunH3-7cepKmtH4pxhtCoHqpWmT8YAmZxa' To iSuccess Get ComAppend Of hoSbJwk "ewHgHAjLYsp1ZSe7zFYHj7C6ul7TjeLQeZD_YwD66t62wDmpe_HlB-TnBA" To iSuccess Get ComAppend Of hoSbJwk '-njbglfIsRLtXlnDzQkv5dTltRJ11BKBBypeeF6689rjcJIDEz9RWdc",' To iSuccess Get ComAppend Of hoSbJwk '"dp":"BwKfV3Akq5_MFZDFZCnW-wzl-CCo83WoZvnLQwCTeDv8uzluRSnm71I3Q' To iSuccess Get ComAppend Of hoSbJwk "CLdhrqE2e9YkxvuxdBfpT_PI7Yz-FOKnu1R6HsJeDCjn12Sk3vmAktV2zb" To iSuccess Get ComAppend Of hoSbJwk '34MCdy7cpdTh_YVr7tss2u6vneTwrA86rZtu5Mbr1C1XsmvkxHQAdYo0",' To iSuccess Get ComAppend Of hoSbJwk '"dq":"h_96-mK1R_7glhsum81dZxjTnYynPbZpHziZjeeHcXYsXaaMwkOlODsWa' To iSuccess Get ComAppend Of hoSbJwk "7I9xXDoRwbKgB719rrmI2oKr6N3Do9U0ajaHF-NKJnwgjMd2w9cjz3_-ky" To iSuccess Get ComAppend Of hoSbJwk 'NlxAr2v4IKhGNpmM5iIgOS1VZnOZ68m6_pbLBSp3nssTdlqvd0tIiTHU",' To iSuccess Get ComAppend Of hoSbJwk '"qi":"IYd7DHOhrWvxkwPQsRM2tOgrjbcrfvtQJipd-DlcxyVuuM9sQLdgjVk2o' To iSuccess Get ComAppend Of hoSbJwk "y26F0EmpScGLq2MowX7fhd_QJQ3ydy5cY7YIBi87w93IKLEdfnbJtoOPLU" To iSuccess Get ComAppend Of hoSbJwk 'W0ITrJReOgo1cq9SbsxYawBgfp_gh6A5603k2-ZQwVK0JKSHuLFkuQ3U"' To iSuccess Get ComAppend Of hoSbJwk "}" To iSuccess Get Create (RefClass(cComChilkatPrivateKey)) To hoRsaKey If (Not(IsComObjectCreated(hoRsaKey))) Begin Send CreateComObject of hoRsaKey End // Note: This example loads the RSA key from JWK format. Any format can be loaded // into the private key object. (See the online reference documentation..) Get ComGetAsString Of hoSbJwk To sTemp1 Get ComLoadJwk Of hoRsaKey sTemp1 To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoRsaKey To sTemp1 Showln sTemp1 Procedure_Return End // Create the JWS Protected Header Get Create (RefClass(cComChilkatJsonObject)) To hoJwsProtHdr If (Not(IsComObjectCreated(hoJwsProtHdr))) Begin Send CreateComObject of hoJwsProtHdr End Get ComAppendString Of hoJwsProtHdr "alg" "PS256" To iSuccess Get Create (RefClass(cComChilkatJws)) To hoJws If (Not(IsComObjectCreated(hoJws))) Begin Send CreateComObject of hoJws End // Set the protected header: Move 0 To iSignatureIndex Get pvComObject of hoJwsProtHdr to vJwsProtHdr Get ComSetProtectedHeader Of hoJws iSignatureIndex vJwsProtHdr To iSuccess // Set the RSA key: Get pvComObject of hoRsaKey to vRsaKey Get ComSetPrivateKey Of hoJws iSignatureIndex vRsaKey To iSuccess // Set the payload. Move False To iBIncludeBom Move "In our village, folks say God crumbles up the old moon into stars." To sPayloadStr Get ComSetPayload Of hoJws sPayloadStr "utf-8" iBIncludeBom To iSuccess // Create the JWS // By default, the compact serialization is used. Get ComCreateJws Of hoJws To sJwsCompact Get ComLastMethodSuccess Of hoJws To bTemp1 If (bTemp1 <> True) Begin Get ComLastErrorText Of hoJws To sTemp1 Showln sTemp1 Procedure_Return End Showln "JWS: " sJwsCompact // Note: RSA PSS padding uses random values, so the output will be different each time a signature is produced. // sample output: // JWS: eyJhbGciOiJQUzI1NiJ9.SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu.TRWhwRo5dMv9-8OzrInfJTwmUGYgjLfHk8lqF072ND-FmLWEBnUTOpY8oJXp8FdWw2SalbdOeNlrtlJjwk4XK8Ql2iJ_2qMCtxsvLPhKBOqFoAF4aBvTOEDVJDxf0DaBSiydEEtfTVV2iwBcjWabu5J2XieR5y7QZQtuHsn7T3qKBvCcCejN3Y2oqAT3qMHvu1fTms1r_91wBn_K7Wjd9UkZ1n02qQcUHJznR_OF2BgN7_KWIDAF9ZS9keoju2NPpPelO4yxa2XUPnehY3G7dHKoCxUEQR4d2Xc5voqDASTVCDqQS4PVOZdvT3Ein6-SanAlCwbWBbkvT8g6-5PImQ // Now load the JWS, validate, and recover the original text. Get Create (RefClass(cComChilkatJws)) To hoJws2 If (Not(IsComObjectCreated(hoJws2))) Begin Send CreateComObject of hoJws2 End // Load the JWS. Get ComLoadJws Of hoJws2 sJwsCompact To iSuccess Get ComGetPublicKey Of hoRsaKey To vRsaPubKey If (IsComObject(vRsaPubKey)) Begin Get Create (RefClass(cComChilkatPublicKey)) To hoRsaPubKey Set pvComObject Of hoRsaPubKey To vRsaPubKey End // Set the RSA public key used for validation. Move 0 To iSignatureIndex Get ComSetPublicKey Of hoJws2 iSignatureIndex vRsaPubKey To iSuccess Send Destroy of hoRsaPubKey // Validate the 1st (and only) signature at index 0.. Get ComValidate Of hoJws2 iSignatureIndex To v If (v < 0) Begin // Perhaps Chilkat was not unlocked or the trial expired.. Showln "Method call failed for some other reason." Get ComLastErrorText Of hoJws2 To sTemp1 Showln sTemp1 Procedure_Return End If (v = 0) Begin Showln "Invalid signature. The RSA key was incorrect, the JWS was invalid, or both." Procedure_Return End // If we get here, the signature was validated.. Showln "Signature validated." // Recover the original content: Get ComGetPayload Of hoJws2 "utf-8" To sTemp1 Showln sTemp1 // Examine the protected header: Get ComGetProtectedHeader Of hoJws2 iSignatureIndex To vJoseHeader If (IsComObject(vJoseHeader)) Begin Get Create (RefClass(cComChilkatJsonObject)) To hoJoseHeader Set pvComObject Of hoJoseHeader To vJoseHeader End Get ComLastMethodSuccess Of hoJws2 To bTemp1 If (bTemp1 <> True) Begin Showln "No protected header found at the given index." Procedure_Return End Set ComEmitCompact Of hoJoseHeader To False Showln "Protected (JOSE) header:" Get ComEmit Of hoJoseHeader To sTemp1 Showln sTemp1 Send Destroy of hoJoseHeader // Output: // Signature validated. // In our village, folks say God crumbles up the old moon into stars. // Protected (JOSE) header: // { // "alg": "PS256" // } End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.