Ruby To objective-C explanation

106 Views Asked by At

I have not worked with Ruby before and I have the following snippet I need to simulate it in objective-c

require 'base64'

# ...

  # Where request is of class Net::HTTP::{Get|Post|Put|Delete}
  #
  def sign(request)

    return unless @secret

    request['x-date'] = Time.now.utc.httpdate

    headers = []
    tosign = []

    tosign << request.class.name.split('::').last.upcase
    tosign << request.path.to_s.match(/(\/api.*)$/)[1]

    request.each_header do |h|
      headers << h
      tosign << "#{h}:#{request[h]}"
    end

    tosign << "\n"
    tosign << request.body || ''

    headers = headers.join(";")
    tosign = tosign.join("\n")

    sig =
      Base64.encode64(
        OpenSSL::HMAC.digest(
          OpenSSL::Digest.new('SHA256'),
          @secret,
          OpenSSL::Digest::SHA256.digest(tosign))).strip

    request['x-authorization'] =
      "My " +
      "Key=#{@key_id}," +
      "Algorithm=HMACSHA256," +
      "SignedH=#{headers}," +
      "Signature=#{sig}"
  end1

All I Know that I need to make a NSDictionary to put inside it the value of 'x-date' and 'x-authorization' then pass it as an header for my Request.

x-date is easy I wrote the following as value for it

 NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";

NSString *stringDate = [df stringFromDate:[NSDate date]];

but I need help with 'x-authorization'

0

There are 0 best solutions below