coldfingertips has asked for the wisdom of the Perl Monks concerning the following question:

Anyone have a snippet they've used to put their link in scripts the've given to people? Just something that would scramble a link code so the user can't find your link by CTRL+F "link.com" in the source code but have the script decode it at runtime.

I realize anyone who knows anything about Perl would know which scramble code to remove to get rid of the link, but it would help others from removing the copyright notices on some of my scripts.

Replies are listed 'Best First'.
Re: scramble ad link
by GrandFather (Saint) on Jul 02, 2006 at 03:14 UTC

    If all you are looking for is some light weight encoding then the following may be of help:

    use strict; use warnings; srand 10; while (<DATA>) { chomp; print pack ('h2', $_) for /(..)/g; print "\n"; } __DATA__ 95f6570286166756023757363656564656460296e602465636f64696e6760247865602 +47568747e2 7556c6c60246f6e656e2

    Replace the pack line with:

    print unpack ('h2', $_) for split '';

    To generate the encoded string.


    DWIM is Perl's answer to Gödel
Re: scramble ad link
by davido (Cardinal) on Jul 02, 2006 at 04:06 UTC

    Could you do a rot-13 on it? Or how about just translating links like "http://www.somelink.com" to "http (colon) (slash) (slash) www (dot) somelink (dot) com"? That would be easy enough to create with either a hash or a substitution operator. ...for example:

    my $url = 'http://www.somelink.com'; my %translation = ( '.' => ' (dot) ' , '/' => ' (slash) ', ':' => ' (colon) ', ); foreach my $key ( keys %translation ) { $url =~ s/\Q$key\E/$translation{$key}/g; } print $url, "\n";

    I'm not sure if that's what you're after, but it might give you some ideas to work with. It would be easy enough to reverse this process too.


    Dave

Re: scramble ad link
by TedPride (Priest) on Jul 02, 2006 at 08:10 UTC
    This is more an obfuscation problem than anything else. Sure, there are millions of ways to encrypt the link, but anyone with half a brain will still be able to figure out where it is in the code, even with little or no programming experience. What you need to do is add a verification routine entirely separate from the link print (and preferably hidden somewhere they aren't likely to look) that loads the script, computes a hash of its size and/or contents, and checks it against an internal hash to make sure the script hasn't been modified. Then if they remove your link, the script errors out, and most people won't be smart enough to figure out why.

      Note Re: $. - smarter than you might think which describes using <DATA> to access the source of the script. That's a neat way to get the source actually being executed and if there is any excuse (like configuration parameters for example) to have a __DATA__ section then much of the code pertaining to the tests can vanish from sight as being relevant to checking configuration.

      The code protection could be made rather strong by requiring a key (some of the source) to decode important parts of the code that then get executed using eval.


      DWIM is Perl's answer to Gödel