DataFlex
DataFlex
Compute JWK Thumbprint for RSA and EC Public Keys
See more ECC Examples
Demonstrates how to compute a JSON Web Key thumbprint for a public key (RSA or ECC).Note: This example requires Chilkat v9.5.0.66 or greater.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPubKey
Handle hoSb
String sTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatPublicKey)) To hoPubKey
If (Not(IsComObjectCreated(hoPubKey))) Begin
Send CreateComObject of hoPubKey
End
// A public key can be loaded from any format (binary DER, PEM, etc.)
// This example will load the public keys from JWK format,
// and will then compute the JWK thumbprint.
// First do it for an RSA public key...
Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
If (Not(IsComObjectCreated(hoSb))) Begin
Send CreateComObject of hoSb
End
Get ComAppend Of hoSb "{" To iSuccess
Get ComAppend Of hoSb '"kty": "RSA",' To iSuccess
Get ComAppend Of hoSb '"n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAt' To iSuccess
Get ComAppend Of hoSb "VT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn6" To iSuccess
Get ComAppend Of hoSb "4tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FD" To iSuccess
Get ComAppend Of hoSb "W2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n9" To iSuccess
Get ComAppend Of hoSb "1CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINH" To iSuccess
Get ComAppend Of hoSb 'aQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",' To iSuccess
Get ComAppend Of hoSb '"e": "AQAB",' To iSuccess
Get ComAppend Of hoSb '"alg": "RS256",' To iSuccess
Get ComAppend Of hoSb '"kid": "2011-04-29"' To iSuccess
Get ComAppend Of hoSb "}" To iSuccess
// The JWK format is automatically detected..
Get ComGetAsString Of hoSb To sTemp1
Get ComLoadFromString Of hoPubKey sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoPubKey To sTemp1
Showln sTemp1
Procedure_Return
End
// Get the JWK thumbprint (using SHA256)
Get ComGetJwkThumbprint Of hoPubKey "SHA256" To sTemp1
Showln "JWK thumbprint: " sTemp1
// Output:
// JWK thumbprint: NzbLsXh8uDCcd-6MNwXF4W_7noWXFZAfHkxZsRGC9Xs
// --------------------------------------------------------------
// Now let's do an EC public key:
Send ComClear To hoSb
Get ComAppend Of hoSb "{ " To iSuccess
Get ComAppend Of hoSb ' "kty": "EC",' To iSuccess
Get ComAppend Of hoSb ' "crv": "P-256",' To iSuccess
Get ComAppend Of hoSb ' "x": "tDeeYABgKEAbWicYPCEEI8sP4SRIhHKcHDW7VqrB4LA",' To iSuccess
Get ComAppend Of hoSb ' "y": "J08HOoIZ0rX2Me3bNFZUltfxIk1Hrc8FsLu8VaSxsMI"' To iSuccess
Get ComAppend Of hoSb "}" To iSuccess
Get ComGetAsString Of hoSb To sTemp1
Get ComLoadFromString Of hoPubKey sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoPubKey To sTemp1
Showln sTemp1
Procedure_Return
End
// Get the JWK thumbprint (using SHA256)
Get ComGetJwkThumbprint Of hoPubKey "SHA256" To sTemp1
Showln "JWK thumbprint: " sTemp1
// Output:
// JWK thumbprint: 8fm8079s3nu4FLV_7dVJoJ69A8XCXn7Za2mtaWCnxR4
End_Procedure