Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ultimate newbie with perl script that won't run

Status
Not open for further replies.

canrocks

Technical User
Feb 23, 2003
4
CA
I found this script on the web somewhere, and I've been working through it to get it to be able to run. I've guessed my way through some syntax errors (see, perl isn't that tough, once you know one language, they're all the same:)) but that only got me so far. This is the script:


#!/usr/bin/perl
use Digest::MD5 qw(md5_hex);
printf "Usage: file.pl email id";

$$pad = pack("H2048 ",
"00000000963007772c610eeeba51099...e0cc31bdf05ä8def022d");
@pad = unpack("L256", $pad);

@owner = unpack("C", $ARGV[0 ]);
@regid = reverse unpack("C", pack("H", substr($ARGV[1], 5, 4).substr($ARGV[1], 10, 4)));

@tab = (@regid, @owner);
$$g = $$pad[71];

foreach $$c(@tab)
{
$$i = ($$c^$g) & 0xff;
$$g = ($$g>>8)^ $$pad[$i ];
}

$$key = $$g << 16;
$$g = ($$g>>8)^($$pad[$g & 0xff ]);
$$g = ($$g>>8)^($$pad[$g & 0xff ]);
$$key = $$key|((($$g>>16)^$g) & 0xffff);

$$keystr = uc(unpack("H * ", pack("N", $key)));
$$digest = md5_hex("$keystr\n");

$$keystr = $$keystr.uc(substr($digest, 0, 4));
$$keystr = ~ s/^(..)(..)(..)(..)(..)(..)$/\1-\2-\3-\4-\5-\6 /;
printf "key: %s\n ", $$keystr;

It seems to get hooked up on $$g = $$pad[71]; and I'm not sure why.
 
Using two dollar signs when naming variables is actually referencing a variable. Take out the $$ and replace with a single $ to correct your scripts problems.

- Rieekan
 

If the output should look like this

Usage: file.pl email id
key: 4294967295

Then
Turn your code to this

and notice that at the 'foreach $$c(@tab)'
the '$$c' must not be an array, but a variable.


#!/usr/bin/perl
use Digest::MD5 qw(md5_hex);
printf "Usage: file.pl email id\n";

$pad = pack("H2048 ",
"00000000963007772c610eeeba51099...e0cc31bdf05?¤8def022d");
@pad = unpack("L256", $pad);

@owner = unpack("C", $ARGV[0 ]);
@regid = reverse unpack("C", pack("H", substr($ARGV[1], 5, 4).substr($ARGV[1], 10, 4)));

@tab = (@regid, @owner);
$$g = @pad[71];

foreach $c(@tab)
{
@i = (@c^$g) & 0xff;
@g = (@g>>8)^ @pad[$i ];
}

@key = @g << 16;
@g = (@g>>8)^($$pad[$g & 0xff ]);
@g = (@g>>8)^($$pad[$g & 0xff ]);
@key = @key|(((@g>>16)^$g) & 0xffff);

@keystr = uc(unpack("H * ", pack("N", $key)));
@digest = md5_hex("$keystr\n");

@keystr = $$keystr.uc(substr($digest, 0, 4));
@keystr = ~ s/^(..)(..)(..)(..)(..)(..)$/\1-\2-\3-\4-\5-\6 /;
printf "key: %s\n ", @keystr;



 
sorry i missed some lines

change every '$$' to '@'

Its not a big differense but it looks beter reading it.

But dont touch the singles '$'.
 
can someone delete my posts here in this thread cause
they got to be wrong

cause i notise that even with only these 4 lines

use Digest::MD5 qw(md5_hex);
printf "Usage: file.pl email id";

@keystr = ~ s/^(..)(..)(..)(..)(..)(..)$/\1-\2-\3-\4-\5-\6 /;
printf "key: %s\n ", @keystr;


the program gives the exact same result!!!

Either the program is just poetry or i am loosing it.
 
I think what everyone is trying to say is....

Code:
#!/usr/bin/perl -w
use strict;

use Digest::MD5 qw(md5_hex);
print "Usage: file.pl email id\n";

my $pad = pack("H2048", "00000000963007772c610eeeba51099...e0cc31bdf05ä8def022d");
my @pad = unpack("L256", $pad);

my @owner = unpack("C", $ARGV[0]);
my @regid = reverse unpack("C", pack("H", substr($ARGV[1], 5, 4).substr($ARGV[1], 10, 4)));

my $g = $pad[71];

foreach my $c (@regid, @owner) {
	my $i = ($c^$g) & 0xff;
	$g = ($g>>8) ^ $pad[$i];
}

my $key = $g << 16;
$g = ($g>>8)^($pad[$g & 0xff ]);
$g = ($g>>8)^($pad[$g & 0xff ]);
$key = $key|((($g>>16)^$g) & 0xffff);

my $keystr = uc(unpack("H * ", pack("N", $key)));
my $digest = md5_hex("$keystr\n");

$keystr = $keystr.uc(substr($digest, 0, 4));
$keystr = join('-',($keystr =~ /(..)/g)[0..5]);
print "key: $keystr\n";

I recommend you add the following lines at the start of scripts:

Code:
#!/usr/bin/perl -w
use strict;

as they will help you to spot some obvious bugs very quickly. It does require you to add 'my' to each variable declaration, but ultimately its worth the effort.

Also don't use printf unnecessarily, where print will do. The reson being that in your printf statements you were doing interpolation on the format string, and then printf interpolating the arguments into the string.

Note the line before the last print statement. This is a bit of a shortcut form of what you wanted, to put a dash between each pair of numbers. As you had 6 pairs in your original code the list dereference is [0..5], however, if you are quite happy whatever length of string you want to handle you can replace it with:

Code:
$keystr = join('-',($keystr =~ /(..)/g));

Barbie
Leader of Birmingham Perl Mongers
 
This the output of the code you just wrote barbie!


C:\>a.pl
Usage: file.pl email id
Use of uninitialized value in unpack at C:\a.pl line 10.
Use of uninitialized value in substr at C:\a.pl line 11.
substr outside of string at C:\a.pl line 11.
Use of uninitialized value in substr at C:\a.pl line 11.
substr outside of string at C:\a.pl line 11.
Use of uninitialized value in concatenation (.) or string at C:\a.pl line 11.
Use of uninitialized value in concatenation (.) or string at C:\a.pl line 11.
Use of uninitialized value in join or string at C:\a.pl line 29.
Use of uninitialized value in join or string at C:\a.pl line 29.
Use of uninitialized value in join or string at C:\a.pl line 29.
Use of uninitialized value in join or string at C:\a.pl line 29.
key: 08-97----


Could someone tell us what is this script? (not what it does, but why it is written like this)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top