Class: IBMDBPassword::Encrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/ibm_db_password.rb

Overview

32 characters required of course PWD_KEY should not be in script but this is an example (people read)

Class Method Summary collapse

Class Method Details

.decode(item) ⇒ Object

base64 decode from web/yaml



146
147
148
149
150
# File 'lib/active_record/connection_adapters/ibm_db_password.rb', line 146

def self.decode(item)
  encrypted = URI.unescape(item)
  decoded = Base64.decode64(encrypted)
  decoded
end

.decrypt(encoded, key = "*DEFAULT") ⇒ Object

password decrypt



85
86
87
88
89
90
91
92
93
94
# File 'lib/active_record/connection_adapters/ibm_db_password.rb', line 85

def self.decrypt(encoded, key = "*DEFAULT")
  IBMDBPassword::Encrypt.establish_key(key);
  decoded = IBMDBPassword::Encrypt.decode(encoded)
  cipher = OpenSSL::Cipher::Cipher.new(PWD_TYPE)
  cipher.decrypt
  cipher.key = @pwdkey
  cipher.iv = decoded.slice!(0,16) # Remove the IV from the encrypted data
  decrypted = cipher.update(decoded) + cipher.final
  decrypted 
end

.encode(item) ⇒ Object

base64 encode for web/yaml usage



139
140
141
142
143
# File 'lib/active_record/connection_adapters/ibm_db_password.rb', line 139

def self.encode(item)
  encrypted = Base64.encode64(item)
  encoded = URI.escape(encrypted)
  encoded
end

.establish_key(key = "*DEFAULT") ⇒ Object

assure valid iv and key



130
131
132
133
134
135
136
# File 'lib/active_record/connection_adapters/ibm_db_password.rb', line 130

def self.establish_key(key = "*DEFAULT")
  if key && key != '*DEFAULT'
    @pwdkey = key
  else
    @pwdkey = PWD_KEY
  end
end

.gen_keyObject

generate a pass key



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_record/connection_adapters/ibm_db_password.rb', line 97

def self.gen_key()
  IBMDBPassword::Encrypt.establish_key("*DEFAULT");
  cipher = OpenSSL::Cipher::Cipher.new(PWD_TYPE)
  cipher.encrypt
  cipher.key = @pwdkey
  all = " "
  for i in 1..3
    cipher.iv = iv = cipher.random_iv
    raw = IBMDBPassword::Encrypt.encode(iv)
    all << raw.to_s
  end
  encode = all.slice!(1..32)
  encode
end

.gen_password(plaintext, key = "*DEFAULT") ⇒ Object

generate a encrypted password



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/active_record/connection_adapters/ibm_db_password.rb', line 113

def self.gen_password(plaintext, key = "*DEFAULT")
  IBMDBPassword::Encrypt.establish_key(key);
  cipher = OpenSSL::Cipher::Cipher.new(PWD_TYPE)
  cipher.encrypt
  cipher.key = @pwdkey
  cipher.iv = iv = cipher.random_iv
  encrypted = cipher.update(plaintext) + cipher.final
  encrypted = iv + encrypted # Send along the IV
  encoded = IBMDBPassword::Encrypt.encode(encrypted)
  encoded
end

.nest_yaml(config, key, value) ⇒ Object

recursive *_yaml: /path/thing.yml



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/active_record/connection_adapters/ibm_db_password.rb', line 162

def self.nest_yaml(config,key,value)
  if key.include? "_yaml" or (value != nil and value.instance_of? String and value.to_s.include? ".yml")
    # recursive /path/password.yml
    if value.include? "password.yml"
      if !config.has_key?(:username)
        if config.has_key?("username")
          config[:username] = config["username"]
        end
      end
      if config.has_key?(:username)
        config = IBMDBPassword::Encrypt.parse_yaml(config, value, config[:username])
      end
    # recursive *_yaml: /path/thing.yml
    else
      config = IBMDBPassword::Encrypt.parse_yaml(config, value, "*ALL")
    end
  end
  config
end

.parse_user_config(config) ⇒ Object

decrypt using password.yml (possible key.yml) (see top module)



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_record/connection_adapters/ibm_db_password.rb', line 67

def self.parse_user_config(config)
  if config.has_key?(:username)
    if config.has_key?(:pwd_yaml)
      # password.yml file
      config = IBMDBPassword::Encrypt.parse_yaml(config, config[:pwd_yaml].to_s, config[:username].to_s)
      config = IBMDBPassword::Encrypt.symbolize_keys(config)
    end
    if config.has_key?(:pwd_enc)
      if !config.has_key?(:pwd_key)
        config[:pwd_key] = "*DEFAULT"
      end
      config[:password] = IBMDBPassword::Encrypt.decrypt(config[:pwd_enc].to_s, config[:pwd_key].to_s)
    end
  end
  config
end

.parse_yaml(config, yaml_file, yaml_key) ⇒ Object

parse nested yaml files



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/active_record/connection_adapters/ibm_db_password.rb', line 183

def self.parse_yaml(config, yaml_file, yaml_key)
  rfile = Pathname.new(yaml_file)
  if rfile
    f = open(rfile.to_s)
    doc = YAML::load_stream( f )
    doc.each do |key, value|
     key.each do |key0, value0|
      # recursive *_yaml
      config = IBMDBPassword::Encrypt.nest_yaml(config,key0,value0)
      # take everything
      if yaml_key == "*ALL"
        config[key0] = value0
      # found target key="username"
      elsif yaml_key == key0
        value0.each do |key1, value1|
          # recursive *_yaml
          config = IBMDBPassword::Encrypt.nest_yaml(config,key1,value1)
          # take everything
          config[key1] = value1
        end
      end
     end
    end
  end
  config
end

.symbolize_keys(config) ⇒ Object

Converts all config keys to symbols



153
154
155
156
157
158
159
# File 'lib/active_record/connection_adapters/ibm_db_password.rb', line 153

def self.symbolize_keys(config)
  # config = config.symbolize_keys
  config.keys.each do |key|
    config[(key.to_sym rescue key) || key] = config.delete(key)
  end
  config
end