Sample code for 30+ languages & platforms
Rust

Calculate a X_RISKIFIED_HMAC_SHA256 for riskified.com API Calls

See more REST Misc Examples

Demonstrates how to calculate the value for the X_RISKIFIED_HMAC_SHA256 header for riskified.com HTTP requests (REST API calls).

Chilkat Rust Downloads

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

// Create the JSON that is to be the body of the HTTP request.
// The JSON created by this code is shown at the bottom of this example.
// 
// The JSON Code Generator at http://tools.chilkat.io/ can be used
// to generate the following code from sample JSON.

let json = chilkat::JsonObject::new();
let _ = json.update_null("order.cancel_reason");
let _ = json.update_null("order.cancelled_at");
let _ = json.update_string("order.cart_token", "68778783ad298f1c80c3bafcddeea02f");
let _ = json.update_null("order.closed_at");
let _ = json.update_string("order.created_at", "2008-01-10T11:00:00-05:00");
let _ = json.update_string("order.currency", "USD");
let _ = json.update_string("order.email", "bob.norman@hostmail.com");
let _ = json.update_string("order.gateway", "authorize_net");
let _ = json.update_number("order.id", "450789469");
let _ = json.update_string("order.total_discounts", "0.00");
let _ = json.update_string("order.total_price", "409.94");
let _ = json.update_string("order.updated_at", "2008-01-10T11:00:00-05:00");
let _ = json.update_string("order.note", "some note made by the shop’s stuff member");
let _ = json.update_null("order.browser_ip");
let _ = json.update_string("order.discount_codes[0].amount", "10.00");
let _ = json.update_string("order.discount_codes[0].code", "TENOFF");
let _ = json.update_string("order.line_items[0].title", "IPod Nano - 8gb - green");
let _ = json.update_number("order.line_items[0].price", "199.00");
let _ = json.update_number("order.line_items[0].product_id", "632910392");
let _ = json.update_number("order.line_items[0].quantity", "1");
let _ = json.update_string("order.line_items[0].sku", "IPOD2008GREEN");
let _ = json.update_string("order.shipping_lines[0].code", "Free Shipping");
let _ = json.update_string("order.shipping_lines[0].price", "0.00");
let _ = json.update_string("order.shipping_lines[0].title", "Free Shipping");
let _ = json.update_null("order.payment_details.avs_result_code");
let _ = json.update_null("order.payment_details.credit_card_bin");
let _ = json.update_string("order.payment_details.credit_card_company", "Visa");
let _ = json.update_string("order.payment_details.credit_card_number", "XXXX-XXXX-XXXX-4242");
let _ = json.update_null("order.payment_details.cvv_result_code");
let _ = json.update_string("order.payment_details.authorization_id", "RK346IK124");
let _ = json.update_string("order.billing_address.address1", "Chestnut Street 92");
let _ = json.update_string("order.billing_address.address2", "");
let _ = json.update_string("order.billing_address.city", "Louisville");
let _ = json.update_null("order.billing_address.company");
let _ = json.update_string("order.billing_address.country", "United States");
let _ = json.update_string("order.billing_address.country_code", "US");
let _ = json.update_string("order.billing_address.first_name", "Bob");
let _ = json.update_string("order.billing_address.last_name", "Norman");
let _ = json.update_string("order.billing_address.name", "Bob Norman");
let _ = json.update_string("order.billing_address.phone", "555-625-1199");
let _ = json.update_string("order.billing_address.province", "Kentucky");
let _ = json.update_string("order.billing_address.province_code", "KY");
let _ = json.update_string("order.billing_address.zip", "40202");
let _ = json.update_string("order.shipping_address.address1", "Chestnut Street 92");
let _ = json.update_string("order.shipping_address.address2", "");
let _ = json.update_string("order.shipping_address.city", "Louisville");
let _ = json.update_null("order.shipping_address.company");
let _ = json.update_string("order.shipping_address.country", "United States");
let _ = json.update_string("order.shipping_address.country_code", "US");
let _ = json.update_string("order.shipping_address.first_name", "Bob");
let _ = json.update_string("order.shipping_address.last_name", "Norman");
let _ = json.update_string("order.shipping_address.name", "Bob Norman");
let _ = json.update_string("order.shipping_address.phone", "555-625-1199");
let _ = json.update_string("order.shipping_address.province", "Kentucky");
let _ = json.update_string("order.shipping_address.province_code", "KY");
let _ = json.update_string("order.shipping_address.zip", "40202");
let _ = json.update_string("order.customer.created_at", "2013-04-23T13:36:50-04:00");
let _ = json.update_string("order.customer.email", "bob.norman@hostmail.com");
let _ = json.update_string("order.customer.first_name", "Bob");
let _ = json.update_number("order.customer.id", "207119551");
let _ = json.update_string("order.customer.last_name", "Norman");
let _ = json.update_null("order.customer.note");
let _ = json.update_number("order.customer.orders_count", "0");
let _ = json.update_bool("order.customer.verified_email", true);

