nikolis

nikolis

Problem calculating Signatures(aws) in Eliir

So I am going through the documenation of AWS in order to calculate Signature for the Authorization Header

and I am following through with code in Elixir but at some point something appartently goes terribly wrong but I can’t get what.

So working with the Example: GET Object section from (https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html#canonical-request)
in order to verify my process I have the following problem

  1. I have a function that returns the canonical request as explained in the page and works fine for me, when I add the attributes of the example I get back exactly what was expected

This is the string returned if printed to the console using the IO.write function

GET
/text.txt

host:examplebucket.s3.amazonaws.com
range:bytes=0-9
x-amz-content-sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
x-amz-date:20130524T000000Z

host;range;x-amz-content-sha256;x-amz-date
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

Which is exactly how it looks also in the example(nothing difficult so far just some minor string manipulation)

Next steps is to created the so called StringToSign
and the steps for calculating this one as explained in the aws web site is the following

“AWS4-HMAC-SHA256” + “\n” +
timeStampISO8601Format + “\n” +
<Scope> + “\n” +
Hex(SHA256Hash( <CanonicalRequest> ))

my code for calculating this is the following

def string_to_sign(path, headers, payload) do
    @authorization<> "\n"<>
    date_time_aws_8601() <> "\n" <> 
    scope() <> "\n"<>
    hex_sha_canonical_request(path, headers, payload)
  end 


  def canonical_request2(path, headers, payload) do
    """
    GET
    #{path}

    #{canonical_headers(headers)}
    
    #{signed_headers(headers)}
    #{hashed_payload(payload)}
    """
  end


def hex_sha_canonical_request(path, headers, payload) do
    can_req = canonical_request2(path, headers, payload)
    :crypto.hash(:sha256, can_req)
    |> Base.encode16
  end

then the result expected by the site looks like this

AWS4-HMAC-SHA256
20130524T000000Z
20130524/us-east-1/s3/aws4_request
9e0e90d9c76de8fa5b200d8c849cd5b8dc7a3be3951ddb7f6a76b4158342019d

But mine looks like this

AWS4-HMAC-SHA256
20130524T000000Z
20130524/us-east-1/s3/aws4_request
BFD7EE52C8FEBBBD70EA653DE76795FC415FB65C331ADC4DDD266E8C6369AD17

The problem apparently is that those look completly different but
I can’t understand how this is possible since my function for getting the canonical_request looks like it’s giving correct results and then the calculations do get the hex digits that do not match looks fairly straight forward

Hex(SHA256Hash(<CanonicalRequest>))

If anybody can understend where the problem could possible be please give me some direction because right now I can’t think what could have possible went wrong.

Marked As Solved

nikolis

nikolis

Somewere along the way to insanity I figured that the copy to clipboard functionality of the of the AWS web site adds two additional spaces at the end of the text which as you might expect change the hash created by sha256 completly the thing is that although the copy to clipboard would add those to spaces this is probably a mistake becase as I figured later they were not included when the hash indicated underneath was calculated thus most of the confusion.

Thanks a lot all

Also Liked

ityonemo

ityonemo

Double check that your line break matches AWS spec. Is it \n or \r\n, etc.

ityonemo

ityonemo

Oh. Are you sure you don’t need hmac-sha256?

:crypto.hmac/3

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement