Is it possible to sort JSON in rust language? If it is possible then how?
Like this one:
const headers = {
'checkout-account': '1234',
'checkout-algorithm': 'sha256',
'checkout-method': 'POST',
'checkout-nonce': '564635208570151',
'checkout-timestamp': '2018-07-06T10:01:31.904Z',
};
const calculateHmac = (body=false, params) => {
const hmacPayload = Object.keys(params)
.sort()
.map((key) => [key, params[key]].join(':'))
.concat(body ? JSON.stringify(body) : '')
.join('\n');
};
calculateHmac(false, headers);
>Solution :
More or less the same code in Rust:
use itertools::Itertools;
use std::collections::HashMap;
fn main() {
let headers = HashMap::from([
("checkout-account", "1234"),
("checkout-algorithm", "sha256"),
("checkout-method", "POST"),
("checkout-nonce", "564635208570151"),
("checkout-timestamp", "2018-07-06T10,01,31.904Z"),
]);
let hmac_payload = headers
.keys()
.sorted()
.map(|key| format!("{}:{}", key, headers[key]))
.join("\n");
println!("{hmac_payload}");
}