Class: RCS::Updater::SharedKey

Inherits:
Object
  • Object
show all
Defined in:
lib/rcs-common/updater/shared_key.rb

Instance Method Summary collapse

Constructor Details

#initializeSharedKey

Returns a new instance of SharedKey.



8
9
10
11
12
13
14
15
# File 'lib/rcs-common/updater/shared_key.rb', line 8

def initialize
  # Search for the signature file in some places
  [File.expand_path(Dir.pwd), "C:/RCS/DB", "C:/RCS/Collector"].each do |root|
    ["#{root}/config/rcs-updater.sig", "#{root}/config/certs/rcs-updater.sig"].each do |path|
      @path = path if File.exists?(path)
    end
  end
end

Instance Method Details

#decrypt(data) ⇒ Object



41
42
43
44
# File 'lib/rcs-common/updater/shared_key.rb', line 41

def decrypt(data)
  prepare_cipher(:decrypt)
  return @cipher.update(data) + @cipher.final
end

#decrypt_hash(data) ⇒ Object



50
51
52
# File 'lib/rcs-common/updater/shared_key.rb', line 50

def decrypt_hash(data)
  JSON.parse(decrypt(Base64.urlsafe_decode64(data)))
end

#encrypt(data) ⇒ Object



36
37
38
39
# File 'lib/rcs-common/updater/shared_key.rb', line 36

def encrypt(data)
  prepare_cipher(:encrypt)
  return @cipher.update(data) + @cipher.final
end

#encrypt_hash(hash) ⇒ Object



46
47
48
# File 'lib/rcs-common/updater/shared_key.rb', line 46

def encrypt_hash(hash)
  Base64.urlsafe_encode64(encrypt(hash.to_json))
end

#prepare_cipher(mode) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/rcs-common/updater/shared_key.rb', line 28

def prepare_cipher(mode)
  @cipher = OpenSSL::Cipher::AES.new(256, :CBC)
  mode == :encrypt ? @cipher.encrypt : @cipher.decrypt
  @cipher.padding = 1
  @cipher.key = read_key_from_file || raise("Missing or empty signature file")
  @cipher.iv = "\xBA\xF0\xC0Z\xD7\xE8~[TP\xFE\x88rW\xC8\xF4"
end

#read_key_from_fileObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/rcs-common/updater/shared_key.rb', line 17

def read_key_from_file
  return ENV['SIGNATURE'] if ENV['SIGNATURE']

  if @path
    key = File.read(@path)
    return key.empty? ? nil : key
  else
    return nil
  end
end