7

I'm just new to Elixir.

After generating a hmac hash, I got a bitstring:

:crypto.hmac(:sha512, secret, data)
Sign: <<104, 155, 224, 193, 121, 129, 237, 103, 233, 236, 161, 130...>>

Now, I have to convert it to String, but don't know how exactly.

Any Elixir/erlang module to do this directly?

3
  • Instead of editing in your answer, please post it as an answer to your question and accept it to avoid confusion. Commented Feb 15, 2016 at 0:41
  • Ok @AMACB, I'll do this way. Thanks for the tip. Commented Feb 15, 2016 at 11:34
  • Make sure to accept it too :) Commented Feb 15, 2016 at 14:22

1 Answer 1

7

Oops, I didn't see that you originally want to use the output of the bitstring with the String Module. You already can! You can see this by trying to pipe your output into String.length and getting a successful return value.

This getting started guide does a good job of walking through the basics. Specifically how "A string is a UTF-8 encoded binary".

What do you want to be able to do with the output?

If you you instead want to be able to pass the hash through URL for an auth system or something like that, I left the original answer.


You can use the Base Module to achieve that.

For example you could pipe the output like

:crypto.hmac(:sha512, secret, data) |> Base.encode64

If you need it to be filename or url safe there is an alternative url_encode64 function.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @the-brofessor. I'll post it to an API authenticating my access. Just used Base.encode16 Base.encode16(:crypto.hmac(:sha512, secret, postdata), case: :lower)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.