require 'base64' require 'cgi' require 'hmac-sha1' module App42 module Client class RestUtil # # Finds the current time # # @return time format # # def get_timestamp_utc timestamp = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ") return timestamp.freeze end # # It signs the request that has to be sent in the Hmac format. # # @param secretKey # @param params # # @rescue InvalidKeyException # def sign secret_key, params sorted_params = sort_convert_table(params) signature = compute_hmac(secret_key, sorted_params) signature end # # This method sorts all the values that are stored in the table. # # @param table # @return sorted string # # def sort_convert_table(table) sorted_params = "" table.sort {|a,b| a[0] <=> b[0]}.each{ |key, val| sorted_params << key sorted_params << val } sorted_params end # # Encoding and decoding of the queries are done using the Hmac Algorithm. # # @param baseString # @param key # # @rescue NoSuchAlgorithmException # def compute_hmac(secret_key, sorted_params) signature = Base64.encode64(HMAC::SHA1::digest(secret_key, sorted_params)).strip computed_hmac = CGI.escape(signature) computed_hmac end end end end