Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add recreate functionality #25

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Support for changing or removing the password from the private key
- `minisign` executable

## [0.1.0] - 2024-02-09

Expand Down
1 change: 1 addition & 0 deletions bin/minisign
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ rescue OptionParser::InvalidOption
end

Minisign::CLI.generate(options) if options[:G]
Minisign::CLI.recreate(options) if options[:R]
16 changes: 16 additions & 0 deletions lib/minisign/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module CLI
# lol
def self.help
puts '-G generate a new key pair'
puts '-R recreate a public key file from a secret key file'
puts '-f force. Combined with -G, overwrite a previous key pair'
puts '-p <pubkey_file> public key file (default: ./minisign.pub)'
puts '-s <seckey_file> secret key file (default: ~/.minisign/minisign.key)'
Expand All @@ -17,6 +18,7 @@ def self.help
def self.usage
puts 'Usage:'
puts 'minisign -G [-f] [-p pubkey_file] [-s seckey_file] [-W]'
puts 'minisign -R [-s seckey_file] [-p pubkey_file]'
end

def self.prompt
Expand Down Expand Up @@ -58,5 +60,19 @@ def self.generate(options)
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize

def self.recreate(options)
secret_key = options[:s] || "#{Dir.home}/.minisign/minisign.key"
public_key = options[:p] || './minisign.pub'
private_key_contents = File.read(secret_key)
begin
# try without a password first
private_key = Minisign::PrivateKey.new(private_key_contents)
rescue RuntimeError
print 'Password: '
private_key = Minisign::PrivateKey.new(private_key_contents, prompt)
end
File.write(public_key, private_key.public_key)
end
end
end
15 changes: 15 additions & 0 deletions spec/minisign/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,19 @@
expect(File.exist?(options[:p])).to eq(true)
end
end

describe '.recreate' do
it 'recreates the public key from a private key' do
keyname = SecureRandom.uuid
options = {
p: "test/generated/#{keyname}.pub",
s: 'test/minisign.key'
}
allow(Minisign::CLI).to receive(:prompt).and_return('password')
Minisign::CLI.recreate(options)
new_public_key = File.read(options[:p])
existing_public_key = File.read('test/minisign.pub').gsub(' yay', '')
expect(new_public_key).to eq(existing_public_key)
end
end
end
Loading