Module: Blowfish

Defined in:
lib/blowfish.rb,
lib/blowfish/block.rb,
lib/blowfish/padding.rb,
lib/blowfish/version.rb,
lib/blowfish/keys/init.rb,
lib/blowfish/keys/generate.rb

Overview

This is an implementation of Blowfish – symmetric block cipher designed in 1993 by Bruce Schneier.

Examples:

key = Blowfish::Key.generate('123456')
str = "abc"
encrypted_str = Blowfish.encrypt(str, key)
decrypted_str = Blowfish.decrypt(encrypted_str, key)

Defined Under Namespace

Modules: Block, Key, Padding Classes: Error, InvalidDataSizeError, UnknownPaddingModeError

Constant Summary collapse

VERSION =
'1.0.1'

Class Method Summary collapse

Class Method Details

.decrypt(data, key, options = {}) ⇒ Object

Decrypt string of data.

Parameters:

  • options (Hash) (defaults to: {})

    options of decryption

Options Hash (options):

  • :padding (Symbol) — default: :ansi_x923

    the padding mode, must be same as in encrypt



39
40
41
42
43
44
45
# File 'lib/blowfish.rb', line 39

def self.decrypt(data, key, options = {})
  res = ''
  0.step(data.size - 8, 8) do |i|
    res += Block.decrypt(data.byteslice(i..(i + 7)), key)
  end
  Padding.unpad(res, options[:padding])
end

.encrypt(data, key, options = {}) ⇒ Object

Encrypt string of data.

Parameters:

  • options (Hash) (defaults to: {})

    the options of encryption

Options Hash (options):

  • :padding (Symbol) — default: :ansi_x923

    the padding mode



26
27
28
29
30
31
32
33
# File 'lib/blowfish.rb', line 26

def self.encrypt(data, key, options = {})
  data = Padding.pad(data, options[:padding])
  res = ''
  0.step(data.size - 8, 8) do |i|
    res += Block.encrypt(data.byteslice(i..(i + 7)), key)
  end
  res
end