Class: Paperback::Preparer

Inherits:
Object
  • Object
show all
Defined in:
lib/paperback/preparer.rb

Overview

Class wrapping functions to prepare data for paperback storage, including QR code and sixword encoding.

Constant Summary collapse

PassChars =
[*'a'..'z', *'A'..'Z', *'0'..'9'].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename:, encrypt: true, qr_base64: false, qr_level: nil, comment: nil, passphrase_file: nil) ⇒ Preparer


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/paperback/preparer.rb', line 21

def initialize(filename:, encrypt: true, qr_base64: false, qr_level: nil,
               comment: nil, passphrase_file: nil)

  log.debug('Preparer#initialize')

  log.info("Reading #{filename.inspect}")
  plain_data = File.read(filename)

  log.debug("Read #{plain_data.bytesize} bytes")

  @encrypt = encrypt

  if encrypt
    @data = self.class.gpg_encrypt(filename: filename, password: passphrase)
  else
    @data = plain_data
  end
  @sha256 = Digest::SHA256.hexdigest(plain_data)

  @qr_base64 = qr_base64
  @qr_level = qr_level

  @passphrase_file = passphrase_file

  @labels = {}
  @labels['Filename'] = filename
  @labels['Backed up'] = Time.now.to_s

  stat = File.stat(filename)
  @labels['Mtime'] = stat.mtime
  @labels['Bytes'] = plain_data.bytesize
  @labels['Comment'] = comment if comment

  @labels['SHA256'] = Digest::SHA256.hexdigest(plain_data)

  @document = Paperback::Document.new
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data


15
16
17
# File 'lib/paperback/preparer.rb', line 15

def data
  @data
end

#encryptObject (readonly)

Returns the value of attribute encrypt


18
19
20
# File 'lib/paperback/preparer.rb', line 18

def encrypt
  @encrypt
end

#labelsObject (readonly)

Returns the value of attribute labels


16
17
18
# File 'lib/paperback/preparer.rb', line 16

def labels
  @labels
end

#passphrase_fileObject (readonly)

Returns the value of attribute passphrase_file


19
20
21
# File 'lib/paperback/preparer.rb', line 19

def passphrase_file
  @passphrase_file
end

#qr_base64Object (readonly)

Returns the value of attribute qr_base64


17
18
19
# File 'lib/paperback/preparer.rb', line 17

def qr_base64
  @qr_base64
end

Class Method Details

.gpg_ascii_dearmor(data) ⇒ Object


153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/paperback/preparer.rb', line 153

def self.gpg_ascii_dearmor(data)
  cmd = %w[gpg --batch --dearmor]
  out = nil

  log.debug('+ ' + cmd.join(' '))
  Subprocess.check_call(cmd, stdin: Subprocess::PIPE,
                        stdout: Subprocess::PIPE) do |p|
    out, _err = p.communicate(data)
  end

  out
end

.gpg_ascii_enarmor(data, strip_comments: true) ⇒ Object


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/paperback/preparer.rb', line 136

def self.gpg_ascii_enarmor(data, strip_comments: true)
  cmd = %w[gpg --batch --enarmor]
  out = nil

  log.debug('+ ' + cmd.join(' '))
  Subprocess.check_call(cmd, stdin: Subprocess::PIPE,
                        stdout: Subprocess::PIPE) do |p|
    out, _err = p.communicate(data)
  end

  if strip_comments
    out = out.each_line.select { |l| !l.start_with?('Comment: ') }.join
  end

  out
end

.gpg_encrypt(filename:, password:) ⇒ Object


121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/paperback/preparer.rb', line 121

def self.gpg_encrypt(filename:, password:)
  cmd = %w[
    gpg -c -o - --batch --cipher-algo aes256 --passphrase-fd 0 --
  ] + [filename]
  out = nil

  log.debug('+ ' + cmd.join(' '))
  Subprocess.check_call(cmd, stdin: Subprocess::PIPE,
                        stdout: Subprocess::PIPE) do |p|
    out, _err = p.communicate(password)
  end

  out
end

.logObject


62
63
64
# File 'lib/paperback/preparer.rb', line 62

def self.log
  @log ||= Paperback.class_log(self)
end

.random_passphrase(entropy_bits: 256, char_set: PassChars) ⇒ Object


109
110
111
112
113
114
# File 'lib/paperback/preparer.rb', line 109

def self.random_passphrase(entropy_bits: 256, char_set: PassChars)
  chars_needed = (entropy_bits / Math.log2(char_set.length)).ceil
  (0...chars_needed).map {
    PassChars.fetch(SecureRandom.random_number(char_set.length))
  }.join
end

.truncated_sha256(content) ⇒ Object

Compute a truncated SHA256 digest


117
118
119
# File 'lib/paperback/preparer.rb', line 117

def self.truncated_sha256(content)
  Digest::SHA256.hexdigest(content)[0...16]
end

Instance Method Details

#logObject


59
60
61
# File 'lib/paperback/preparer.rb', line 59

def log
  @log ||= Paperback.class_log(self.class)
end

#passphraseObject


102
103
104
105
# File 'lib/paperback/preparer.rb', line 102

def passphrase
  raise "Can't have passphrase without encrypt" unless encrypt
  @passphrase ||= self.class.random_passphrase
end

#render(output_filename:, extra_draw_opts: {}) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/paperback/preparer.rb', line 66

def render(output_filename:, extra_draw_opts: {})
  log.debug('Preparer#render')

  opts = {
    labels: labels,
    qr_code: qr_code,
    sixword_lines: sixword_lines,
    sixword_bytes: data.bytesize,
  }

  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,
               0400) do |f|
        f.write(passphrase)
      end
      log.info("Wrote passphrase to #{passphrase_file.inspect}")
    end
  end

  opts.merge!(extra_draw_opts)

  @document.render(output_file: output_filename, draw_opts: opts)

  log.info('Render complete')

  if encrypt
    puts "SHA256(passphrase)[0...16]: " + opts.fetch(:passphrase_sha)
    if !passphrase_file
      puts "Passphrase: #{passphrase}"
    end
  end
end