Simplify key generation for tests and debugging
Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
03fd69f323
commit
66d1f99a1e
|
|
@ -374,6 +374,23 @@ impl sqlite::Bindable for &PublicKey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod keypair {
|
||||||
|
use super::*;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
/// Generate a new keypair using OS randomness.
|
||||||
|
pub fn generate() -> KeyPair {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
if env::var("RAD_DEBUG").is_ok() {
|
||||||
|
// Generate a test keypair that is always the same.
|
||||||
|
// This is useful for debugging and testing, since the
|
||||||
|
// public key is known in advance.
|
||||||
|
return KeyPair::from_seed(Seed::new([0xff; 32]));
|
||||||
|
}
|
||||||
|
KeyPair::generate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::PublicKey;
|
use crate::PublicKey;
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use std::{fs, io};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use zeroize::Zeroizing;
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
use crate::{KeyPair, PublicKey, SecretKey, Signature, Signer, SignerError};
|
use crate::{keypair, PublicKey, SecretKey, Signature, Signer, SignerError};
|
||||||
|
|
||||||
/// A secret key passphrase.
|
/// A secret key passphrase.
|
||||||
pub type Passphrase = Zeroizing<String>;
|
pub type Passphrase = Zeroizing<String>;
|
||||||
|
|
@ -47,7 +47,7 @@ impl Keystore {
|
||||||
/// The `comment` is associated with the private key.
|
/// The `comment` is associated with the private key.
|
||||||
/// The `passphrase` is used to encrypt the private key.
|
/// The `passphrase` is used to encrypt the private key.
|
||||||
pub fn init(&self, comment: &str, passphrase: &str) -> Result<PublicKey, Error> {
|
pub fn init(&self, comment: &str, passphrase: &str) -> Result<PublicKey, Error> {
|
||||||
let pair = KeyPair::generate();
|
let pair = keypair::generate();
|
||||||
let ssh_pair = ssh_key::private::Ed25519Keypair::from_bytes(&pair)?;
|
let ssh_pair = ssh_key::private::Ed25519Keypair::from_bytes(&pair)?;
|
||||||
let ssh_pair = ssh_key::private::KeypairData::Ed25519(ssh_pair);
|
let ssh_pair = ssh_key::private::KeypairData::Ed25519(ssh_pair);
|
||||||
let secret = ssh_key::PrivateKey::new(ssh_pair, comment)?;
|
let secret = ssh_key::PrivateKey::new(ssh_pair, comment)?;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue