summaryrefslogtreecommitdiff
path: root/utils/tcp_connection/src/instance_challenge.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-02-12 03:11:51 +0800
committer魏曹先生 <1992414357@qq.com>2026-02-12 03:11:51 +0800
commit78edd539c3a40186804a40b2fcefce6f3e0b8e2c (patch)
treedc71b216dbf0df0685e538b6b66986b3065884e5 /utils/tcp_connection/src/instance_challenge.rs
parent762e3119401fbee25ec18fee2ff220d9b12d48e8 (diff)
Update crypto dependencies and refactor challenge generation
Diffstat (limited to 'utils/tcp_connection/src/instance_challenge.rs')
-rw-r--r--utils/tcp_connection/src/instance_challenge.rs17
1 files changed, 10 insertions, 7 deletions
diff --git a/utils/tcp_connection/src/instance_challenge.rs b/utils/tcp_connection/src/instance_challenge.rs
index 3a7f6a3..12fce54 100644
--- a/utils/tcp_connection/src/instance_challenge.rs
+++ b/utils/tcp_connection/src/instance_challenge.rs
@@ -1,6 +1,5 @@
use std::path::Path;
-use rand::TryRngCore;
use rsa::{
RsaPrivateKey, RsaPublicKey,
pkcs1::{DecodeRsaPrivateKey, DecodeRsaPublicKey},
@@ -43,12 +42,7 @@ impl ConnectionInstance {
public_key_dir: impl AsRef<Path>,
) -> Result<(bool, String), TcpTargetError> {
// Generate random challenge
- let mut challenge = [0u8; 32];
- rand::rngs::OsRng
- .try_fill_bytes(&mut challenge)
- .map_err(|e| {
- TcpTargetError::Crypto(format!("Failed to generate random challenge: {}", e))
- })?;
+ let challenge = Self::gen_challenge()?;
// Send challenge to target
self.stream.write_all(&challenge).await?;
@@ -106,6 +100,15 @@ impl ConnectionInstance {
Ok((verified, key_id))
}
+ fn gen_challenge() -> Result<[u8; 32], TcpTargetError> {
+ let mut challenge = [0u8; 32];
+ let mut rng = rand::rng();
+ rand::TryRng::try_fill_bytes(&mut rng, &mut challenge).map_err(|e| {
+ TcpTargetError::Crypto(format!("Failed to generate random challenge: {}", e))
+ })?;
+ return Ok(challenge);
+ }
+
/// Accepts a challenge from the target machine to verify connection security
///
/// This method performs a cryptographic challenge-response authentication: