Oct 20, 2013 04:47 PM
(This post was last modified: Jun 10, 2015 06:02 PM by sp3ctrum.)
[0x01] Permute()
Hello HF members, I havent been on this forum in sometime but I would like to once again be active. Today I will be discussing how to do some simple permutations in perl via the recursive subroutine 'permut' from the Perl Cookbook. So I did try putting this in the cryptography section but as expected it got trashed quickly and disregarded. As for the perl section it seems pretty quite as well. But the WFH section is where I see some nice crypto based stuff being posted so might as well. c: So here we go, for those who are into cryptography I personally like to use perl to do a lot of my handy work, since the language is primarily based on text manipulation and such.
[0x02] Simple Perl Script:
[*] The above script prints out all permutations of the strings supplied as command-line arguments. Example of run:
[*] Output:
[0x03] Script Functions:
[*] Perl Unshift Function: Places the elements from LIST, in order, at the beginning of ARRAY. This is opposite function to shift().
[*] Return Value:
Number of new elements in ARRAY.
[*] Example:
[*] Output:
[*] Perl Splice Function: Removes the elements of ARRAY from the element OFFSET for LENGTH elements, replacing the elements removed with LIST, if specified. If LENGTH is omitted, removes everything from OFFSET onward.
[*] Return Value:
In scalar context undef if no elements removed
In scalar context last element removed
In list context empty list on failure
In list context list of elements removed
[*] Example:
[*] Output:
[0x04] Pimping The Script & Output:
Now of course, I'm sure you must have a simple understanding of the above script and how the functions work. So why not just edit it yourself and make it handle more. For example, instead of it handling words given via command line arguments, edit it to read a text file of words and shift them around to create new words. Use some bash commands to add prefixes and suffixes to each line, digits, padding techniques, bash brace expressions, use text editors such as sed, awk, gawk, nawk to edit lines. The possibilities are pretty much endless.
[*] Example of above code being pipped to sed:
[*] Output:
[0x05] Conclusion:
Well thats my little short tutorial to string permutation in perl. Hope this helps those who are interested in this language and cryptography! :3
Hello HF members, I havent been on this forum in sometime but I would like to once again be active. Today I will be discussing how to do some simple permutations in perl via the recursive subroutine 'permut' from the Perl Cookbook. So I did try putting this in the cryptography section but as expected it got trashed quickly and disregarded. As for the perl section it seems pretty quite as well. But the WFH section is where I see some nice crypto based stuff being posted so might as well. c: So here we go, for those who are into cryptography I personally like to use perl to do a lot of my handy work, since the language is primarily based on text manipulation and such.
[0x02] Simple Perl Script:
Code
#!/usr/bin/perl -w
permut([@ARGV], []);
sub permut
{
my @items = @{ $_[0] };
my @perms = @{ $_[1] };
unless (@items)
{
print "@perms\n";
}
else
{
my (@newitems, @newperms, $i);
foreach $i (0 .. $#items)
{
@newitems = @items;
@newperms = @perms;
unshift(@newperms, splice(@newitems, $i, 1));
permut([@newitems], [@newperms]);
}
}
}Code
perl perm.pl look at me[*] Output:
Code
me at look
at me look
me look at
look me at
at look me
look at me[0x03] Script Functions:
[*] Perl Unshift Function: Places the elements from LIST, in order, at the beginning of ARRAY. This is opposite function to shift().
[*] Return Value:
Number of new elements in ARRAY.
[*] Example:
Code
#!/usr/bin/perl -w
@array = ( 1, 2, 3, 4);
print "Value of array is @array\n" ;
unshift( @array, 20, 30, 40 );
print "Now value of array is @array\n" ;Code
Value of array is 1 2 3 4
Now value of array is 20 30 40 1 2 3 4[*] Perl Splice Function: Removes the elements of ARRAY from the element OFFSET for LENGTH elements, replacing the elements removed with LIST, if specified. If LENGTH is omitted, removes everything from OFFSET onward.
[*] Return Value:
In scalar context undef if no elements removed
In scalar context last element removed
In list context empty list on failure
In list context list of elements removed
[*] Example:
Code
#!/usr/bin/perl -w
@array = ("a", "e", "i", "o", "u");
@removedItems = splice(@array, 0 , 3, ("A", "E", "I"));
print "Removed items: @removedItems\n";[*] Output:
Code
Removed items: a e i[0x04] Pimping The Script & Output:
Now of course, I'm sure you must have a simple understanding of the above script and how the functions work. So why not just edit it yourself and make it handle more. For example, instead of it handling words given via command line arguments, edit it to read a text file of words and shift them around to create new words. Use some bash commands to add prefixes and suffixes to each line, digits, padding techniques, bash brace expressions, use text editors such as sed, awk, gawk, nawk to edit lines. The possibilities are pretty much endless.
[*] Example of above code being pipped to sed:
Code
perl perm.pl hey you | sed -e 's/\ /\n/g'
-- The above command does a simple string permutation of "hey" & "you" then pipes to sed to use substitution, starting a new line for each string given.Code
you
hey
hey
you[0x05] Conclusion:
Well thats my little short tutorial to string permutation in perl. Hope this helps those who are interested in this language and cryptography! :3



