Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Recursive Subroutine permute | Basic Permutations

[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:
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]);

  }
}
}
[*] The above script prints out all permutations of the strings supplied as command-line arguments. Example of run:
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" ;
[*] Output:
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.
[*] Output:
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
Well, I can't write perl, but I can read a lot of it due to my python exprience ;)
this is a great tutorial!
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
(Oct 20, 2013 05:23 PM)Goodies Wrote:  Well, I can't write perl, but I can read a lot of it due to my python exprience ;)
this is a great tutorial!

Thanks Goodies :3 and yes this can apply in any coding language. Its also a great practice for those who are stepping into programming.
Nice write up 3DES!
Reminds me the days your tutorials were posted on Z+
Good times :p

P.S I still have the DB, so no worries, I'll be uploading them in a form of Wiki anytime soon
(Oct 21, 2013 01:35 PM)Zhen. Wrote:  Nice write up 3DES!
Reminds me the days your tutorials were posted on Z+
Good times :p

P.S I still have the DB, so no worries, I'll be uploading them in a form of Wiki anytime soon

Thanks Zero! and finally, I have been trying to contact you forever about the matter c: PM me your skype if you have one, or Im sure Ill catch you in the Dynasty IRC!
That's really good to see you again sp3ct <3
Great thread(I even didn't read, I'm sure it is)
O o
/¯/_____________________________________/¯¯
| 0x41414141 ~~~
\_\¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ¯\__
/█\
/ \
A really great guide you got there. 🤝
af
Oh shit is that who I think it is? Nice tut, bro. Good to see you're still around
#FREEKNIGHT

If you know me, PM me for new XMPP. Old one is dead
(Oct 22, 2013 08:47 PM)Ichi Wrote:  Oh shit is that who I think it is? Nice tut, bro. Good to see you're still around

Yes I is back at HF lol hmu on skype or something bro!
Skype username: xx.des.xx

Others are welcome to add me c:


Possibly Related Threads...
Thread / Author Replies Views Last Post
by inactiveSquadSep 12, 2016 07:17 AM 4 193 Sep 12, 2016 07:17 AM
Last Post: Fusion_elite
by Th3PonyWizardJul 24, 2014 06:15 AM 3 387 Jul 24, 2014 06:15 AM
Last Post: /jeremy/