pub fn init_shared_secret(path: &Path) -> Result<(), Error> {
    if path.exists() {
        return Ok(());
    }

    const PASSWORD_LEN: usize = 512;
    let mut rng = rand::thread_rng();

    let secret: String = std::iter::repeat(())
        .map(|()| rng.sample(Alphanumeric))
        .map(char::from)
        .take(PASSWORD_LEN)
        .collect();

    let mut file = File::create(path)?;
    file.write_all(&secret.into_bytes())?;

    // Set proper file permissions for unix filesystems
    #[cfg(not(target_os = "windows"))]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut permissions = file
            .metadata()
            .map_err(|err| {
                Error::Generic(format!("Failed to set secret file permissions:\n{}", err))
            })?
            .permissions();
        permissions.set_mode(0o640);
        std::fs::set_permissions(path, permissions).map_err(|err| {
            Error::Generic(format!("Failed to set secret file permissions:\n{}", err))
        })?;
    }

    Ok(())
}
/// Generate a random secret and write it to a file.
Generate a random secret and write it to a file.
Generate a random secret and write it to a file .
pub fn init_shared_secret(path: &Path) -> Result<(), Error> {
    if path.exists() {
        return Ok(());
    }
    const PASSWORD_LEN: usize = 512;
    let mut rng = rand::thread_rng();
    let secret: String = std::iter::repeat(())
        .map(|()| rng.sample(Alphanumeric))
        .map(char::from)
        .take(PASSWORD_LEN)
        .collect();
    let mut file = File::create(path)?;
    file.write_all(&secret.into_bytes())?;
    // Set proper file permissions for unix filesystems
    #[cfg(not(target_os = "windows"))]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut permissions = file
            .metadata()
            .map_err(|err| {
                Error::Generic(format!("Failed to set secret file permissions:\n{}", err))
            })?
            .permissions();
        permissions.set_mode(0o640);
        std::fs::set_permissions(path, permissions).map_err(|err| {
            Error::Generic(format!("Failed to set secret file permissions:\n{}", err))
        })?;
    }
    Ok(())
}
pub fn init_shared_secret ( path : & Path ) -> Result < ( ) , Error > { if path . exists ( ) { return Ok ( ( ) ) ; } const PASSWORD_LEN : usize = 512 ; let mut rng = rand :: thread_rng ( ) ; let secret : String = std :: iter :: repeat ( ( ) ) . map ( | ( ) | rng . sample ( Alphanumeric ) ) . map ( char :: from ) . take ( PASSWORD_LEN ) . collect ( ) ; let mut file = File :: create ( path ) ? ; file . write_all ( & secret . into_bytes ( ) ) ? ; // Set proper file permissions for unix filesystems # [ cfg ( not ( target_os " " ) ) ] { use std :: os :: unix :: fs :: PermissionsExt ; let mut permissions = file . metadata ( ) . map_err ( | err | { Error :: Generic ( format ! ( " \n " err ) ) } ) ? . permissions ( ) ; permissions . set_mode ( 0o640 ) ; std :: fs :: set_permissions ( path , permissions ) . map_err ( | err | { Error :: Generic ( format ! ( " \n " err ) ) } ) ? ; } Ok ( ( ) ) }
Generate a random secret and write it to a file.
Generate a random secret and write it to a file .
path
&Path
path
&Path