Rust
Rust
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 Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let crypt = chilkat::Crypt2::new();
crypt.set_encoding_mode("base64");
crypt.set_hash_algorithm("sha256");
crypt.set_mac_algorithm("hmac");
let public_key = "oRNWbHFXtAECmhnZmEndcjLIaSKbRMVE".to_string();
let private_key = "MaQHPOnmYkPZNgeRziPnQyyOJYytUbcFBVJBvbMKoDdpPqaZbaOiLUTWzPAkpPsZFZbJHrcoltdgpZolyNcgvvBaKcmkqFjucFzXhDONTsPAtHHyccQlLUZpkOuywMiOycDWcCySFsgpDiyGnCWCZJkNTtVdPxbSUTWVIFQiUxaPDYDXRQAVVTbSVZArAZkaLDLOoOvPzxSdhnkkJWzlQDkqsXNKfAIgAldrmyfROSyCGMCfvzdQdUQEaYZTPEoA".to_string();
// 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
let http_verb = "GET".to_string();
let content_type = "application/xml".to_string();
let x_bol_date = "Wed, 17 Feb 2016 00:00:00 GMT".to_string();
let uri = "/services/rest/orders/v2".to_string();
// 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 = chilkat::StringBuilder::new();
let _ = sb.append(&http_verb);
let _ = sb.append("\n\n");
let _ = sb.append(&content_type);
let _ = sb.append("\n");
let _ = sb.append(&x_bol_date);
let _ = sb.append("\nx-bol-date:");
let _ = sb.append(&x_bol_date);
let _ = sb.append("\n");
let _ = sb.append(&uri);
println!("[{}]", sb.get_as_string().unwrap_or_default());
// Set the HMAC key:
let _ = crypt.set_mac_key_encoded(&private_key, "ascii");
let mac = crypt.mac_string_enc(&sb.get_as_string().unwrap_or_default()).unwrap_or_default();
// The answer should be: nqzLWvXI1eBhBXrRx5NF23V5hS8Q1xWCloJzPi/RAts=
println!("{}", mac);
// The last step is to append the public key with the signature
let sb_header = chilkat::StringBuilder::new();
let _ = sb_header.append(&public_key);
let _ = sb_header.append(":");
let _ = sb_header.append(&mac);
let hdr_value = sb_header.get_as_string().unwrap_or_default();
println!("{}", hdr_value);