Skip to content

Instantly share code, notes, and snippets.

@glejeune
Created June 26, 2017 08:03
Show Gist options
  • Save glejeune/d975caae85afdaff2f69fc6f7fc62f32 to your computer and use it in GitHub Desktop.
Save glejeune/d975caae85afdaff2f69fc6f7fc62f32 to your computer and use it in GitHub Desktop.
Fun with message...
-module(message).
-export([decode/3, decode/4, encode/1]).
decode(M1, M2, M3) ->
decode(M1, M2, M3, float_to_integer(length(M1)/8 - 1)).
decode(M1, M2, M3, Size) ->
[L1, L2, L3] = [[list_to_integer(string:substr(M, I*8+1, 8), 2) || I <- lists:seq(0, Size)] || M <- [M1, M2, M3]],
[A bxor B bxor C || {A, B, C} <- lists:zip3(L1, L2, L3)].
encode(Message) ->
encode(Message, "", "", "").
encode([], Acc1, Acc2, Acc3) ->
io:format("~p~n~p~n~p~n", [Acc1, Acc2, Acc3]),
decode(Acc1, Acc2, Acc3, float_to_integer(length(Acc1)/8 - 1));
encode([L|Rest], Acc1, Acc2, Acc3) ->
{A, B, C} = reverse(L),
encode(Rest, Acc1 ++ A, Acc2 ++ B, Acc3 ++ C).
reverse(N) ->
A = float_to_integer(N/2),
M = N bxor A,
B = float_to_integer(M/2),
C = M bxor B,
N = A bxor B bxor C,
{string:right(integer_to_list(A, 2), 8, $0),
string:right(integer_to_list(B, 2), 8, $0),
string:right(integer_to_list(C, 2), 8, $0)}.
float_to_integer(N) ->
list_to_integer(float_to_list(N, [{decimals,0}])).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment