pub fn register_key(
        &mut self,
        name_new_key: &str,
        new_key: RSAPublicKey,
        already_registered_private_key: &RSAPrivateKey,
    ) -> Result<(), VaultfileError> {
        if self.secrets.len() > 0 {
            let private_key_name =
                match self.find_registered_name_of_key(already_registered_private_key) {
                    Some(key_name) => key_name,
                    None => {
                        return Err(VaultfileError {
                            kind: VaultfileErrorKind::PrivateKeyNotRegistered,
                        });
                    }
                };
            let mut rng = rand::rngs::OsRng;
            // we now grant access to all the shared secrets to the newly registered key
            for (secret_name, secret) in self.secrets.iter_mut() {
                let encrypted_key = match secret.encrypted_key.get(&private_key_name) {
                    Some(key) => key,
                    None => {
                        return Err(VaultfileError {
                            kind: VaultfileErrorKind::SecretNotSharedWithAllRegisteredKeys(
                                String::from(secret_name),
                            ),
                        })
                    }
                };
                let raw_encrypted_key = base64::decode(&encrypted_key)?;
                let raw_aes_key =
                    already_registered_private_key.decrypt(PADDING, &raw_encrypted_key)?;
                // we encrypt the AES key using the newly registered public key
                // to grant them access to the secret
                let new_encrypted = new_key.encrypt(&mut rng, PADDING, &raw_aes_key)?;
                let base64_encrypted_key = base64::encode(&new_encrypted);
                secret
                    .encrypted_key
                    .insert(String::from(name_new_key), base64_encrypted_key);
            }
        }
        self.keys.insert(String::from(name_new_key), new_key);
        Ok(())
    }
/// Registers a new key in the vaultfile, under a specified name.
/// If a key under that name already exists, it will be overwritten.
Registers a new key in the vaultfile, under a specified name.
If a key under that name already exists, it will be overwritten.
Registers a new key in the vaultfile under a specified name . If a key under that name already exists it will be overwritten .
pub fn register_key(
        &mut self,
        name_new_key: &str,
        new_key: RSAPublicKey,
        already_registered_private_key: &RSAPrivateKey,
    ) -> Result<(), VaultfileError> {
        if self.secrets.len() > 0 {
            let private_key_name =
                match self.find_registered_name_of_key(already_registered_private_key) {
                    Some(key_name) => key_name,
                    None => {
                        return Err(VaultfileError {
                            kind: VaultfileErrorKind::PrivateKeyNotRegistered,
                        });
                    }
                };
            let mut rng = rand::rngs::OsRng;
            // we now grant access to all the shared secrets to the newly registered key
            for (secret_name, secret) in self.secrets.iter_mut() {
                let encrypted_key = match secret.encrypted_key.get(&private_key_name) {
                    Some(key) => key,
                    None => {
                        return Err(VaultfileError {
                            kind: VaultfileErrorKind::SecretNotSharedWithAllRegisteredKeys(
                                String::from(secret_name),
                            ),
                        })
                    }
                };
                let raw_encrypted_key = base64::decode(&encrypted_key)?;
                let raw_aes_key =
                    already_registered_private_key.decrypt(PADDING, &raw_encrypted_key)?;
                // we encrypt the AES key using the newly registered public key
                // to grant them access to the secret
                let new_encrypted = new_key.encrypt(&mut rng, PADDING, &raw_aes_key)?;
                let base64_encrypted_key = base64::encode(&new_encrypted);
                secret
                    .encrypted_key
                    .insert(String::from(name_new_key), base64_encrypted_key);
            }
        }
        self.keys.insert(String::from(name_new_key), new_key);
        Ok(())
    }
pub fn register_key ( & mut self , name_new_key : & str , new_key : RSAPublicKey , already_registered_private_key : & RSAPrivateKey , ) -> Result < ( ) , VaultfileError > { if self . secrets . len ( ) > 0 { let private_key_name = match self . find_registered_name_of_key ( already_registered_private_key ) { Some ( key_name ) => key_name , None => { return Err ( VaultfileError { kind : VaultfileErrorKind :: PrivateKeyNotRegistered , } ) ; } } ; let mut rng = rand :: rngs :: OsRng ; // we now grant access to all the shared secrets to the newly registered key for ( secret_name , secret ) in self . secrets . iter_mut ( ) { let encrypted_key = match secret . encrypted_key . get ( & private_key_name ) { Some ( key ) => key , None => { return Err ( VaultfileError { kind : VaultfileErrorKind :: SecretNotSharedWithAllRegisteredKeys ( String :: from ( secret_name ) , ) , } ) } } ; let raw_encrypted_key = base64 :: decode ( & encrypted_key ) ? ; let raw_aes_key = already_registered_private_key . decrypt ( PADDING , & raw_encrypted_key ) ? ; // we encrypt the AES key using the newly registered public key // to grant them access to the secret let new_encrypted = new_key . encrypt ( & mut rng , PADDING , & raw_aes_key ) ? ; let base64_encrypted_key = base64 :: encode ( & new_encrypted ) ; secret . encrypted_key . insert ( String :: from ( name_new_key ) , base64_encrypted_key ) ; } } self . keys . insert ( String :: from ( name_new_key ) , new_key ) ; Ok ( ( ) ) }
Registers a new key in the vaultfile, under a specified name.
Registers a new key in the vaultfile under a specified name .
&mut self
name_new_key
&str
new_key
RSAPublicKey
already_registered_private_key
&RSAPrivateKey