Module: CcipherFactory::Common

Instance Method Summary collapse

Instance Method Details

#attach_modeObject

attached mode Flag to indicate if the result of the operation should be part of the header/meta data directly return from the API



82
83
84
# File 'lib/ccipher_factory/helpers/common.rb', line 82

def attach_mode
  @attachMode = true
end

#cleanup_intOutputBufObject



35
36
37
38
39
# File 'lib/ccipher_factory/helpers/common.rb', line 35

def cleanup_intOutputBuf
  if not @intOutputBuf.nil?
    @intOutputBuf = nil
  end
end

#cleanup_intOutputFileObject



48
49
50
51
52
53
# File 'lib/ccipher_factory/helpers/common.rb', line 48

def cleanup_intOutputFile
  if not @intOutputFile.nil?
    @intOutputFile.close!
    @intOutputFile = nil
  end
end

#detach_modeObject



86
87
88
# File 'lib/ccipher_factory/helpers/common.rb', line 86

def detach_mode
  @attachMode = false
end

#disposeOutput(obj) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ccipher_factory/helpers/common.rb', line 55

def disposeOutput(obj)
  case obj
  when intOutputBuf
    cnt = 0
    len = @intOutputBuf.length
    loop do
      @intOutputBuf.rewind
      @intOutputBuf.write(SecureRandom.random_bytes(len))
      cnt += 1
      break if cnt >= 16
    end
    @intOutputBuf.rewind
    @intOutputBuf = nil
  when intOutputFile
    @intOutputFile.close!
  end
end

#intOutputBufObject



28
29
30
31
32
33
# File 'lib/ccipher_factory/helpers/common.rb', line 28

def intOutputBuf
  if @intOutputBuf.nil?
    @intOutputBuf = MemBuf.new
  end
  @intOutputBuf
end

#intOutputFileObject



41
42
43
44
45
46
# File 'lib/ccipher_factory/helpers/common.rb', line 41

def intOutputFile
  if @intOutputFile.nil?
    @intOutputFile = Tempfile.new
  end
  @intOutputFile
end

#is_attach_mode?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ccipher_factory/helpers/common.rb', line 90

def is_attach_mode?
  if @attachMode.nil? or not is_bool?(@attachMode)
    # default detach
    # The impact is mainly on huge data encrypt/decrypt
    # Returning the huge data through the API returned might
    # have undetermined behaviour
    false
  else
    @attachMode
  end
end

#is_output_given?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/ccipher_factory/helpers/common.rb', line 24

def is_output_given?
  not @output.nil?
end

#output(output) ⇒ Object

output section



11
12
13
14
# File 'lib/ccipher_factory/helpers/common.rb', line 11

def output(output)
  raise CcipherFactory::Error, "Output requires to support write(). StringIO is a good example." if output.nil? or not output.respond_to?(:write)
  @output = output
end

#output_objObject



16
17
18
# File 'lib/ccipher_factory/helpers/common.rb', line 16

def output_obj
  @output
end

#sanitize_symbol(sym, conv = :downcase) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ccipher_factory/helpers/common.rb', line 106

def sanitize_symbol(sym, conv = :downcase)
  if not_empty?(sym)
    case conv
    when :downcase
      sym.to_s.downcase.to_sym
    when :upcase
      sym.to_s.upcase.to_sym
    when :capitalize
      sym.to_s.capitalize.to_sym
    else
      sym
    end
  else
    sym
  end
end

#write_to_output(val) ⇒ Object



20
21
22
# File 'lib/ccipher_factory/helpers/common.rb', line 20

def write_to_output(val)
  @output.write(val) if not @output.nil? and not_empty?(val)
end