Module: Sekrets::Blowfish

Extended by:
Blowfish
Included in:
Sekrets, Blowfish
Defined in:
lib/sekrets.rb

Instance Method Summary collapse

Instance Method Details

#cipher(mode, key, data) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/sekrets.rb', line 318

def cipher(mode, key, data)
  cipher =
    begin
      ::OpenSSL::Cipher.new('bf-cbc').send(mode)
    rescue StandardError => error
      raise if(
        @openssl_is_already_monkey_patched or
        (error.class.name != "OpenSSL::Cipher::CipherError") or
        (not defined?(::OpenSSL::Provider))
      )

      @openssl_is_already_monkey_patched = true
      ::OpenSSL::Provider.load("legacy")
      ::OpenSSL::Cipher.new('bf-cbc').send(mode)
    end

  cipher.key = ::Digest::SHA256.digest(key.to_s).slice(0, 16)

  cipher.update(data) << cipher.final
end

#cycle(key, data) ⇒ Object



347
348
349
# File 'lib/sekrets.rb', line 347

def cycle(key, data)
  decrypt(key, encrypt(key, data))
end

#decrypt(key, text) ⇒ Object



343
344
345
# File 'lib/sekrets.rb', line 343

def decrypt(key, text)
  cipher(:decrypt, key, text)
end

#encrypt(key, data) ⇒ Object



339
340
341
# File 'lib/sekrets.rb', line 339

def encrypt(key, data)
  cipher(:encrypt, key, data)
end

#recrypt(old_key, new_key, data) ⇒ Object



351
352
353
# File 'lib/sekrets.rb', line 351

def recrypt(old_key, new_key, data)
  encrypt(new_key, decrypt(old_key, data))
end