Split and XOR a 160bits SHA Digest?

Sean Hinde <>
Fri May 14 18:07:57 CEST 2004



On 14 May 2004, at 18:15,  wrote:

> Hi All,
>
> I'm wondering if there are a better way to split and xor a 160 bits 
> sha-digest. I tryed this 2 functions (see below), but both are very 
> slow.
>
> % Create 160 bits masks
> -define(BIT_MASK1, 4294967295). % First 32 bits on
> -define(BIT_MASK2, 18446744069414584320). % Second 32 bits on
> -define(BIT_MASK3, 79228162495817593519834398720). % ...
> -define(BIT_MASK4, 340282366841710300949110269838224261120).
> -define(BIT_MASK5, 1461501636990620551282746369252908412224164331520).
>
>
> split_and_xor_version1(S, Key) ->
> <<SHA:160>> = crypto:sha_mac(S, Key),
> D5 = SHA band ?BIT_MASK5,
> D4 = SHA band ?BIT_MASK4,
> D3 = SHA band ?BIT_MASK3,
> D2 = SHA band ?BIT_MASK2,
> D1 = SHA band ?BIT_MASK1,
> D5 bxor D4 bxor D3 bxor D2 bxor D1.
>
> split_and_xor_version2(S, Key) ->
> <<D5:32, D4:32, D3:32, D2:32, D1:32>> = crypto:sha_mac(S, Key),
> D5 bxor D4 bxor D3 bxor D2 bxor D1.
>
> Any other ideas?

I would expect at least the second to do quite well in normal compiled 
code.

Note that the bit syntax has a special implementation for commands 
which are run in the the erl shell - are you seeing this slowness in a 
properly compiled module or just running it at the shell?

Sean




More information about the erlang-questions mailing list