B:BD[
2.1290] → [
2.1290:1491]
run(Req, #{salt := Salt} = State) ->
{ok, [{<<"data">>, Data}], Req1} = cowboy_req:read_urlencoded_body(Req),
{ok, Hash} = bcrypt:hashpw(Data, Salt),
Req2 = cowboy_req:set_resp_body(Hash, Req1),
from_json(Req, State) ->
{ok, Body, Req1} = cowboy_req:read_body(Req),
{ok, Data} = thoas:decode(Body),
% Use same defaults than bcrypt
Rounds = maps:get(rounds, Data, 12),
{ok, Salt} = bcrypt:gen_salt(Rounds),
% Hash the whole Body to ensure that players don't overwrite
% settings in the requests.
{ok, Hash} = bcrypt:hashpw(Body, Salt),
Res = thoas:encode(#{<<"digest">> => list_to_binary(Hash)}),
Req2 = cowboy_req:set_resp_body(Res, Req1),