BoZhenka

BoZhenka

Right way to use :crypto.verify(...)

I have some problems with :crypto.verify.

message = 'whatever'
sign = 'zFuf7bRH4RHwyktaqHQwmX5rn3LfSb4dKo'
public_key = 'MIGJAoGBAM1fmNUvezts3yglTdhXuqG7OhHxQtDFA'
:crypto.verify(:rsa, :sha256, message, sign, public_key)

This is give me an ArgumentError, like:

(crypto) :crypto.pkey_verify_nif(:rsa, :sha256, 'whatever', 'zFuf7bRH4RHwyktaqHQwmX5rn3LfSb4dKo', ["M", "I", "G", "J", "A", "o", "G", "B", "A", "M", "1", "f", "m", "N", "U", "v", "e", "z", "t", "s", "3", "y", "g", "l", "T", "d", "h", "X", "u", "q", "G", "7", "O", "h", "H", "x", "Q", "t", "D", "F", "A"], [])
(crypto) crypto.erl:874: :crypto.verify/6

Method :crypto.verify/5 exist, but some reason it’s gave me error about …verify/6
I don’t get it. What should I do for correct working of this?..

Most Liked

voltone

voltone

The public_key variable appears to contains a truncated Base64 fragment of a 1024-bit RSA public key. Assuming you actually have the full value you can convert it to an rsa_public() type like this:

public_key_base64 = "MIGJAoGBAM1fmNUvezts3yglTdhXuqG7OhHxQtDFA..." # truncated
public_key = :public_key.der_decode(:RSAPublicKey, Base.decode64!(public_key_base64))

The signature also appears to be truncated. Given the full Base64 encoded signature you can verify the signature with :crypto.verify:

message = "whatever"
signature_base64 = "zFuf7bRH4RHwyktaqHQwmX5rn3LfSb4dKo..." # truncated
signature = Base.decode64!(signature_base64)
:crypto.verify(:rsa, :sha256, message, signature, public_key)

Note the use of binary strings as opposed to chartists (single quotes), as others have pointed out.

idi527

idi527

:waving_hand:

According to crypto — OTP 29.0.2 (crypto 5.9), the message needs to be a binary, not a charlist. As well as signature and it shouldn’t be encoded either. And public key needs to be in the format erlang understands.

victorolinasc

victorolinasc

The function signature of :crypto.verify/5 does not match with your params.

verify(Algorithm, DigestType, Msg, Signature, Key) -> Result
verify(Algorithm, DigestType, Msg, Signature, Key, Options) ->
          Result
Types
Algorithm = pk_sign_verify_algs()
DigestType =
    rsa_digest_type() | dss_digest_type() | ecdsa_digest_type()
Msg = binary() | {digest, binary()}
Signature = binary()
Key =
    rsa_public() |
    dss_public() |
    [ecdsa_public() | ecdsa_params()] |
    [eddsa_public() | eddsa_params()] |
    engine_key_ref()
Options = pk_sign_verify_opts()
Result = boolean()

First 2 parameters seem to match but your message seems not to be “cleartext” so you’d need to pass it wrapped in a tuple. Your public key also does not match the typespec.

Here is a StackOverflow example: https://stackoverflow.com/a/20509599/1397594

Also note your parameters are passing charlists and not binaries.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement