hoseinisalim

hoseinisalim

Convert base64 string to byte string

how to convert base64 string to byte string?

Most Liked

chrismcg

chrismcg

You can turn it into a list:

iex(11)> "Like this šŸ˜€" |> Base.encode64
"TGlrZSB0aGlzIPCfmIA="
iex(12)> "TGlrZSB0aGlzIPCfmIA=" |> Base.decode64!() |> :binary.bin_to_list()
[76, 105, 107, 101, 32, 116, 104, 105, 115, 32, 240, 159, 152, 128]
NobbZ

NobbZ

I already told you a few times, the documentation of that library is far from par.

Also @chrismcg told you to try Base.url_decode64!/1, which you obviously didn’t, as it works for me:

iex(1)> message = "Foo"
"Foo"
iex(2)> {:ok, key64} = ExCrypto.generate_aes_key(:aes_256, :base64)
{:ok, "YBFgCMzhnxIOVeirWf5hgzbFmUcvZcHY0yKRJJ4Ocks="}
iex(3)> key = Base.url_decode64!(key64)
<<96, 17, 96, 8, 204, 225, 159, 18, 14, 85, 232, 171, 89, 254, 97, 131, 54, 197,
  153, 71, 47, 101, 193, 216, 211, 34, 145, 36, 158, 14, 114, 75>>
iex(4)> {:ok, {iv, ciphered}} = ExCrypto.encrypt(key, message)
{:ok,
 {<<195, 207, 172, 38, 128, 248, 158, 10, 14, 239, 231, 195, 48, 154, 244, 177>>,
  <<4, 238, 11, 86, 7, 6, 5, 86, 13, 182, 189, 113, 193, 85, 194, 141>>}}
iex(5)> {:ok, clear} = ExCrypto.decrypt(key, iv, ciphered)
{:ok, "Foo"}

It seems as if you really need to actively read and understand the sources of this library to be able to use it.

My suggestion is, that you use :bytes with the key generator and then convert the resulting binary to exactly the format you really need. No surprises anymore because of bad documentation, that claims to return base64 encoded string but actually does return an Base64-URL-encoded string…

NobbZ

NobbZ

As there is no native kind of array in elixir, you can’t.

But at least there is the Base module, which allows you to decode base64 encoded stuff to a binary.

Where Next?

Popular in Questions Top

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
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
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
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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[ā€˜SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement