Class: XMLSERVICEPassword::Encrypt
- Inherits:
-
Object
- Object
- XMLSERVICEPassword::Encrypt
- Defined in:
- lib/password/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
-
.decode(item) ⇒ Object
base64 decode from web/yaml.
-
.decrypt(encoded, key = "*DEFAULT") ⇒ Object
password decrypt.
-
.encode(item) ⇒ Object
base64 encode for web/yaml usage.
-
.establish_key(key = "*DEFAULT") ⇒ Object
assure valid iv and key.
-
.gen_key ⇒ Object
generate a pass key.
-
.gen_password(plaintext, key = "*DEFAULT") ⇒ Object
generate a encrypted password.
-
.nest_yaml(config, key, value) ⇒ Object
recursive *_yaml: /path/thing.yml.
-
.parse_user_config(config) ⇒ Object
decrypt using password.yml (possible key.yml) (see top module).
-
.parse_yaml(config, yaml_file, yaml_key) ⇒ Object
parse nested yaml files.
-
.symbolize_keys(config) ⇒ Object
Converts all
config
keys to symbols.
Class Method Details
.decode(item) ⇒ Object
base64 decode from web/yaml
162 163 164 165 166 |
# File 'lib/password/password.rb', line 162 def self.decode(item) encrypted = URI.unescape(item) decoded = Base64.decode64(encrypted) decoded end |
.decrypt(encoded, key = "*DEFAULT") ⇒ Object
password decrypt
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/password/password.rb', line 101 def self.decrypt(encoded, key = "*DEFAULT") XMLSERVICEPassword::Encrypt.establish_key(key); decoded = XMLSERVICEPassword::Encrypt.decode(encoded) cipher = OpenSSL::Cipher::Cipher.new(PWD_TYPE) cipher.decrypt cipher.key = @xml_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
155 156 157 158 159 |
# File 'lib/password/password.rb', line 155 def self.encode(item) encrypted = Base64.encode64(item) encoded = URI.escape(encrypted) encoded end |
.establish_key(key = "*DEFAULT") ⇒ Object
assure valid iv and key
146 147 148 149 150 151 152 |
# File 'lib/password/password.rb', line 146 def self.establish_key(key = "*DEFAULT") if key && key != '*DEFAULT' @xml_pwdkey = key else @xml_pwdkey = PWD_KEY end end |
.gen_key ⇒ Object
generate a pass key
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/password/password.rb', line 113 def self.gen_key() XMLSERVICEPassword::Encrypt.establish_key("*DEFAULT"); cipher = OpenSSL::Cipher::Cipher.new(PWD_TYPE) cipher.encrypt cipher.key = @xml_pwdkey all = " " for i in 1..3 cipher.iv = iv = cipher.random_iv raw = XMLSERVICEPassword::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
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/password/password.rb', line 129 def self.gen_password(plaintext, key = "*DEFAULT") XMLSERVICEPassword::Encrypt.establish_key(key); cipher = OpenSSL::Cipher::Cipher.new(PWD_TYPE) cipher.encrypt cipher.key = @xml_pwdkey cipher.iv = iv = cipher.random_iv encrypted = cipher.update(plaintext) + cipher.final encrypted = iv + encrypted # Send along the IV encoded = XMLSERVICEPassword::Encrypt.encode(encrypted) encoded end |
.nest_yaml(config, key, value) ⇒ Object
recursive *_yaml: /path/thing.yml
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/password/password.rb', line 179 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 = XMLSERVICEPassword::Encrypt.parse_yaml(config, value, config[:username]) end # recursive *_yaml: /path/thing.yml else config = XMLSERVICEPassword::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)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/password/password.rb', line 83 def self.parse_user_config(config) if config.has_key?(:username) if config.has_key?(:pwd_yaml) # password.yml file config = XMLSERVICEPassword::Encrypt.parse_yaml(config, config[:pwd_yaml].to_s, config[:username].to_s) config = XMLSERVICEPassword::Encrypt.symbolize_keys(config) end if config.has_key?(:pwd_enc) if !config.has_key?(:pwd_key) config[:pwd_key] = "*DEFAULT" end config[:password] = XMLSERVICEPassword::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
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/password/password.rb', line 200 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 = XMLSERVICEPassword::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 = XMLSERVICEPassword::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
169 170 171 172 173 174 175 |
# File 'lib/password/password.rb', line 169 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 |