[repo] Undo cargo fmt churn in files this branch does not change; CLAUDE.md: the trap

This commit is contained in:
2026-11-04 00:27:37 +01:00
parent 06410ab3be
commit 510d5eecd1
7 changed files with 34 additions and 113 deletions
+6 -21
View File
@@ -84,11 +84,9 @@ pub fn generate(alg: Algorithm, comment: &str) -> Result<KeyPair, KeyError> {
/// An OpenSSH key keeps its own comment unless it has none.
pub fn import(text: &str, comment: &str) -> Result<KeyPair, KeyError> {
// Pasted or read from a Windows file: CRLF line ends.
let text = text.replace(
"
let text = text.replace("
", "
",
);
");
let text = text.trim();
let has = |label: &str| text.contains(&format!("-----BEGIN {label}-----"));
if has("ENCRYPTED PRIVATE KEY") || text.contains("Proc-Type: 4,ENCRYPTED") {
@@ -101,9 +99,7 @@ pub fn import(text: &str, comment: &str) -> Result<KeyPair, KeyError> {
}
let comment = if key.comment().is_empty() { comment.to_owned() } else { key.comment().to_owned() };
return match key.key_data() {
KeypairData::Ed25519(k) => {
from_ed25519(ed25519_dalek::SigningKey::from_bytes(&k.private.to_bytes()), &comment)
}
KeypairData::Ed25519(k) => from_ed25519(ed25519_dalek::SigningKey::from_bytes(&k.private.to_bytes()), &comment),
KeypairData::Ecdsa(EcdsaKeypair::NistP256 { private, .. }) => {
from_p256(p256::SecretKey::from_slice(private.as_slice()).map_err(|_| KeyError::Invalid)?, &comment)
}
@@ -139,13 +135,8 @@ pub fn import(text: &str, comment: &str) -> Result<KeyPair, KeyError> {
/// rebuilt from its components here.
fn rsa_from_openssh(k: &RsaKeypair) -> Result<rsa::RsaPrivateKey, KeyError> {
let n = |m: &ssh_key::Mpint| rsa::BigUint::try_from(m).map_err(|_| KeyError::Invalid);
let sk = rsa::RsaPrivateKey::from_components(
n(&k.public.n)?,
n(&k.public.e)?,
n(&k.private.d)?,
vec![n(&k.private.p)?, n(&k.private.q)?],
)
.map_err(|_| KeyError::Invalid)?;
let sk = rsa::RsaPrivateKey::from_components(n(&k.public.n)?, n(&k.public.e)?, n(&k.private.d)?, vec![n(&k.private.p)?, n(&k.private.q)?])
.map_err(|_| KeyError::Invalid)?;
sk.validate().map_err(|_| KeyError::Invalid)?;
Ok(sk)
}
@@ -178,13 +169,7 @@ fn from_rsa(sk: rsa::RsaPrivateKey, comment: &str) -> Result<KeyPair, KeyError>
assemble(&format!("rsa-{bits}"), KeypairData::Rsa(enc(RsaKeypair::try_from(sk))?), private_pem, public_pem, comment)
}
fn assemble(
algorithm: &str,
data: KeypairData,
private_pem: String,
public_pem: String,
comment: &str,
) -> Result<KeyPair, KeyError> {
fn assemble(algorithm: &str, data: KeypairData, private_pem: String, public_pem: String, comment: &str) -> Result<KeyPair, KeyError> {
let private = enc(PrivateKey::new(data, comment))?;
let public = private.public_key();
Ok(KeyPair {
+1 -2
View File
@@ -87,8 +87,7 @@ fn every_private_key_format_imports_to_the_same_key() {
for text in inputs {
// Surrounding whitespace (a paste, a file with CRLF line ends) does not matter.
let pasted = format!("\r\n {}\r\n", text.replace('\n', "\r\n"));
let i = custos_keys::import(&pasted, "deploy")
.unwrap_or_else(|e| panic!("{alg:?} {}: {e:?}", text.lines().next().unwrap()));
let i = custos_keys::import(&pasted, "deploy").unwrap_or_else(|e| panic!("{alg:?} {}: {e:?}", text.lines().next().unwrap()));
assert_eq!((i.algorithm.as_str(), i.fingerprint.as_str()), (alg.name(), k.fingerprint.as_str()), "{alg:?}");
assert_eq!(i.public_pem, k.public_pem);
}