Scoty

Scoty

Concatenation of Strings fails with {ArgumentError}

Hey, guys,

I am trying to write a simple module that prepares a valid URL link. The URL needs a checksum param passed. The checksum calculations is very simple: concat all the values of the passed params(key + value) add salt and hash it.

Here is the sample* code(lets ignore for the moment that I need some kind of ordered map, cause there is no guarantee that the map is iterated in the same order. ScRequest is a struct containing the secret_key and the hashish algorithm):

defp calculate_v4_checksum(sc_request, params) do
  # this fails: str_to_hash = Enum.map(params, fn {k, v} -> Atom.to_string(k) <> v end) <> sc_request.secret_key
  str_to_hash = Enum.map(params, fn {k, v} -> Atom.to_string(k) <> v end)
  IO.puts (str_to_hash)
  IO.puts (sc_request.secret_key)
  # this fails: IO.puts (str_to_hash <> sc_request.secret_key)
  # or this: :crypto.hash(sc_request.algorithm, str_to_hash <> sc_request.secret_key)
  :crypto.hash(sc_request.algorithm, str_to_hash)
  |> Base.encode16(case: :lower)
end

Why the concatenation of the salt(secret_key) fails with ArgumentError ? Currently I can only hash the concatenated params…

Most Liked

NobbZ

NobbZ

Of course it does. All (well, most) Enum functions return a list. You can not use <> on Lists.

If I understand you correctly, you want this:

str_to_hash = params
|> Enum.map({k, v} -> "#{k}#{v}" end)
|> Enum.concat([sc_request.secret_key])
|> Enum.join

(untested)

NobbZ

NobbZ

PS: You can simulate your ordered map roughly like this (and also do it single pipe):

params
|> Enum.sort()
|> Enum.map(fn {k, v} -> "#{k}#{v}" end)
|> Enum.concat([sc_request.secret_key])
|> Enum.join
|> (&:crypto.hash(sc_request.algorithm, &1)).()
|> Base.encode16(case: :lower)
NobbZ

NobbZ

As I already said in my first answer, Enum.map returns a list, due to the function you passed into it, that was a list of String.t, but a list of strings is not a string. <> does really only work for binaries (and therefore for strings as well).

Where Next?

Popular in Chat/Questions Top

Allyedge
So, I want to get an Elixir book, but don’t know which one to get. Both Programming Elixir 1.6 and Elixir in Action looks interesting, b...
New
Yoga
Or at least, in the works? All I can find are bits and pieces but not a single project from start to finish. Including things like i18n,...
New
pdgonzalez872
Do we have a list of academic/research papers: about Elixir/Erlang? that use Elixir/Erlang? about the Beam? If so, could you please po...
New
Chawki
hi,i’m new to programming world i had learned front-end( javascript,react.js) and i wanna learn a back-end programming language i thought...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
wallyfoo
Long story short, I have a real world need to create a view for hooking up a shipping terminal to live data, but because of some early ar...
New
Kielo
Hi, I run a language learning blog and would like to learn how to code so I can create an app to help English speakers learn French. I ...
New
Nopp
Hey guys and girls, i am completely “new” to programming, recently played a bit with Python, Ruby and PureBasic, but i want to try somet...
New
zeroexcuses
Besides https://elixir-lang.org/getting-started/basic-types.html are there any other well recommended “elixir by example” style resources...
New
Emily
Preface: I’m not sure if thise is the right place, because this is not direclty Elixir related… but I’ve always got some of the best advi...
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...
985 42920 311
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement