Sample code for 30+ languages & platforms
PowerBuilder

Ibanity HTTP Signature for XS2A, Isabel Connect, Ponto Connect

See more Ibanity Examples

Demonstrates how to add a Signature header for Ibanity HTTP requests.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Json
string ls_Payload
oleobject loo_DtNow
string ls_Created
oleobject loo_Crypt
oleobject loo_SbDigestHdrValue
string ls_Request_target
oleobject loo_SbSigningString
string ls_IdempotencyKey
string ls_Signed_headers_list
oleobject loo_PrivKey
oleobject loo_Rsa
string ls_SigBase64
oleobject loo_SbSigHeaderValue

li_Success = 0

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// In order to sign your HTTP requests, you have to add 2 headers to the HTTP request: Digest: the digest of the request payload and Signature: the actual signature of the request. 

// POST /xs2a/customer-access-tokens HTTP/1.1
// Host: api.ibanity.com
// Content-Type: application/json
// Digest: SHA-512=z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==
// Ibanity-Idempotency-Key: 61f02718-eeee-46e1-b5eb-e8fd6e799c2d
// Signature: keyId="62f02718-eeee-46e1-b5eb-e8fd6e799c2e",created=1599659223,algorithm="hs2019",headers="(request-target) host digest (created) ibanity-idempotency-key",signature="SjWJWbWN7i0...zsbM="
// 
// {"data":{"type":"customerAccessToken", "attributes":{"applicationCustomerReference":"15874569"}}}

// The payload (body) of the above HTTP request is the JSON.
// Build the JSON above.
// Use this online tool to generate code from sample JSON: 
// Generate Code to Create JSON
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Json
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Json.UpdateString("data.type","customerAccessToken")
loo_Json.UpdateString("data.attributes.applicationCustomerReference","15874569")

ls_Payload = loo_Json.Emit()
Write-Debug "payload = " + ls_Payload

// Step 1: Build the (created) virtual header

loo_DtNow = create oleobject
li_rc = loo_DtNow.ConnectToNewObject("Chilkat.CkDateTime")

loo_DtNow.SetFromCurrentSystemTime()
ls_Created = loo_DtNow.GetAsUnixTimeStr(0)
Write-Debug "created = " + ls_Created

// Step 2: Build the Digest header
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")

loo_Crypt.HashAlgorithm = "sha512"
loo_Crypt.EncodingMode = "base64"
loo_Crypt.Charset = "utf-8"

loo_SbDigestHdrValue = create oleobject
li_rc = loo_SbDigestHdrValue.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbDigestHdrValue.Append("SHA-512=")
loo_SbDigestHdrValue.Append(loo_Crypt.HashStringENC(loo_Json.Emit()))

Write-Debug loo_SbDigestHdrValue.GetAsString()

// Step 3: Build the (request target) virtual header

// In order to build the signature you will need a virtual header named (request-target) (the parentheses are important). 
// The (request-target) is the string concatenation of the HTTP method (in lowercase) with the path and query parameters.
ls_Request_target = "post /xs2a/customer-access-tokens"

// Step 4: Build the signing string

// The signing string is the concatenation of the signed header names (in lowercase) and values separated by a LF.

// You must always sign the following headers: (request-target), host, (created), digest. 
// If used, you must also sign the authorization header and any ibanity-* headers, such as ibanity-idempotency-key. 

loo_SbSigningString = create oleobject
li_rc = loo_SbSigningString.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbSigningString.Append("(request-target): ")
loo_SbSigningString.AppendLine(ls_Request_target,0)
loo_SbSigningString.Append("host: ")
loo_SbSigningString.AppendLine("api.ibanity.com",0)
loo_SbSigningString.Append("digest: ")
loo_SbSigningString.AppendLine(loo_SbDigestHdrValue.GetAsString(),0)
loo_SbSigningString.Append("(created): ")
loo_SbSigningString.AppendLine(ls_Created,0)
loo_SbSigningString.Append("ibanity-idempotency-key: ")
ls_IdempotencyKey = loo_Crypt.GenerateUuid()
loo_SbSigningString.Append(ls_IdempotencyKey)

// Step 5: Build the signed headers list

// To allow Ibanity to check the signed headers, you must provide a list of the header names. They should be lowercase and in the same order used to create the signing string. 
ls_Signed_headers_list = "(request-target) host digest (created) ibanity-idempotency-key"

// Step 6: Build the Signature header

// This is where the real signing happens. The signature header is a combination of several sub-headers -
// 
//     keyId: the identifier for the application's signature certificate, obtained from the Developer Portal
//     algorithm: the digital signature algorithm used to generate the signature (must be hs2019)
//     headers: The list of HTTP headers created in step 5
//     signature: the Base64-encoded digital signature of the signing string created in step 4.

loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_PrivKey.LoadEncryptedPemFile("my_ibanity_signature_private_key.pem","pem_password")
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_Json
    destroy loo_DtNow
    destroy loo_Crypt
    destroy loo_SbDigestHdrValue
    destroy loo_SbSigningString
    destroy loo_PrivKey
    return
end if

loo_Rsa = create oleobject
li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa")

loo_Rsa.PssSaltLen = 32
loo_Rsa.EncodingMode = "base64"
// Use the RSASSA-PSS signature algorithm
loo_Rsa.PkcsPadding = 0

li_Success = loo_Rsa.UsePrivateKey(loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Rsa.LastErrorText
    destroy loo_Json
    destroy loo_DtNow
    destroy loo_Crypt
    destroy loo_SbDigestHdrValue
    destroy loo_SbSigningString
    destroy loo_PrivKey
    destroy loo_Rsa
    return
end if

// Sign the signing string.
ls_SigBase64 = loo_Rsa.SignStringENC(loo_SbSigningString.GetAsString(),"sha-256")
if loo_Rsa.LastMethodSuccess = 0 then
    Write-Debug loo_Rsa.LastErrorText
    destroy loo_Json
    destroy loo_DtNow
    destroy loo_Crypt
    destroy loo_SbDigestHdrValue
    destroy loo_SbSigningString
    destroy loo_PrivKey
    destroy loo_Rsa
    return
end if

// Build the signature header value.
loo_SbSigHeaderValue = create oleobject
li_rc = loo_SbSigHeaderValue.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbSigHeaderValue.Append("keyId=~"")
// Use your identifier for the application's signature certificate, obtained from the Developer Portal
loo_SbSigHeaderValue.Append("62f02718-eeee-46e1-b5eb-e8fd6e799c2e")
loo_SbSigHeaderValue.Append("~",created=")
loo_SbSigHeaderValue.Append(ls_Created)
loo_SbSigHeaderValue.Append(",algorithm=~"hs2019~",headers=~"")
loo_SbSigHeaderValue.Append(ls_Signed_headers_list)
loo_SbSigHeaderValue.Append("~",signature=~"")
loo_SbSigHeaderValue.Append(ls_SigBase64)
loo_SbSigHeaderValue.Append("~"")

Write-Debug loo_SbSigHeaderValue.GetAsString()


destroy loo_Json
destroy loo_DtNow
destroy loo_Crypt
destroy loo_SbDigestHdrValue
destroy loo_SbSigningString
destroy loo_PrivKey
destroy loo_Rsa
destroy loo_SbSigHeaderValue