Class: Sekrit::Encoder

Inherits:
Object show all
Defined in:
lib/sekrit/encoder.rb

Instance Method Summary collapse

Constructor Details

#initialize(password: String) ⇒ Encoder

Returns a new instance of Encoder.



9
10
11
# File 'lib/sekrit/encoder.rb', line 9

def initialize(password: String)
  @password = password
end

Instance Method Details

#encode(string: String) ⇒ Object

[bsarrazin] July 20th, 2019 Stolen from Fastlane, as I am not an expert on encryption/decryption. If you have experience and want to help, please submit a pull request :)

We encrypt with MD5 because that was the most common default value in older fastlane versions which used the local OpenSSL installation A more secure key and IV generation is needed in the future, IV should be randomly generated and provided unencrypted salt should be randomly generated and provided unencrypted (like in the current implementation) key should be generated with OpenSSL::KDF::pbkdf2_hmac with properly chosen parameters Short explanation about salt and IV: https://stackoverflow.com/a/1950674/6324550



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sekrit/encoder.rb', line 22

def encode(string: String)

  salt = SecureRandom.random_bytes(8)

  cipher = OpenSSL::Cipher.new('AES-256-CBC')
  cipher.encrypt
  cipher.pkcs5_keyivgen(@password, salt, 1, "MD5")
  data = "Salted__" + salt + cipher.update(string) + cipher.final

  Base64.encode64(data)
end