-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
executable file
·33 lines (25 loc) · 908 Bytes
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'net/http'
require 'uri'
require 'gpgme'
require 'dnsruby'
require 'base64'
# Retrieve signature
ret = Dnsruby::Resolver.new.query('security.2fa.directory', 'CERT')
# Import public key(s) from CERT RR
imports = GPGME::Key.import(ret.answer.rrsets[0][0].cert).imports
# Fetch fingerprints from imported key(s)
fingerprints = imports.map(&:fpr)
# Specify which file to download
filename = ARGV[0]
# Fetch file
res = Net::HTTP.get_response(URI("https://api.2fa.directory/v3/#{filename}.sig")).body
# Decipher signed data file
data = GPGME::Crypto.new.verify(GPGME::Data.new(res)) do |sig|
# Verify that the same key as before signed the file
raise 'Invalid key' unless sig.valid?
raise 'Mismatching key' unless fingerprints.include? sig.fingerprint
end
# Write verified data to new file
File.open(filename, 'w') { |file| file.write data }