Last active
August 29, 2015 13:56
-
-
Save puzza007/9033136 to your computer and use it in GitHub Desktop.
bxor a binary k against a (longer) binary value
This file contains 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
-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