bxor

Martin Bjorklund mbj@REDACTED
Thu Feb 20 10:22:27 CET 2003


Dan Melomedman <dan-erlang@REDACTED> wrote:
> Thanks all for great examples. I am trying to write a RADIUS client. It
> needs to XOR an MD5 digest (128 bit binary) with an arbitrary long user 
> password. I'll try a few things, and see what works better. Thanks.

Here's another example, which may be of use to you :)


/martin



mk_password(Secret, Auth, Passwd) ->
    scramble(Secret, Auth, Passwd).

scramble(Secret, Auth, Passwd) ->
    B = erlang:md5([Secret, Auth]),
    case xor16(Passwd, B) of
	{C, <<>>}   -> C;
	{C, Tail} -> concat_binary([C, scramble(Secret, C, Tail)])
    end.

xor16(Passwd, B) when size(Passwd) < 16 ->
    xor16(pad16(Passwd), B);
xor16(<<P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,T/binary>>,
      <<B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,B11,B12,B13,B14,B15,B16>>) ->
    {<<(P1 bxor B1),
       (P2  bxor B2),
       (P3  bxor B3),
       (P4  bxor B4),
       (P5  bxor B5),
       (P6  bxor B6),
       (P7  bxor B7),
       (P8  bxor B8),
       (P9  bxor B9),
       (P10 bxor B10),
       (P11 bxor B11),
       (P12 bxor B12),
       (P13 bxor B13),
       (P14 bxor B14),
       (P15 bxor B15),
       (P16 bxor B16)>>,
     T}.

pad16(Passwd) ->
    concat_binary([Passwd, list_to_binary(zero(16 - size(Passwd)))]).

zero(0) -> [];
zero(N) -> [0 | zero(N-1)].




More information about the erlang-questions mailing list