Class: Obfuscate::Crypt

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Crypt

New instance of Obfuscate::Crypt

Parameters:



28
29
30
31
# File 'lib/obfuscate/crypt.rb', line 28

def initialize( config )
  @crypt = Crypt::Blowfish.new( config.salt )
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



22
23
24
# File 'lib/obfuscate/crypt.rb', line 22

def config
  @config
end

#exec_configObject (readonly)

Returns the value of attribute exec_config.



23
24
25
# File 'lib/obfuscate/crypt.rb', line 23

def exec_config
  @exec_config
end

Instance Method Details

#clarify(text, override_mode = nil) ⇒ String

Clarify text

Parameters:

  • override_mode (Symbol) (defaults to: nil)

    to explicit set clarify mode to :string or :block

Returns:

  • (String)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/obfuscate/crypt.rb', line 62

def clarify( text, override_mode = nil )

  @exec_config = @config.apply( :mode => (override_mode || @config.mode) )
  obfuscated = text.to_s


  if @exec_config.encode
    obfuscated << "=" if @exec_config.remove_trailing_equal?
    obfuscated = Base64.urlsafe_decode64( obfuscated )
  end

  if @exec_config.mode == :string
    @crypt.decrypt_string( obfuscated )
  elsif @exec_config.mode == :block
    @crypt.decrypt_block( obfuscated ).strip
  else
    raise "Unsupport Mode"
  end
end

#obfuscate(text, override_mode = nil) ⇒ String

Obfuscate text

Parameters:

  • override_mode (Symbol) (defaults to: nil)

    to explicit set obfuscate mode to :string or :block

Returns:

  • (String)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/obfuscate/crypt.rb', line 37

def obfuscate( text, override_mode = nil )

  @exec_config = @config.apply( :mode => (override_mode || @config.mode) )

  obfuscated = nil
  if @exec_config.mode == :string
    obfuscated = @crypt.encrypt_string(text)
  elsif @exec_config.mode == :block
    obfuscated = @crypt.encrypt_block(text.to_s.ljust(8))
  else
    raise "Unsupport Mode"
  end

  if @exec_config.encode
    obfuscated = Base64.urlsafe_encode64(obfuscated).strip
    obfuscated = obfuscated.chomp("=") if @exec_config.remove_trailing_equal?
  end

  obfuscated
end