Skip to content

Commit

Permalink
use sha256
Browse files Browse the repository at this point in the history
  • Loading branch information
MattFerraro committed Apr 10, 2024
1 parent 86901b6 commit bef6880
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 27 deletions.
77 changes: 68 additions & 9 deletions packages/cadmium/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cadmium/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ geo = "0.26.0"
serde_with = "3.4.0"
crc32fast = "1.3.2"
indexmap = "2.1.0"
highhash = "0.2.0"
sha2 = "0.10.8"

[patch.crates-io]
tsify = { git = "https://github.com/siefkenj/tsify" }
Expand Down
35 changes: 18 additions & 17 deletions packages/cadmium/src/oplog/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use highhash::Murmur3Hasher;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::hash::{Hash, Hasher};
use std::process::id;

Expand Down Expand Up @@ -47,9 +47,9 @@ impl OpLog {

fn id_from_op_and_parent(operation: &Operation, parent: &Sha) -> Sha {
let h = operation.hash();
let mut hasher = Murmur3Hasher::default();
hasher.write(format!("{h}-{parent}").as_bytes());
format!("{:x}", hasher.finish())
let mut hasher = Sha256::new();
hasher.update(format!("{h}-{parent}").as_bytes());
format!("{:x}", hasher.finalize())
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -125,7 +125,8 @@ impl Commit {
}

pub fn pretty_print(&self) -> String {
format!("{}: {}", self.id, self.operation.pretty_print())
// truncate to just the first 10 chars of self.id
format!("{}: {}", &self.id[..10], self.operation.pretty_print())
}
}

Expand Down Expand Up @@ -172,45 +173,45 @@ pub enum Operation {

impl Operation {
pub fn hash(&self) -> Sha {
let mut hasher = Murmur3Hasher::default();
hasher.write("cadmium".as_bytes()); // mm, salt
let mut hasher = Sha256::new();

hasher.update("cadmium".as_bytes()); // mm, salt
match self {
Operation::Create { nonce } => hasher.write(format!("{nonce}").as_bytes()),
Operation::Create { nonce } => hasher.update(format!("{nonce}").as_bytes()),
Operation::Describe {
description,
commit,
} => hasher.write(format!("{description}-{commit}").as_bytes()),
} => hasher.update(format!("{description}-{commit}").as_bytes()),
Operation::NewPlane { name, plane } => {
hasher.write(format!("{name}-{plane:?}").as_bytes())
hasher.update(format!("{name}-{plane:?}").as_bytes())
}
Operation::NewSketch { name, plane_name } => {
hasher.write(format!("{name}-{plane_name:?}").as_bytes())
hasher.update(format!("{name}-{plane_name:?}").as_bytes())
}
Operation::NewRectangle {
sketch_name,
x,
y,
width,
height,
} => hasher.write(format!("{sketch_name}-{x}-{y}-{width}-{height}").as_bytes()),
} => hasher.update(format!("{sketch_name}-{x}-{y}-{width}-{height}").as_bytes()),
Operation::NewExtrusion {
name,
sketch_name,
click_x,
click_y,
depth,
} => {
hasher.write(format!("{name}-{sketch_name}-{click_x}-{click_y}-{depth}").as_bytes())
}
} => hasher
.update(format!("{name}-{sketch_name}-{click_x}-{click_y}-{depth}").as_bytes()),
Operation::NewCircle {
sketch_name,
x,
y,
radius,
} => hasher.write(format!("{sketch_name}-{x}-{y}-{radius}").as_bytes()),
} => hasher.update(format!("{sketch_name}-{x}-{y}-{radius}").as_bytes()),
}

format!("{:x}", hasher.finish())
format!("{:x}", hasher.finalize())
}

pub fn pretty_print(&self) -> String {
Expand Down

0 comments on commit bef6880

Please sign in to comment.