DataFlex
DataFlex
RSA Sign utf-8 Bytes of String to get Base64 RSA Signature
See more Apple Keychain Examples
Demonstrates how RSA sign the utf-8 byte representation of a string to get the signature in base64 format.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Variant vCert
Handle hoCert
Handle hoSb
Boolean iCrlfLineEnding
Integer i
Handle hoRsa
String sStringToSign
String sSigBase64
String sTemp1
Boolean bTemp1
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 cert: " sTemp1
// Create a string to be hashed and signed.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
If (Not(IsComObjectCreated(hoSb))) Begin
Send CreateComObject of hoSb
End
Move True To iCrlfLineEnding
For i From 0 To 10
Get ComAppendLine Of hoSb "This is a test." iCrlfLineEnding 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
// Sign the SHA-256 hash of the utf-8 byte representation of the contents of sb
// Return the signature in base64 format.
Set ComEncodingMode Of hoRsa To "base64"
Set ComCharset Of hoRsa To "utf-8"
Get ComGetAsString Of hoSb To sStringToSign
Get ComSignStringENC Of hoRsa sStringToSign "sha256" To sSigBase64
Get ComLastMethodSuccess Of hoRsa To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoRsa To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "RSA signature as base64: " sSigBase64
End_Procedure