Skip to content

Commit

Permalink
Merge pull request #134 from flokli/keyfile-mode
Browse files Browse the repository at this point in the history
Set mode when creating key file
  • Loading branch information
LeeSmet authored Feb 27, 2024
2 parents 80bf154 + f140167 commit 9feff08
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,23 @@ async fn load_key_file(path: &Path) -> Result<crypto::SecretKey, io::Error> {
}

async fn save_key_file(key: &crypto::SecretKey, path: &Path) -> io::Result<()> {
let mut file = File::create(path).await?;
file.write_all(key.as_bytes()).await?;
#[cfg(target_family = "unix")]
{
use tokio::fs::OpenOptions;

let mut file = OpenOptions::new()
.create(true)
.write(true)
.mode(0o600) // rw by the owner, not readable by group or others
.open(path)
.await?;
file.write_all(key.as_bytes()).await?;
}
#[cfg(not(target_family = "unix"))]
{
let mut file = File::create(path).await?;
file.write_all(key.as_bytes()).await?;
}

Ok(())
}

0 comments on commit 9feff08

Please sign in to comment.