Sample code for 30+ languages & platforms
Swift

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 Swift Downloads

Swift

func chilkatTest() {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let crypt = CkoCrypt2()!

    crypt.encodingMode = "base64"
    crypt.hashAlgorithm = "sha256"
    crypt.macAlgorithm = "hmac"

    var publicKey: String? = "oRNWbHFXtAECmhnZmEndcjLIaSKbRMVE"
    var privateKey: String? = "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

    var http_verb: String? = "GET"
    var content_type: String? = "application/xml"
    var x_bol_date: String? = "Wed, 17 Feb 2016 00:00:00 GMT"
    var uri: String? = "/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.
    let sb = CkoStringBuilder()!
    sb.append(value: http_verb)
    sb.append(value: "\n\n")
    sb.append(value: content_type)
    sb.append(value: "\n")
    sb.append(value: x_bol_date)
    sb.append(value: "\nx-bol-date:")
    sb.append(value: x_bol_date)
    sb.append(value: "\n")
    sb.append(value: uri)
    print("[\(sb.getAsString()!)]")

    // Set the HMAC key:
    crypt.setMacKeyEncoded(key: privateKey, encoding: "ascii")
    var mac: String? = crypt.macStringENC(inText: sb.getAsString())

    // The answer should be: nqzLWvXI1eBhBXrRx5NF23V5hS8Q1xWCloJzPi/RAts=
    print("\(mac!)")

    // The last step is to append the public key with the signature
    let sbHeader = CkoStringBuilder()!
    sbHeader.append(value: publicKey)
    sbHeader.append(value: ":")
    sbHeader.append(value: mac)

    var hdrValue: String? = sbHeader.getAsString()
    print("\(hdrValue!)")

}