Skip to content

Instantly share code, notes, and snippets.

@puzza007
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save puzza007/9033136 to your computer and use it in GitHub Desktop.
Save puzza007/9033136 to your computer and use it in GitHub Desktop.
bxor a binary k against a (longer) binary value
-module(foo).
-export([b/2]).
b(K, B) when is_binary(K) andalso
is_binary(B) andalso
byte_size(K) =< byte_size(B) ->
SizeB = byte_size(B),
N = (SizeB div byte_size(K)) + 1,
K2 = lists:duplicate(N, K),
K3 = list_to_binary(K2),
K4 = <<K3:SizeB/binary>>,
binary_xor(K4, B).
binary_xor(A, B) when is_bitstring(A) andalso
is_bitstring(B) andalso
bit_size(A) =:= bit_size(B) ->
BitSize = bit_size(A),
<<IA:BitSize>> = A,
<<IB:BitSize>> = B,
I = IA bxor IB,
<<I:BitSize>>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment