How to use digest method of Api Package

Best Active_mocker_ruby code snippet using Api.digest

client.rb

Source:client.rb Github

copy

Full Screen

...8 # because Post modules require these to be defined when including HttpClient9 def register_autofilter_ports(ports=[]); end10 def register_autofilter_hosts(ports=[]); end11 def register_autofilter_services(services=[]); end12 def hexdigest(value)13 if value.nil? || !value.instance_of?(String)14 print_error "Unexpected value format"15 return nil16 end17 digest = OpenSSL::Digest::SHA256.new18 if value.respond_to?(:read)19 chunk = nil20 chunk_size = 1024 * 1024 # 1 megabyte21 digest.update(chunk) while chunk = value.read(chunk_size)22 value.rewind23 else24 digest.update(value)25 end26 digest.hexdigest27 end28 def hmac(key, value)29 if key.nil? || !key.instance_of?(String) || value.nil? || !value.instance_of?(String)30 print_error "Unexpected key/value format"31 return nil32 end33 OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, value)34 end35 def hexhmac(key, value)36 if key.nil? || !key.instance_of?(String) || value.nil? || !value.instance_of?(String)37 print_error "Unexpected key/value format"38 return nil39 end40 OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, value)41 end42 def request_to_sign(headers, body_digest)43 if headers.nil? || !headers.instance_of?(Hash) || body_digest.nil? || !body_digest.instance_of?(String)44 return nil, nil45 end46 headers_block = headers.sort_by(&:first).map do |k, v|47 v = "#{v},#{v}" if k == 'Host'48 "#{k.downcase}:#{v}"49 end.join("\n")50 headers_list = headers.keys.sort.map(&:downcase).join(';')51 flat_request = [ "POST", "/", '', headers_block + "\n", headers_list, body_digest].join("\n")52 [headers_list, flat_request]53 end54 def sign(creds, service, headers, body_digest, now)55 date_mac = hmac("AWS4" + creds.fetch('SecretAccessKey'), now[0, 8])56 region_mac = hmac(date_mac, datastore['Region'])57 service_mac = hmac(region_mac, service)58 credentials_mac = hmac(service_mac, 'aws4_request')59 headers_list, flat_request = request_to_sign(headers, body_digest)60 doc = "AWS4-HMAC-SHA256\n#{now}\n#{now[0, 8]}/#{datastore['Region']}/#{service}/aws4_request\n#{hexdigest(flat_request)}"61 signature = hexhmac(credentials_mac, doc)62 [headers_list, signature]63 end64 def auth(creds, service, headers, body_digest, now)65 headers_list, signature = sign(creds, service, headers, body_digest, now)66 "AWS4-HMAC-SHA256 Credential=#{creds.fetch('AccessKeyId')}/#{now[0, 8]}/#{datastore['Region']}/#{service}/aws4_request, SignedHeaders=#{headers_list}, Signature=#{signature}"67 end68 def body(vars_post)69 pstr = ""70 vars_post.each_pair do |var, val|71 pstr << '&' unless pstr.empty?72 pstr << var73 pstr << '='74 pstr << val75 end76 pstr77 end78 def headers(creds, service, body_digest, now = nil)79 now = Time.now.utc.strftime("%Y%m%dT%H%M%SZ") if now.nil?80 headers = {81 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',82 'Accept-Encoding' => '',83 'User-Agent' => USER_AGENT,84 'X-Amz-Date' => now,85 'Host' => datastore['RHOST'],86 'X-Amz-Content-Sha256' => body_digest,87 'Accept' => '*/*'88 }89 headers['X-Amz-Security-Token'] = creds['Token'] if creds['Token']90 sign_headers = ['Content-Type', 'Host', 'User-Agent', 'X-Amz-Content-Sha256', 'X-Amz-Date']91 auth_headers = headers.select { |k, _| sign_headers.include?(k) }92 headers['Authorization'] = auth(creds, service, auth_headers, body_digest, now)93 headers94 end95 def print_hsh(hsh)96 return if hsh.nil? || !hsh.instance_of?(Hash)97 hsh.each do |key, value|98 vprint_status "#{key}: #{value}"99 end100 end101 def print_results(doc, action)102 response = "#{action}Response"103 result = "#{action}Result"104 resource = /[A-Z][a-z]+([A-Za-z]+)/.match(action)[1]105 if doc["ErrorResponse"] && doc["ErrorResponse"]["Error"]106 print_error doc["ErrorResponse"]["Error"]["Message"]107 return nil108 end109 idoc = doc.fetch(response)110 if idoc.nil? || !idoc.instance_of?(Hash)111 print_error "Unexpected response structure"112 return {}113 end114 idoc = idoc[result] if idoc[result]115 idoc = idoc[resource] if idoc[resource]116 if idoc["member"]117 idoc["member"].each do |x|118 print_hsh x119 end120 else121 print_hsh idoc122 end123 idoc124 end125 def call_api(creds, service, api_params)126 vprint_status("Connecting (#{datastore['RHOST']})...")127 body = body(api_params)128 body_length = body.length129 body_digest = hexdigest(body)130 begin131 res = send_request_raw(132 'method' => 'POST',133 'data' => body,134 'headers' => headers(creds, service, body_digest)135 )136 if res.nil?137 print_error "#{peer} did not respond"138 else139 Hash.from_xml(res.body)140 end141 rescue => e142 print_error e.message143 end144 end145 def call_iam(creds, api_params)146 api_params['Version'] = '2010-05-08' unless api_params['Version']147 call_api(creds, 'iam', api_params)148 end...

Full Screen

Full Screen

digest_algorithm.rb

Source:digest_algorithm.rb Github

copy

Full Screen