// Emit the JSON in compact format..
json.set_emit_compact(true);
// Get the JSON that will be HMAC'd and will also be the contents of the HTTP request body.
let json_body = json.emit().unwrap_or_default();

let crypt = chilkat::Crypt2::new();
crypt.set_encoding_mode("hex");
crypt.set_hash_algorithm("sha256");
crypt.set_mac_algorithm("hmac");

let _ = crypt.set_mac_key_string("55fe0f4d4023bbdfbc124cabd88bf9bb");

let Ok(mut hmac_hex_str) = crypt.mac_string_enc(&json_body) else {
    println!("{}", crypt.last_error_text());
    return;
};

// We need a lowercase hmacHexStr...
let sb_hmac_hex = chilkat::StringBuilder::new();
let _ = sb_hmac_hex.append(&hmac_hex_str);
let _ = sb_hmac_hex.to_lowercase();

hmac_hex_str = sb_hmac_hex.get_as_string().unwrap_or_default();

println!("The value of the X_RISKIFIED_HMAC_SHA256 should be: {}", hmac_hex_str);

// This example is only to show the HMAC SHA256 calculation.
// See examples of sending REST requests to riskified.com at http://rest-examples.chilkat.io/riskified/default.cshtml

// ----------------------------------------------
// This is the JSON created by the above code..

// {
//   "order": {
//     "cancel_reason": null,
//     "cancelled_at": null,
//     "cart_token": "68778783ad298f1c80c3bafcddeea02f",
//     "closed_at": null,
//     "created_at": "2008-01-10T11:00:00-05:00",
//     "currency": "USD",
//     "email": "bob.norman@hostmail.com",
//     "gateway": "authorize_net",
//     "id": 450789469,
//     "total_discounts": "0.00",
//     "total_price": "409.94",
//     "updated_at": "2008-01-10T11:00:00-05:00",
//     "note": "some note made by the shop’s stuff member",
//     "browser_ip": null,
//     "discount_codes": [
//         {
//             "amount": "10.00",
//             "code": "TENOFF"
//         }
//     ],
//     "line_items": [
//         {
//             "title": "IPod Nano - 8gb - green",
//             "price": 199.00,
//             "product_id": 632910392,
//             "quantity": 1,
//             "sku": "IPOD2008GREEN"
//         }
//     ],
//     "shipping_lines": [
//         {
//             "code": "Free Shipping",
//             "price": "0.00",
//             "title": "Free Shipping"
//         }
//     ],
//     "payment_details": {
//         "avs_result_code": null,
//         "credit_card_bin": null,
//         "credit_card_company": "Visa",
//         "credit_card_number": "XXXX-XXXX-XXXX-4242",
//         "cvv_result_code": null,
//         "authorization_id": "RK346IK124"
//     },
//     "billing_address": {
//         "address1": "Chestnut Street 92",
//         "address2": "",
//         "city": "Louisville",
//         "company": null,
//         "country": "United States",
//         "country_code": "US",
//         "first_name": "Bob",
//         "last_name": "Norman",
//         "name": "Bob Norman",
//         "phone": "555-625-1199",
//         "province": "Kentucky",
//         "province_code": "KY",
//         "zip": "40202"
//     },
//     "shipping_address": {
//         "address1": "Chestnut Street 92",
//         "address2": "",
//         "city": "Louisville",
//         "company": null,
//         "country": "United States",
//         "country_code": "US",
//         "first_name": "Bob",
//         "last_name": "Norman",
//         "name": "Bob Norman",
//         "phone": "555-625-1199",
//         "province": "Kentucky",
//         "province_code": "KY",
//         "zip": "40202"
//     },
//     "customer": {
//         "created_at": "2013-04-23T13:36:50-04:00",
//         "email": "bob.norman@hostmail.com",
//         "first_name": "Bob",
//         "id": 207119551,
//         "last_name": "Norman",
//         "note": null,
//         "orders_count": 0,
//         "verified_email": true
//     }
//  }
// }