package Crypt::MultiKey::PKey::Password;
our $VERSION = '0.000_002'; # TRIAL VERSION
# ABSTRACT: Default PKey protection scheme - encrypt private half with a password

use v5.12;
use warnings;
use Carp;
use parent 'Crypt::MultiKey::PKey';


sub protection_scheme {
   @_ > 1? $_[0]->_set_protection_scheme($_[1]) : 'Password';
}


sub can_obtain_private {
   return -t *main::STDIN;
}


sub obtain_private {
   my $self= shift;
   # private already loaded? nothing to do.
   return if $self->has_private;
   croak "Can't decrypt an empty private_encrypted attribute"
      unless defined $self->private_encrypted;
   my $pw= secret(stringify_mask => '[PASSWORD]');
   my $name= $self->path // $self->fingerprint;
   $pw->append_console_line(\*main::STDIN, prompt => "Private key $name is encrypted.\nEnter password: ")
      or croak "Canceled password prompt";
   $pw->length
      or croak "Empty password";
   $self->decrypt_private($pw);
}

# Avoid depending on namespace::clean
delete @Crypt::MultiKey::PKey::Password::{qw(
   carp confess croak
)};

__END__

=pod

=encoding UTF-8

=head1 NAME

Crypt::MultiKey::PKey::Password - Default PKey protection scheme - encrypt private half with a password

=head1 DESCRIPTION

This is the default PKey protection scheme that you get when you call C<encrypt_private> on a
previously unprotected PKey object.  In order to interactively read the password, it expects
STDIN to be a terminal.  You can bypass this requirement using  C<decrypt_private> directly.

=head1 METHODS

=head2 can_obtain_private

This returns true if STDIN is a console, meaning that it can display a password prompt to the
user.

=head2 obtain_private

Prompt the user for a password and pass that to L</decrypt_private>.  This is a no-op if the
private half is already loaded.

=head1 VERSION

version 0.000_002

=head1 AUTHOR

Michael Conrad <mike@nrdvana.net>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Michael Conrad.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
