in reply to Low level operations

1. is pretty easy,

$key_arr[0] = pack 'h2', unpack 'H2', ( $lock_arr[0] ^ $lock_arr[-1] # ^ $lock_arr[-2] ^ 5 ); # not quite right ^ $lock_arr[-2] ^ chr(5) );

I simplified the end of array indexing a bit.

You can view the results with

print unpack 'H*', join '', @lock_arr; print unpack 'H*', join '', @key_arr;

You appear to have split the lock phrase into an array for convenience. Another approach would be with substr. I'd combine both operations in a single sub which takes the lock string or array as an argument and returns the key:

sub keycalc { my @key = @_; for (0..$#key) { $key[$_] = pack 'h2', unpack 'H2', ($_ ? $key[$_] ^ $key[$_ - 1] # : $key[-1] ^ $key[-2] ^ 5); # same error : $key[-1] ^ $key[-2] ^ chr(5)); } @key; } print unpack( 'H*', join '', @lock_arr), $/; print unpack( 'H*', join '', keycalc( @lock_arr)), $/;

Update: Oops! you need the numeric 5 as a character, Fixed with chr, as indicated.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Low level operations
by narse (Pilgrim) on Oct 09, 2002 at 16:33 UTC
    I am using something like your keycalc sub now, although there are two problems. First, using $key[$_] as the destination confuses $key[$_-1]. The main problem is the use of ... ^ 5. I have tried this before and it gives me an error. Quoting '5' however does execute. Will this work? I do not know how perl actually handles strings so I do not know if this will give me the result I am looking for,