DataFlex
DataFlex
RSA Sign an Encoded Hash
See more RSA Examples
Demonstrates signing a hash (e.g., SHA256) provided as an encoded string (e.g., base64 or hex).Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Variant vCert
Handle hoCert
Handle hoBd
Integer i
Handle hoRsa
String sSha256_base64
String sRsaSig_base64
String sTemp1
Move False To iSuccess
// Assuming the smartcard/USB token is installed with the correct drivers from the manufacturer,
// this code can work on multiple platforms including Windows, MacOS, Linux, and iOS.
// Chilkat automatically detects and determines the way in which the HSM is used,
// which can be by PKCS11, Apple Keychain, Microsoft CNG / Crypto API, or ScMinidriver.
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
// Set the token/smartcard PIN prior to loading.
Set ComSmartCardPin Of hoCert To "123456"
// Specify the certificate by its common name.
Get ComLoadFromSmartcard Of hoCert "cn=chilkat-rsa-2048" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCert To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComSubjectCN Of hoCert To sTemp1
Showln "Signing with the private key for this cert: " sTemp1
// Create data to be hashed and signed.
Get Create (RefClass(cComChilkatBinData)) To hoBd
If (Not(IsComObjectCreated(hoBd))) Begin
Send CreateComObject of hoBd
End
For i From 0 To 100
Get ComAppendEncoded Of hoBd "000102030405060708090A0B0C0D0E0F" "hex" To iSuccess
Loop
Get Create (RefClass(cComChilkatRsa)) To hoRsa
If (Not(IsComObjectCreated(hoRsa))) Begin
Send CreateComObject of hoRsa
End
// Use the certificate's private key for signing.
Get pvComObject of hoCert to vCert
Get ComSetX509Cert Of hoRsa vCert True To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoRsa To sTemp1
Showln sTemp1
Procedure_Return
End
// We'll first compute the hash and then pass the encoded hash to be signed.
Get ComGetHash Of hoBd "sha256" "base64" To sSha256_base64
Showln "sha256 hash in base64 format: " sSha256_base64
// Pass in the base64 hash and return a base64 signature.
Set ComEncodingMode Of hoRsa To "base64"
Get ComSignHashENC Of hoRsa sSha256_base64 "sha256" To sRsaSig_base64
If (iSuccess = False) Begin
Get ComLastErrorText Of hoRsa To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "RSA signature as base64: " sRsaSig_base64
End_Procedure