Module: ImageKitIo::Utils::Calculation

Included in:
Client
Defined in:
lib/imagekitio/utils/calculation.rb

Constant Summary collapse

DEFAULT_TIME_DIFF =
60 * 30

Class Method Summary collapse

Class Method Details

.get_authenticated_params(token, expire, private_key) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/imagekitio/utils/calculation.rb', line 26

def get_authenticated_params(token, expire, private_key)
  # return authenticated param
  default_expire = DateTime.now.strftime("%s").to_i + DEFAULT_TIME_DIFF
  token ||= SecureRandom.uuid
  expire ||= default_expire

  auth_params = {'token': token, 'expire': expire, 'signature': ""}
  unless private_key
    return nil
  end

  signature = OpenSSL::HMAC.hexdigest("SHA1", private_key, token.to_s + expire.to_s)
  auth_params[:token] = token
  auth_params[:expire] = expire
  auth_params[:signature] = signature
  auth_params
end

.hamming_distance(first, second) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/imagekitio/utils/calculation.rb', line 16

def hamming_distance(first, second)
  # Calculate Hamming distance between to hex string
  unless is_valid_hex(first) && is_valid_hex(second)
    raise ArgumentError, "Both argument should be hexadecimal"
  end
  a = first.to_i(16)
  b = second.to_i(16)
  (a ^ b).to_s(2).count("1")
end

.is_valid_hex(hex_string) ⇒ Object



11
12
13
14
# File 'lib/imagekitio/utils/calculation.rb', line 11

def is_valid_hex(hex_string)
  # checks if hexadecimal value is valid or not
  /^[[:xdigit:]]+$/ === hex_string
end