]> git.proxmox.com Git - proxmox.git/blob - proxmox-acme/src/json.rs
move to proxmox-acme
[proxmox.git] / proxmox-acme / src / json.rs
1 use openssl::hash::Hasher;
2 use serde_json::Value;
3
4 use crate::Error;
5
6 pub fn to_hash_canonical(value: &Value, output: &mut Hasher) -> Result<(), Error> {
7 match value {
8 Value::Null | Value::String(_) | Value::Number(_) | Value::Bool(_) => {
9 serde_json::to_writer(output, &value)?;
10 }
11 Value::Array(list) => {
12 output.update(b"[")?;
13 let mut iter = list.iter();
14 if let Some(item) = iter.next() {
15 to_hash_canonical(item, output)?;
16 for item in iter {
17 output.update(b",")?;
18 to_hash_canonical(item, output)?;
19 }
20 }
21 output.update(b"]")?;
22 }
23 Value::Object(map) => {
24 output.update(b"{")?;
25 let mut keys: Vec<&str> = map.keys().map(String::as_str).collect();
26 keys.sort_unstable();
27 let mut iter = keys.into_iter();
28 if let Some(key) = iter.next() {
29 serde_json::to_writer(&mut *output, &key)?;
30 output.update(b":")?;
31 to_hash_canonical(&map[key], output)?;
32 for key in iter {
33 output.update(b",")?;
34 serde_json::to_writer(&mut *output, &key)?;
35 output.update(b":")?;
36 to_hash_canonical(&map[key], output)?;
37 }
38 }
39 output.update(b"}")?;
40 }
41 }
42 Ok(())
43 }