3 @public_repo='./public/'
5 @private_repo_base='./private/'
9 @names=%w[cercei tyrion jaime tywin ned aria bran]
15 system('rm', '-rf', "./#{dir}")
23 def spawn_publ_importer(gpg_publ_cmd)
26 spawn(gpg_publ_cmd, :in => r)
32 def gen_key (gpg_user_cmd_base, name)
35 pid = spawn(gpg_user_cmd_base + " --gen-key -", :in => r)
38 w.puts "%echo Generating #{name}"
39 w.puts "Key-Type: RSA"
40 w.puts "Key-Length: 2048"
41 w.puts "Name-Real: #{name}"
42 w.puts "Name-Email: #{name}@westeros.com"
50 def export_key (gpg_user_cmd_base, sink_pipe)
51 system(gpg_user_cmd_base + " --export", :out => sink_pipe)
55 reinit_dir @public_repo
57 reinit_dir @private_repo_base
59 sink_pipe = spawn_publ_importer "gpg --batch --homedir #{@public_repo} --import"
62 private_repo = @private_repo_base + name
64 reinit_dir private_repo
66 gpg_user_cmd_base = "gpg --batch --homedir ./#{private_repo}"
68 gen_key gpg_user_cmd_base, name
70 export_key gpg_user_cmd_base, sink_pipe
78 # returns an array of key IDs
82 pid = spawn("gpg --batch --with-colons -k --homedir #{@public_repo}", :out => w)
88 line_data = $_.split(":")
89 if line_data[0] == 'pub'
90 key_list << line_data[4]
104 spawn('sha256sum -b', :in => r)
110 def generate_bins (key_sink)
111 def generate_key_chunk
112 IO.read('/dev/urandom', 512)
115 def write_bin (number, chunk, keys)
116 bin_name = @bin_dir + '%02X' % number
117 receivers = keys.map { |k| "-r #{k}"}.join(' ')
121 gpg_call = "gpg --batch --always-trust --homedir #{@public_repo} #{receivers} -o #{bin_name} -e"
123 pid = spawn(gpg_call, :in => r)
133 def combine_keys (&block)
134 gap_target_count = @security_level - 1
137 raise "Invlid security level" unless gap_target_count > 0
138 raise "Security level is too high" unless gap_target_count < key_list.size
140 list_keys.combination(key_list.size - gap_target_count, &block)
148 chunk = generate_key_chunk
149 key_sink.write(chunk)
150 write_bin(num, chunk, c)
159 def decrypt_key (rings)
161 # enumerate all bins in alphabetical order
162 (Dir.entries(@bin_dir) - ['.', '..']).sort.map do |x|
169 chunks = bin_list.clone
171 todo_count = bin_list.size
174 break if todo_count <= 0
176 bin_list.each_index do |bi|
177 next if bin_list[bi] == nil
181 pid = spawn("gpg --batch --homedir #{ring} -d #{bin_list[bi]}", :out => w, :err => '/dev/null')
188 if $?.exitstatus == 0
196 raise "Cannot decrypt some of the bins: #{bin_list.inspect}" if todo_count > 0
203 #generate_bins (spawn_sha_sink)
211 sink = spawn_sha_sink