-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
98 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ Metrics/BlockLength: | |
IgnoredMethods: ['describe', 'context'] | ||
AllCops: | ||
NewCops: enable | ||
TargetRubyVersion: 2.7.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module Minisign | ||
# Parse ed25519 verify key from minisign public key | ||
class PublicKey | ||
# Parse the ed25519 verify key from the minisign public key | ||
# | ||
# @param str [String] The minisign public key | ||
# @example | ||
# Minisign::PublicKey.new('RWTg6JXWzv6GDtDphRQ/x7eg0LaWBcTxPZ7i49xEeiqXVcR+r79OZRWM') | ||
def initialize(str) | ||
@decoded = Base64.strict_decode64(str) | ||
@public_key = @decoded[10..] | ||
@verify_key = Ed25519::VerifyKey.new(@public_key) | ||
end | ||
|
||
# @return [String] the key id | ||
# @example | ||
# Minisign::PublicKey.new('RWTg6JXWzv6GDtDphRQ/x7eg0LaWBcTxPZ7i49xEeiqXVcR+r79OZRWM').key_id | ||
# #=> "E86FECED695E8E0" | ||
def key_id | ||
@decoded[2..9].bytes.map { |c| c.to_s(16) }.reverse.join.upcase | ||
end | ||
|
||
# Verify a message's signature | ||
# | ||
# @param sig [Minisign::Signature] | ||
# @param message [String] the content that was signed | ||
# @return [String] the trusted comment | ||
# @raise Ed25519::VerifyError on invalid signatures | ||
# @raise RuntimeError on tampered trusted comments | ||
def verify(sig, message) | ||
blake = OpenSSL::Digest.new('BLAKE2b512') | ||
ensure_matching_key_ids(sig.key_id, key_id) | ||
@verify_key.verify(sig.signature, blake.digest(message)) | ||
begin | ||
@verify_key.verify(sig.trusted_comment_signature, sig.signature + sig.trusted_comment) | ||
rescue Ed25519::VerifyError | ||
raise 'Comment signature verification failed' | ||
end | ||
"Signature and comment signature verified\nTrusted comment: #{sig.trusted_comment}" | ||
end | ||
|
||
private | ||
|
||
def ensure_matching_key_ids(key_id1, key_id2) | ||
raise "Signature key id is #{key_id1}\nbut the key id in the public key is #{key_id2}" unless key_id1 == key_id2 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module Minisign | ||
# Parse a .minisig file's contents | ||
class Signature | ||
# @param str [String] The contents of the .minisig file | ||
# @example | ||
# Minisign::Signature.new(File.read('test/example.txt.minisig')) | ||
def initialize(str) | ||
@lines = str.split("\n") | ||
end | ||
|
||
# @return [String] the key id | ||
# @example | ||
# Minisign::Signature.new(File.read('test/example.txt.minisig')).key_id | ||
# #=> "E86FECED695E8E0" | ||
def key_id | ||
encoded_signature[2..9].bytes.map { |c| c.to_s(16) }.reverse.join.upcase | ||
end | ||
|
||
# @return [String] the trusted comment | ||
# @example | ||
# Minisign::Signature.new(File.read('test/example.txt.minisig')).trusted_comment | ||
# #=> "timestamp:1653934067\tfile:example.txt\thashed" | ||
def trusted_comment | ||
@lines[2].split('trusted comment: ')[1] | ||
end | ||
|
||
def trusted_comment_signature | ||
Base64.decode64(@lines[3]) | ||
end | ||
|
||
# @return [String] the signature | ||
def signature | ||
encoded_signature[10..] | ||
end | ||
|
||
private | ||
|
||
def encoded_signature | ||
Base64.decode64(@lines[1]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters