From 3845a825bbacbe8107fcaf80272f998b845b884e Mon Sep 17 00:00:00 2001 From: Jesse Shawl Date: Wed, 7 Feb 2024 06:49:12 -0600 Subject: [PATCH] support public key from string or from file (#14) --- README.md | 27 ++++++++++++++++++++++++--- lib/minisign/public_key.rb | 15 ++++++++++++--- spec/minisign/public_key_spec.rb | 6 +++++- test/minisign.pub | 2 +- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e50cab0..484c82f 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -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 diff --git a/lib/minisign/public_key.rb b/lib/minisign/public_key.rb index afa51d7..0a0c852 100644 --- a/lib/minisign/public_key.rb +++ b/lib/minisign/public_key.rb @@ -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 @@ -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 diff --git a/spec/minisign/public_key_spec.rb b/spec/minisign/public_key_spec.rb index 1bda9ef..d003de0 100644 --- a/spec/minisign/public_key_spec.rb +++ b/spec/minisign/public_key_spec.rb @@ -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 @@ -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 diff --git a/test/minisign.pub b/test/minisign.pub index fce7ebe..7fd845f 100644 --- a/test/minisign.pub +++ b/test/minisign.pub @@ -1,2 +1,2 @@ -untrusted comment: minisign public key 4CB7A94FABA329A6 +untrusted comment: minisign public key 4CB7A94FABA329A6 yay RWSmKaOrT6m3TGwjwBovgOmlhSbyBUw3hyhnSOYruHXbJa36xHr8rq2M