Skip to content

Commit

Permalink
add change_password! method
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl committed Feb 10, 2024
1 parent 8858994 commit c91ef02
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ A rubygem for creating and verifying [Minisign](http://jedisct1.github.io/minisi
- [Read a public key](#read-a-public-key)
- [Verify a signature](#verify-a-signature)
- [Read a private key](#read-a-private-key)
- [Change the private key's password](#change-the-private-keys-password)
- [Create a signature](#create-a-signature)
- [Generate a key pair](#generate-a-key-pair)
- [Local Development](#local-development)
Expand Down Expand Up @@ -41,6 +42,13 @@ password = "password" # optional, if the key is not encrypted
private_key = Minisign::PrivateKey.new(File.read("minisign.key"), password)
```

### Change the private key's password

```rb
password = "new password"
private_key.change_password! password
```

### Create a signature

```rb
Expand Down
4 changes: 4 additions & 0 deletions lib/minisign/private_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def to_s
"untrusted comment: #{@untrusted_comment}\n#{Base64.strict_encode64(data)}\n"
end

def change_password!(new_password)
@password = new_password
end

private

def signature_algorithm
Expand Down
18 changes: 18 additions & 0 deletions spec/minisign/private_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,22 @@
)).to be(true)
end
end

describe '#change_password!' do
it 'changes the password' do
random_trusted_comment = SecureRandom.uuid
new_password = SecureRandom.uuid
original_public_key = @private_key.public_key
original_signature = @private_key.sign('example.txt', 'example', random_trusted_comment)
original_private_key = @private_key.to_s
@private_key.change_password! new_password
new_signature = @private_key.sign('example.txt', 'example', random_trusted_comment)
expect(original_signature.to_s).to eq(new_signature.to_s)
expect(original_public_key.to_s).to eq(@private_key.public_key.to_s)
expect(original_private_key.to_s).not_to eq(@private_key.to_s)
expect do
Minisign::PrivateKey.new(@private_key.to_s, new_password)
end.not_to raise_error
end
end
end

0 comments on commit c91ef02

Please sign in to comment.