mlen

mlen

I’m new to Elixir/Phoenix, and I’m trying to generate a hash for a given string that matches one made with Web Crypto API.

I have Nginx with NJS module (basically ECMAScript 5.1 module) that I use to generate sha256 for given host, on the fly, e.g.


let hash = await crypto.subtle.digest('SHA-256', r.headersIn.host);

let buf = Buffer.from(hash).toString('hex');

r.setReturnValue(buf);

so for host mydomain.com this gives hexits of 64e2d33ec7149d132d249084147187d043e4bb10c75acf2004597ccbd6128441

Now, I have web app in Phoenix that will create these hashes in the first place (above Nginx code needs to match them in order for things to work). However, given this Elixir code:


:crypto.hash(:sha256, "mydomain.com") |> Base.encode16() |> String.downcase()

gives hexits of 0a79eaf6adb7b1e60d3fa548aa63105f525a00448efbb59ee965b9351a90ac31

which is not the same as one that Nginx has created.

can someone help please?

both, the NJS and Phoenix needs to create same hashes for given (same) string.

BTW my understanding is that the NJS uses crypto library from ECMAScript that, I think, is Web Crypto API implementation, which conforms to FIPS 180-4 spec. Elixir, I guess, uses the Erlang hash functions, which also point to FIPS 180-4 spec from its docs. I’m lost a bit :frowning:

Showing Posts 1 to 2

hauleth

hauleth

Elixir code returns proper value:

Buffer.from(await crypto.subtle.digest('SHA-256', 'mydomain.com')).toString('hex')
// => '0a79eaf6adb7b1e60d3fa548aa63105f525a00448efbb59ee965b9351a90ac31'

So you probably have some rouge characters in your NJS input.

mlen

mlen OP

Thank you!! I knew Elixir was right (bloody JavaScript…)

Turns our the NJS input of r.headersIn.host contains a HTTP port(!) as well, so it behaves different to what Nginx does for same variable, hence I could not spot the obvious. Ehh.

BTW In Nginx $http_host returns host with port, and $host returns just the host. NJS does not have $http_host, and the $host returns host with port.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews