class Diffcrypt::Encryptor

Constants

DEFAULT_CIPHER

Public Class Methods

generate_key(cipher = DEFAULT_CIPHER) click to toggle source
# File lib/diffcrypt/encryptor.rb, line 17
def self.generate_key(cipher = DEFAULT_CIPHER)
  SecureRandom.hex(ActiveSupport::MessageEncryptor.key_len(cipher))
end
new(key, cipher: DEFAULT_CIPHER) click to toggle source
# File lib/diffcrypt/encryptor.rb, line 21
def initialize(key, cipher: DEFAULT_CIPHER)
  @key = key
  @cipher = cipher
  @encryptor ||= ActiveSupport::MessageEncryptor.new([key].pack('H*'), cipher: cipher)
end

Public Instance Methods

decrypt(contents) click to toggle source

@param [String] contents The raw YAML string to be encrypted

# File lib/diffcrypt/encryptor.rb, line 28
def decrypt(contents)
  yaml = YAML.safe_load contents
  decrypted = decrypt_hash yaml['data']
  YAML.dump decrypted
end
decrypt_hash(data) click to toggle source

@param [Hash] data @return [Hash]

# File lib/diffcrypt/encryptor.rb, line 36
def decrypt_hash(data)
  data.each do |key, value|
    data[key] = if value.is_a?(Hash) || value.is_a?(Array)
                  decrypt_hash(value)
                else
                  decrypt_string value
                end
  end
  data
end
decrypt_string(value) click to toggle source

@param [String] value The encrypted value that needs decrypting @return [String]

# File lib/diffcrypt/encryptor.rb, line 95
def decrypt_string(value)
  @encryptor.decrypt_and_verify value
end
encrypt(contents, original_encrypted_contents = nil, cipher: nil) click to toggle source

@param [String] contents The raw YAML string to be encrypted @param [String, nil] original_encrypted_contents The original (encrypted) content to determine which keys have changed @return [String]

# File lib/diffcrypt/encryptor.rb, line 50
def encrypt(contents, original_encrypted_contents = nil, cipher: nil)
  data = encrypt_data contents, original_encrypted_contents
  YAML.dump(
    'client' => "diffcrypt-#{Diffcrypt::VERSION}",
    'cipher' => cipher || @cipher,
    'data' => data,
  )
end
encrypt_data(contents, original_encrypted_contents = nil) click to toggle source

@param [String] contents The raw YAML string to be encrypted @param [String, nil] original_encrypted_contents The original (encrypted) content to determine which keys have changed @return [Hash] Encrypted hash containing the data

# File lib/diffcrypt/encryptor.rb, line 62
def encrypt_data(contents, original_encrypted_contents = nil)
  yaml = YAML.safe_load contents
  original_yaml = original_encrypted_contents ? YAML.safe_load(original_encrypted_contents)['data'] : nil
  encrypt_values yaml, original_yaml
end
encrypt_string(value) click to toggle source

@param [String] value Plain text string that needs encrypting @return [String]

# File lib/diffcrypt/encryptor.rb, line 70
def encrypt_string(value)
  @encryptor.encrypt_and_sign value
end
encrypt_values(data, original_data = nil) click to toggle source

TODO: Fix the complexity of this method rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity @param [Hash] keys @return [Hash]

# File lib/diffcrypt/encryptor.rb, line 78
def encrypt_values(data, original_data = nil)
  data.each do |key, value|
    original_encrypted_value = original_data ? original_data[key] : nil
    data[key] = if value.is_a?(Hash) || value.is_a?(Array)
                  encrypt_values(value, original_encrypted_value)
                else
                  original_decrypted_value = original_encrypted_value ? decrypt_string(original_encrypted_value) : nil
                  key_changed = original_decrypted_value.nil? || original_decrypted_value != value
                  key_changed ? encrypt_string(value) : original_encrypted_value
                end
  end
  data.sort.to_h
end