1require 'digest'2module RestPki3 class DigestAlgorithm4 attr_reader :name, :oid, :byte_length, :api_model, :xml_uri, :crypto_digest5 def initialize(name, oid, byte_length, api_model, xml_uri, crypto_digest)6 @name = name7 @oid = oid8 @byte_length = byte_length9 @api_model = api_model10 @xml_uri = xml_uri11 @crypto_digest = crypto_digest12 end13 def self.MD5; MD5DigestAlgorithm.new end14 def self.SHA1; SHA1DigestAlgorithm.new end15 def self.SHA256; SHA256DigestAlgorithm.new end16 def self.SHA384; SHA384DigestAlgorithm.new end17 def self.SHA512; SHA512DigestAlgorithm.new end18 def ==(comparison_object)19 if comparison_object.equal?(self)20 return true21 end22 unless comparison_object.instance_of?(self.class)23 return false24 end25 self.oid == comparison_object.oid26 end27 def check_length(digest_value)28 unless digest_value.length == @byte_length29 raise "A #{@name} digest should contain #{@byte_length} bytes, but a value with #{digest_value.length} bytes was given"30 end31 end32 def self.algorithms33 [34 DigestAlgorithm.MD5,35 DigestAlgorithm.SHA1,36 DigestAlgorithm.SHA256,37 DigestAlgorithm.SHA384,38 DigestAlgorithm.SHA51239 ]40 end41 42 def self.get_instance_by_name(name)43 begin44 alg = DigestAlgorithm.algorithms.find{|a| a.name == name}45 rescue46 raise "Unrecognized digest algorithm name: #{name}"47 end48 alg49 end50 51 def self.get_instance_by_oid(oid)52 begin53 alg = DigestAlgorithm.algorithms.find{|a| a.oid == oid}54 rescue55 raise "Unrecognized digest algorithm oid: #{oid}"56 end57 alg58 end59 60 def self.get_instance_by_xml_uri(xml_uri)61 begin62 alg = DigestAlgorithm.algorithms.find{|a| a.xml_uri == xml_uri}63 rescue64 raise "Unrecognized digest algorithm xml_uri: #{xml_uri}"65 end66 alg67 end68 69 def self.get_instance_by_api_model(algorithm)70 case algorithm.upcase71 when 'MD5'72 DigestAlgorithm.MD573 when 'SHA1'74 DigestAlgorithm.SHA175 when 'SHA256'76 DigestAlgorithm.SHA25677 when 'SHA384'78 DigestAlgorithm.SHA38479 when 'SHA512'80 DigestAlgorithm.SHA51281 else82 raise "Unsupported digest algorithm: #{algorithm}"83 end84 end85 end86 class MD5DigestAlgorithm < DigestAlgorithm87 def initialize88 @name = 'MD5'89 @oid = Oids.oids["MD5"]90 @byte_length = 1691 @api_model = 'md5'92 @xml_uri = 'http://www.w3.org/2001/04/xmldsig-more#md5'93 @crypto_digest = Digest::MD5.new94 super(name, oid, byte_length, api_model, xml_uri, crypto_digest)95 end96 end97 class SHA1DigestAlgorithm < DigestAlgorithm98 def initialize99 @name = 'SHA1'100 @oid = Oids.oids["SHA1"]101 @byte_length = 20102 @api_model = 'sha1'103 @xml_uri = 'http://www.w3.org/2000/09/xmldsig#sha1'104 @crypto_digest = Digest::SHA1.new105 super(name, oid, byte_length, api_model, xml_uri, crypto_digest)106 end107 end108 class SHA256DigestAlgorithm < DigestAlgorithm109 def initialize110 @name = 'SHA256'111 @oid = Oids.oids["SHA256"]112 @byte_length = 32113 @api_model = 'sha256'114 @xml_uri = 'http://www.w3.org/2001/04/xmlenc#sha256'115 @crypto_digest = Digest::SHA2.new(256)116 super(name, oid, byte_length, api_model, xml_uri, crypto_digest)117 end118 end119 class SHA384DigestAlgorithm < DigestAlgorithm120 def initialize121 @name = 'SHA384'122 @oid = Oids.oids["SHA384"]123 @byte_length = 48124 @api_model = 'sha384'125 @xml_uri = 'http://www.w3.org/2001/04/xmldsig-more#sha384'126 @crypto_digest = Digest::SHA2.new(384)127 super(name, oid, byte_length, api_model, xml_uri, crypto_digest)128 end129 end130 class SHA512DigestAlgorithm < DigestAlgorithm131 def initialize132 @name = 'SHA512'133 @oid = Oids.oids["SHA512"]134 @byte_length = 64135 @api_model = 'sha512'136 @xml_uri = 'http://www.w3.org/2001/04/xmlenc#sha512'137 @crypto_digest = Digest::SHA2.new(512)138 super(name, oid, byte_length, api_model, xml_uri, crypto_digest)139 end140 end141end...

Full Screen

Full Screen

digest

Using AI Code Generation

copy

Full Screen

1 Digest::SHA256.hexdigest('hello')2 def initialize(api)3md = MyDigest.new(Digest)4 def initialize(api)5md = MyDigest.new(Digest)6 def initialize(api)7md = MyDigest.new(Digest)8 def initialize(api)9md = MyDigest.new(Digest)

Full Screen

Full Screen

digest

Using AI Code Generation

copy

Full Screen

1digest.update('The quick brown fox jumps over the lazy dog')2digest.update('The quick brown fox jumps over the lazy dog')3digest.update('The quick brown fox jumps over the lazy dog')4digest.update('The quick brown fox jumps over the lazy dog')5digest.update('The quick brown fox jumps over the lazy dog')

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Active_mocker_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful