Created
June 17, 2010 12:04
-
-
Save darkua/442019 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%-------------------------------------------------------------------- | |
%% @spec build_challenge(Key1::list(),Key2::list(),Key3::binary()) -> binary() | |
%% @doc Builds the challenge needed by WebSocket Handshake 76 | |
%% @end | |
%%-------------------------------------------------------------------- | |
build_challenge(Key1,Key2,Key3)-> | |
Ikey1 = get_key_number(Key1,[]), | |
Ikey2 = get_key_number(Key2,[]), | |
Blank1 = get_n_blanks(Key1,0), | |
Blank2 = get_n_blanks(Key2,0), | |
Part1 = erlang:list_to_integer(Ikey1) div Blank1, | |
Part2 = erlang:list_to_integer(Ikey2) div Blank2, | |
Ckey = <<Part1:4/big-unsigned-integer-unit:8,Part2:4/big-unsigned-integer-unit:8,Key3/binary>>, | |
Challenge = erlang:md5(Ckey). | |
get_n_blanks([],Count)-> | |
Count; | |
get_n_blanks([S|T],Count) when S == 32-> | |
get_n_blanks(T,Count+1); | |
get_n_blanks([_S|T],Count)-> | |
get_n_blanks(T,Count). | |
get_key_number([],Acc)-> | |
Acc; | |
get_key_number([H | T],Acc) when H >= 48 andalso H =< 57-> | |
get_key_number(T, lists:append(Acc,[H])); | |
get_key_number([_H | T],Acc)-> | |
get_key_number(T,Acc). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment