Sample code for 30+ languages & platforms
PureBasic

Plaza API (bol.com) HMAC-SHA256 Authentication

See more Encryption Examples

Demonstrates how to compute the Authorization header for bol.com using HMAC-SHA256.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCrypt2.pb"
IncludeFile "CkStringBuilder.pb"

Procedure ChilkatExample()

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

    crypt.i = CkCrypt2::ckCreate()
    If crypt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkCrypt2::setCkEncodingMode(crypt, "base64")
    CkCrypt2::setCkHashAlgorithm(crypt, "sha256")
    CkCrypt2::setCkMacAlgorithm(crypt, "hmac")

    publicKey.s = "oRNWbHFXtAECmhnZmEndcjLIaSKbRMVE"
    privateKey.s = "MaQHPOnmYkPZNgeRziPnQyyOJYytUbcFBVJBvbMKoDdpPqaZbaOiLUTWzPAkpPsZFZbJHrcoltdgpZolyNcgvvBaKcmkqFjucFzXhDONTsPAtHHyccQlLUZpkOuywMiOycDWcCySFsgpDiyGnCWCZJkNTtVdPxbSUTWVIFQiUxaPDYDXRQAVVTbSVZArAZkaLDLOoOvPzxSdhnkkJWzlQDkqsXNKfAIgAldrmyfROSyCGMCfvzdQdUQEaYZTPEoA"

    ; The string to sign is this:
    ; http_verb +'\n\n'+ content_type +'\n'+ x_bol_date +'\n'+ 'x-bol-date:'+ x_bol_date +'\n'+ uri

    http_verb.s = "GET"
    content_type.s = "application/xml"
    x_bol_date.s = "Wed, 17 Feb 2016 00:00:00 GMT"
    uri.s = "/services/rest/orders/v2"

    ; IMPORTANT: Notice the use of underscore and hyphen (dash) chars in x-bol-date vs. x_bol_date.
    ; In one place hypens are used.  In two places, underscore chars are used.
    sb.i = CkStringBuilder::ckCreate()
    If sb.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sb,http_verb)
    CkStringBuilder::ckAppend(sb,Chr(10) + Chr(10))
    CkStringBuilder::ckAppend(sb,content_type)
    CkStringBuilder::ckAppend(sb,Chr(10))
    CkStringBuilder::ckAppend(sb,x_bol_date)
    CkStringBuilder::ckAppend(sb,Chr(10) + "x-bol-date:")
    CkStringBuilder::ckAppend(sb,x_bol_date)
    CkStringBuilder::ckAppend(sb,Chr(10))
    CkStringBuilder::ckAppend(sb,uri)
    Debug "[" + CkStringBuilder::ckGetAsString(sb) + "]"

    ; Set the HMAC key:
    CkCrypt2::ckSetMacKeyEncoded(crypt,privateKey,"ascii")
    mac.s = CkCrypt2::ckMacStringENC(crypt,CkStringBuilder::ckGetAsString(sb))

    ; The answer should be: nqzLWvXI1eBhBXrRx5NF23V5hS8Q1xWCloJzPi/RAts=
    Debug mac

    ; The last step is to append the public key with the signature
    sbHeader.i = CkStringBuilder::ckCreate()
    If sbHeader.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbHeader,publicKey)
    CkStringBuilder::ckAppend(sbHeader,":")
    CkStringBuilder::ckAppend(sbHeader,mac)

    hdrValue.s = CkStringBuilder::ckGetAsString(sbHeader)
    Debug hdrValue


    CkCrypt2::ckDispose(crypt)
    CkStringBuilder::ckDispose(sb)
    CkStringBuilder::ckDispose(sbHeader)


    ProcedureReturn
EndProcedure