Class: Epithet::Config

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

Overview

Class for passing around preset configs. See Epithet::configure for options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Config

Returns a new instance of Config.

Raises:

  • (ArgumentError)


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/epithet.rb', line 177

def initialize(opts = {})
  opts = opts.dup
  @separator = -String(opts.delete(:separator) { '_' })
  @context = -String(opts.delete(:context))
  alphabet = String(opts.delete(:alphabet) { Block58::Alphabet })
  @cipher = -(opts.delete(:cipher) || 'aes-256-ecb').downcase
  @digest = -(opts.delete(:digest) || 'sha256').downcase
  keygen, passphrase, scrypt = %i[keygen passphrase scrypt].map { opts.delete it }

  cipher = OpenSSL::Cipher.new(@cipher)
  raise ArgumentError, 'separator intersects alphabet' if @separator.bytes.intersect?(alphabet.bytes)
  raise ArgumentError, "#{@cipher} not a 128-bit block cipher" if cipher.block_size != 16
  raise ArgumentError, "#{@cipher} requires an IV/nonce" if cipher.iv_len != 0
  raise ArgumentError, "#{@digest} produces < 64-bit digest" if OpenSSL::Digest.new(@digest).digest_length < 8
  raise ArgumentError, 'use keygen: or passphrase:, not both' if keygen && (passphrase || scrypt)
  raise ArgumentError, 'one of passphrase: or keygen: is required' unless keygen || passphrase
  raise ArgumentError, "unused option(s) #{opts.keys}" unless opts.empty?

  @codec = Block58.build(cipher.block_size, alphabet:)
  @keygen = keygen || Keygen.new(passphrase:, digest: @digest, scrypt:)
  freeze
end

Instance Attribute Details

#cipherObject (readonly)

:nodoc:



175
176
177
# File 'lib/epithet.rb', line 175

def cipher
  @cipher
end

#codecObject (readonly)

:nodoc:



175
176
177
# File 'lib/epithet.rb', line 175

def codec
  @codec
end

#contextObject (readonly)

:nodoc:



175
176
177
# File 'lib/epithet.rb', line 175

def context
  @context
end

#digestObject (readonly)

:nodoc:



175
176
177
# File 'lib/epithet.rb', line 175

def digest
  @digest
end

#keygenObject (readonly)

:nodoc:



175
176
177
# File 'lib/epithet.rb', line 175

def keygen
  @keygen
end

#separatorObject (readonly)

:nodoc:



175
176
177
# File 'lib/epithet.rb', line 175

def separator
  @separator
end