Skip to content

Commit 7d98202

Browse files
committed
fix(key): use algorithm-specific hash for thumbprints
Thumbprints must use the algorithm's hash function, not always SHA-256. Per Coz spec: - ES256 → SHA-256 (32 bytes) - ES384 → SHA-384 (48 bytes) - ES512/Ed25519 → SHA-512 (64 bytes) Thanks to the Coz protocol creator for catching this!
1 parent 2177abb commit 7d98202

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/key.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use crate::alg::{Algorithm, ES256, ES384, ES512, Ed25519};
1818

1919
/// A key thumbprint - the hash of the canonical `{"alg":"...","pub":"..."}`.
2020
///
21-
/// Thumbprints uniquely identify keys and are always SHA-256 (32 bytes),
22-
/// regardless of the algorithm.
21+
/// Thumbprints uniquely identify keys. The hash algorithm is determined by
22+
/// the key's algorithm (e.g., ES256 → SHA-256, ES384 → SHA-384, ES512 → SHA-512).
2323
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
2424
pub struct Thumbprint(#[serde(with = "crate::b64")] Vec<u8>);
2525

@@ -353,13 +353,17 @@ where
353353
// ============================================================================
354354

355355
/// Compute the thumbprint for a key.
356+
///
357+
/// The thumbprint is the hash of the canonical `{"alg":"...","pub":"..."}` JSON.
358+
/// The hash algorithm is determined by the key's algorithm (e.g., ES256 → SHA-256,
359+
/// ES384 → SHA-384, ES512 → SHA-512, Ed25519 → SHA-512).
356360
fn compute_thumbprint<A: Algorithm>(pub_bytes: &[u8]) -> Thumbprint {
357361
use base64ct::{Base64UrlUnpadded, Encoding};
358-
use sha2::Sha256;
362+
use digest::Digest;
359363

360364
let pub_b64 = Base64UrlUnpadded::encode_string(pub_bytes);
361365
let canonical = format!(r#"{{"alg":"{}","pub":"{}"}}"#, A::NAME, pub_b64);
362-
let hash = Sha256::digest(canonical.as_bytes());
366+
let hash = <A::Hasher>::digest(canonical.as_bytes());
363367

364368
Thumbprint::from_bytes(hash.to_vec())
365369
}

0 commit comments

Comments
 (0)