Hello,

After dilligent searching, I've been unable to find any discussions of how to efficiently translate (tr// or y//) strings when one or both of the translation maps are variable.

Understanding that there are many with strongly held beliefs that passwords of any sort do not belong in scripts or files, I'm, never the less, forced to deal with this very situation.

So, after perusing the applicable archives, I've generated my own modified version of a rot-13 type routine that includes, in the obfuscation, all printable ascii characters (the space being considered non-printable).

Within the routine, included below, we generate the appropriate "destination" translation string depending on how far we wish to rotate the printable ascii table. This then makes it necessary to eval the tr//.

As far as Programmer efficiency goes, this is a good option as it is short, effective, and relatively easy to understand and maintain. However, machine wise, evals are expensive. AYRNIEU has a rot-13 module posted that goes through each character of the string and modifies it as needed. This option might be speed and space efficient, but it is, at least arguably, less programmer/maintenance efficient.

Viewing the caesar solving routine, tachyon's solution to have a static translation of one off, and iterating with it the number of times you wish to rotate does a pretty good job of balancing the programmer and speed efficiency. Space efficiency is still not an issue.

Swinging wildly in the space inefficiency arena, one could create a separate static translation for all options, should be faster than tachyon's iteration option, but ugly in the programmer & space areas.

Another option to reduce iterations, and space needed, would be to have static translations for 1, 2, 4, 8, 16, 32, 64 rotations off...

So, how efficient are the various non-eval versions? (I'm a neophyte when it comes to benchmarks; but, I've seen an example benchmark that shows just how bad eval's are when done in bulk) Which options do you like and why? What other options have I not explored?

-Scott

###################################################################### +###### # pwdrot uses the idea of rot13 and expands the characters affected by + the # rotations to include all 94 normal printable ascii characters. # ie. chr(33) '!' - chr(126) '~'. The rotations are thus mod 94. # # Used to obfuscate passwords, NOT encrypt them. # # Default rotation, if not supplied, is 47 ###################################################################### +###### sub pwdrot { my $pwd = shift; my $degree = (@_ > 0) ? ((shift) % 94) : 47; if ($degree == 0) { return $pwd; } if (length($str) == 0) { return $pwd; } $rangestr = "\\" . sprintf("%03lo",$degree+33) . "-\\176\\041-\\" +. sprintf("%03lo",$degree+32); { local $_; eval { #Can't do string interpolation within 'tr' without 'eva +l'ing it. $_ = $pwd; eval "tr[\041-\176][$rangestr];"; $_; }; $pwd = $_; } return $pwd; }

In reply to efficient string translation? by 5mi11er

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.