Skip to content

Instantly share code, notes, and snippets.

@AlexanderWingard
Created May 20, 2009 08:26
Show Gist options
  • Save AlexanderWingard/114699 to your computer and use it in GitHub Desktop.
Save AlexanderWingard/114699 to your computer and use it in GitHub Desktop.
make_qry(Chl) ->
%% First we compose two strings:
%% ChlKey = Challenge + Client-Key
%% ChlPid = Challenge + Client-Id zero padded to even 8
Key = "CFHUR$52U_{VIX5T",
Pid = "PROD0101{0RM?UBW",
ChlKey = Chl ++ Key,
ChlPid0 = Chl ++ Pid,
ChlPadL = 8 - (length(ChlPid0) rem 8),
ChlPid1 = ChlPid0 ++ lists:duplicate(ChlPadL, $0),
%% Chop the strings up in 4 byte parts and flip byte order.
ChlKeyParts = split_32_little(erlang:md5(ChlKey)),
ChlPidParts = split_32_little(ChlPid1),
%% Calculate the two key parts.
{Hgh, Low} = make_qry_calc(ChlKeyParts, ChlPidParts),
%% XOR the key parts with the ChlKey and flip byte order again.
<<Res:128>> = <<(element(1, ChlKeyParts) bxor Hgh):32/little,
(element(2, ChlKeyParts) bxor Low):32/little,
(element(3, ChlKeyParts) bxor Hgh):32/little,
(element(4, ChlKeyParts) bxor Low):32/little>>,
%% Return lower-case 32 char hex notation.
string:to_lower(lists:flatten(io_lib:format("~32.16.0B", [Res]))).
split_32_little(<<X:32/little, T/binary>>) ->
[X | split_32_little(T)];
split_32_little(<<>>) ->
[].
make_qry_calc(M, CHL) ->
make_qry_calc([X band 16#7fffffff || X <- M], CHL, 0, 0).
make_qry_calc([M1, M2, M3, M4], [C1, C2 | T], Hgh, Low) ->
Temp = (M1 * (((16#0e79a9c1 * C1) rem 16#7fffffff) + Hgh) + M2) rem 16#7fffffff,
Hgh1 = (M3 * ((C2 + Temp) rem 16#7fffffff) + M4) rem 16#7fffffff,
Low1 = Low + Hgh1 + Temp,
make_qry_calc([M1, M2, M3, M4], T, Hgh1, Low1);
make_qry_calc([_, M2, _, M4], [], Hgh, Low) ->
Hgh1 = (Hgh + M2) rem 16#7fffffff,
Low1 = (Low + M4) rem 16#7fffffff,
{Hgh1, Low1}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment