Skip to content

Commit

Permalink
signature.to_s (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl authored Feb 7, 2024
1 parent 3845a82 commit 408279d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/minisign/private_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ def ed25519_signing_key
# @param filename [String] The filename to be used in the trusted comment section
# @param message [String] The file's contents
# @param comment [String] An optional trusted comment to be included in the signature
# @return [String] the signature in the .minisig format that can be written to a file.
# @return [Minisign::Signature]
def sign(filename, message, comment = nil)
signature = ed25519_signing_key.sign(blake2b512(message))
trusted_comment = comment || "timestamp:#{Time.now.to_i}\tfile:#{filename}\thashed"
global_signature = ed25519_signing_key.sign("#{signature}#{trusted_comment}")
[
Minisign::Signature.new([
'untrusted comment: <arbitrary text>',
Base64.strict_encode64("ED#{@key_id.pack('C*')}#{signature}"),
"trusted comment: #{trusted_comment}",
Base64.strict_encode64(global_signature),
''
].join("\n")
].join("\n"))
end

def to_s
Expand Down
4 changes: 4 additions & 0 deletions lib/minisign/signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def signature
encoded_signature[10..]
end

def to_s
"#{@lines.join("\n")}\n"
end

private

def encoded_signature
Expand Down
6 changes: 2 additions & 4 deletions spec/minisign/private_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@
File.write("test/generated/#{@filename}", @message)
signature = @private_key.sign(@filename, @message, 'this is a trusted comment')
File.write("test/generated/#{@filename}.minisig", signature)
@signature = Minisign::Signature.new(signature)
@public_key = Minisign::PublicKey.new('RWSmKaOrT6m3TGwjwBovgOmlhSbyBUw3hyhnSOYruHXbJa36xHr8rq2M')
expect(@public_key.verify(@signature, @message)).to match('Signature and comment signature verified')
expect(@public_key.verify(signature, @message)).to match('Signature and comment signature verified')
end
it 'signs a file with an unencrypted key' do
@filename = 'unencrypted-key.txt'
Expand All @@ -100,9 +99,8 @@
@unencrypted_private_key = Minisign::PrivateKey.new(File.read('test/unencrypted.key'))
signature = @unencrypted_private_key.sign(@filename, @message)
File.write("test/generated/#{@filename}.minisig", signature)
@signature = Minisign::Signature.new(signature)
@public_key = Minisign::PublicKey.new('RWT/N/MXaBIWRAPzfdEKqVRq9txskjf5qh7EbqMLVHjkNTGFazO3zMw2')
expect(@public_key.verify(@signature, @message)).to match('Signature and comment signature verified')
expect(@public_key.verify(signature, @message)).to match('Signature and comment signature verified')
end
end
end
9 changes: 8 additions & 1 deletion spec/minisign/signature_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# frozen_string_literal: true

describe Minisign::Signature do
it 'has a key id' do
before do
@signature = Minisign::Signature.new(File.read('test/example.txt.minisig'))
end

it 'has a key id' do
expect(@signature.key_id).to eq('4CB7A94FABA329A6')
end

it 'can be written to a file' do
expect(@signature.to_s).to eq(File.read('test/example.txt.minisig'))
end
end

0 comments on commit 408279d

Please sign in to comment.