lib/paperback/preparer.rb in paperback-0.0.3 vs lib/paperback/preparer.rb in paperback-0.0.4

- old
+ new

@@ -17,11 +17,11 @@ attr_reader :qr_base64 attr_reader :encrypt attr_reader :passphrase_file def initialize(filename:, encrypt: true, qr_base64: false, qr_level: nil, - comment: nil, passphrase_file: nil) + comment: nil, passphrase_file: nil, include_base64: true) log.debug('Preparer#initialize') log.info("Reading #{filename.inspect}") plain_data = File.read(filename) @@ -40,10 +40,12 @@ @qr_base64 = qr_base64 @qr_level = qr_level @passphrase_file = passphrase_file + @include_base64 = !!include_base64 + @labels = {} @labels['Filename'] = filename @labels['Backed up'] = Time.now.to_s stat = File.stat(filename) @@ -71,10 +73,15 @@ qr_code: qr_code, sixword_lines: sixword_lines, sixword_bytes: data.bytesize, } + if include_base64? + opts[:base64_content] = base64_content + opts[:base64_bytes] = data.bytesize + end + if encrypt opts[:passphrase_sha] = self.class.truncated_sha256(passphrase) opts[:passphrase_len] = passphrase.length if passphrase_file File.open(passphrase_file, File::CREAT|File::EXCL|File::WRONLY, @@ -89,12 +96,17 @@ @document.render(output_file: output_filename, draw_opts: opts) log.info('Render complete') - if encrypt && !passphrase_file - puts "Passphrase: #{passphrase}" + if encrypt + puts "SHA256(passphrase)[0...16]: " + opts.fetch(:passphrase_sha) + if !passphrase_file + puts "Passphrase: #{passphrase}" + end + else + log.info('Content was not encrypted') end end def passphrase raise "Can't have passphrase without encrypt" unless encrypt @@ -158,10 +170,17 @@ end out end + # Whether to add the Base64 encoding to the generated document. + # + # @return [Boolean] + def include_base64? + !!@include_base64 + end + private def qr_code @qr_code ||= qr_code! end @@ -169,17 +188,11 @@ def qr_code! log.info('Generating QR code') # Base64 encode data prior to QR encoding as requested if qr_base64 - if encrypt - # If data is already GPG encrypted, use GPG's base64 armor - input = self.class.gpg_ascii_enarmor(data) - else - # Otherwise do plain Base64 - input = Base64.encode64(data) - end + input = base64_content else input = data end # If QR level not specified, pick highest level of redundancy possible @@ -202,7 +215,17 @@ log.info('Encoding with Sixword') @sixword_lines ||= Sixword.pad_encode_to_sentences(data).map(&:downcase) end + def base64_content + log.debug('Encoding with Base64') + if encrypt + # If data is already GPG encrypted, use GPG's base64 armor + self.class.gpg_ascii_enarmor(data) + else + # Otherwise do plain Base64 + Base64.encode64(data) + end + end end end