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
In reply to Re: Low level operations
by Zaxo
in thread Low level operations
by narse
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |