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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |