Skip to content

Commit

Permalink
support public key from string or from file (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl authored Feb 7, 2024
1 parent f073fea commit 3845a82
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,33 @@

A rubygem for creating and verifying [Minisign](http://jedisct1.github.io/minisign/) signatures.

- [Installation \& Usage](#installation--usage)
- [Read a public key](#read-a-public-key)
- [Verify a signature](#verify-a-signature)
- [Create a signature](#create-a-signature)
- [Local Development](#local-development)
- [Documentation](#documentation)

## Installation & Usage

```
gem install minisign
```

### Read a public key

```rb
require 'minisign'
public_key = Minisign::PublicKey.new('RWSmKaOrT6m3TGwjwBovgOmlhSbyBUw3hyhnSOYruHXbJa36xHr8rq2M')
# or from a file
public_key = Minisign::PublicKey.new(File.read("test/minisign.pub"))
```

### Verify a signature

```rb
require 'minisign'
public_key = Minisign::PublicKey.new('RWTg6JXWzv6GDtDphRQ/x7eg0LaWBcTxPZ7i49xEeiqXVcR+r79OZRWM')
public_key = Minisign::PublicKey.new('RWSmKaOrT6m3TGwjwBovgOmlhSbyBUw3hyhnSOYruHXbJa36xHr8rq2M')
message = File.read("test/example.txt")
signature = Minisign::Signature.new(File.read("test/example.txt.minisig"))
public_key.verify(signature, message)
Expand All @@ -21,7 +37,7 @@ public_key.verify(signature, message)
The above is equivalent to:

```
minisign -Vm test/example.txt -P RWTg6JXWzv6GDtDphRQ/x7eg0LaWBcTxPZ7i49xEeiqXVcR+r79OZRWM
minisign -Vm test/example.txt -P RWSmKaOrT6m3TGwjwBovgOmlhSbyBUw3hyhnSOYruHXbJa36xHr8rq2M
```

### Create a signature
Expand All @@ -42,7 +58,12 @@ File.write("#{file_path}.minisig", signature.to_s)
irb -Ilib -rminisign
```

## Local Documentation
## Documentation

The documentation for this gem is published here:
https://www.rubydoc.info/gems/minisign/

or if working locally:

```
yard server --reload
Expand Down
15 changes: 12 additions & 3 deletions lib/minisign/public_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ class PublicKey
# @example
# Minisign::PublicKey.new('RWTg6JXWzv6GDtDphRQ/x7eg0LaWBcTxPZ7i49xEeiqXVcR+r79OZRWM')
def initialize(str)
@decoded = Base64.strict_decode64(str)
parts = str.split("\n")
@decoded = Base64.strict_decode64(parts.last)
@public_key = @decoded[10..]
@verify_key = Ed25519::VerifyKey.new(@public_key)
@untrusted_comment = if parts.length == 1
"minisign public key #{key_id}\n#{key_data}\n"
else
parts.first.split('untrusted comment: ').last
end
end

# @return [String] the key id
Expand Down Expand Up @@ -41,9 +47,12 @@ def verify(sig, message)
"Signature and comment signature verified\nTrusted comment: #{sig.trusted_comment}"
end

def key_data
Base64.strict_encode64("Ed#{@decoded[2..9]}#{@public_key}")
end

def to_s
data = Base64.strict_encode64("Ed#{@decoded[2..9]}#{@public_key}")
"untrusted comment: minisign public key #{key_id}\n#{data}\n"
"untrusted comment: #{@untrusted_comment}\n#{key_data}\n"
end

private
Expand Down
6 changes: 5 additions & 1 deletion spec/minisign/public_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Minisign::PublicKey do
before do
@pk = Minisign::PublicKey.new(File.read('test/minisign.pub').split("\n").pop)
@pk = Minisign::PublicKey.new(File.read('test/minisign.pub'))
@message = File.read('test/example.txt')
end
it 'verifies signatures' do
Expand Down Expand Up @@ -30,4 +30,8 @@
it 'can be written to a file' do
expect(@pk.to_s).to eq(File.read('test/minisign.pub'))
end
it 'regenerates an untrusted comment if not provided' do
@pk = Minisign::PublicKey.new('RWSmKaOrT6m3TGwjwBovgOmlhSbyBUw3hyhnSOYruHXbJa36xHr8rq2M')
expect(@pk.to_s).to match('minisign public key 4CB7A94FABA329A6')
end
end
2 changes: 1 addition & 1 deletion test/minisign.pub
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
untrusted comment: minisign public key 4CB7A94FABA329A6
untrusted comment: minisign public key 4CB7A94FABA329A6 yay
RWSmKaOrT6m3TGwjwBovgOmlhSbyBUw3hyhnSOYruHXbJa36xHr8rq2M

0 comments on commit 3845a82

Please sign in to comment.