Split and XOR a 160bits SHA Digest?

Magnus Thoäng <>
Fri May 14 18:39:48 CEST 2004


 wrote:

> Hi All,
> Sorry for the mistake in split_and_xor_version1/2. I fix it below.
> 
> 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 bsr 128) bxor (D4 bsr 96) bxor (D3 bsr 64) bxor (D2 bsr 32) 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?

Hi,

The second version is, without doubt, better than the first, and 
probably hard to beat.

I'd expect the crypto:sha_mac call to be the "slow stuff" in any case, 
even for a small/short S.

-- 
Magnus Thoäng
ÄL/EAB/UPD/EP
Multi Service Gateway

Ericsson AB
Core Network Development
Tel: + 46 8 719 22 41
Fax: + 46 8 719 77 50



More information about the erlang-questions mailing list