aebstract Posted March 4, 2008 Share Posted March 4, 2008 I want to write something that will make a 6 digit number/code using a certain amount of characters, both numbers and letters. I want it to try every possible option. Could anyone help lead me in the right way of doing this? Quote Link to comment Share on other sites More sharing options...
dbo Posted March 4, 2008 Share Posted March 4, 2008 Sounds like a hacking attempt to me. The algorithm is relatively simple to brute force. Youve got a loop for each digit each subsequent layer is then nested then you just loop while > 10 for each loop. Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 4, 2008 Share Posted March 4, 2008 Simple bruteforce script <?php function brute ($c, $l, $p = FALSE) { foreach ($c as $chr) { $pre = $p . $chr; if ($l > 1) brute($c, $l - 1, $pre); else echo $pre . "<br>\n"; } } $charset = array('a', 'b', 'c', 'd', 'e', 'f'); brute($charset, 5); ?> Have fun, don't get in trouble Quote Link to comment Share on other sites More sharing options...
aebstract Posted March 4, 2008 Author Share Posted March 4, 2008 Sounds like a hacking attempt to me. Actually if you would like to not assume things, I'm gonna make a chart that displays every hex color for web use. Nice assumption though :/ edit: this is gonna be a LOT of results :/ Quote Link to comment Share on other sites More sharing options...
dbo Posted March 4, 2008 Share Posted March 4, 2008 Cool. Shoot us a link when you get done. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2008 Share Posted March 4, 2008 Stating what you are trying to accomplish will always get the quickest solution. Often, stating what you want some piece of code to do does not necessarily get the best or quickest answer. Quote Link to comment Share on other sites More sharing options...
aebstract Posted March 4, 2008 Author Share Posted March 4, 2008 Well I actually got what I needed and did state exactly what I was needing, just didn't say what it was for. Anyhow, with this, is there an easy way to make it group in two doubles and do something such as: aaaaaa aaaabb aaaacc bbaaaa bbaabb etc, in doubles versus each letter individually? Of course keeping those doubles the same character. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 4, 2008 Share Posted March 4, 2008 OMG That will take like 20 years to load the page... I have tried it, and didn't get half way. and mine didn't even load all the colors. Quote Link to comment Share on other sites More sharing options...
aebstract Posted March 4, 2008 Author Share Posted March 4, 2008 Yeah it locked up nicely, thats why I'm gonna do doubles and then study some more and prob. compress it down some more, but I need to know how to keep it grouped together to continue. Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 4, 2008 Share Posted March 4, 2008 Just modify my script. Very easy to do. Quote Link to comment Share on other sites More sharing options...
aebstract Posted March 4, 2008 Author Share Posted March 4, 2008 Yeah I don't understand that script 100% nor what I would need to do to group it in to doubles for each 6 digit doodad. =[ Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 4, 2008 Share Posted March 4, 2008 I'll explain it modified <?php // $c is an array, with every string you want to loop through // $l is the length of the string // $p is a 'prefix' for fruther calls, and will be explained later function brute ($c, $l, $p = FALSE) { // loop through strings foreach ($c as $chr) { // this sets up the prefix... below i will call the function again, and this lets the next call of the function know what this call has parsed $pre = $p . $chr; // this checks if this is the last string to loop through if ($l > 1) // calls itself, lowering the length by 1 each time... brute($c, $l - 1, $pre); else // this is the last call, echo it. echo $pre . "<br>\n"; } } $charset = array('aa', 'bb', 'cc', 'dd', 'ee', 'ff', '00', '11', '22', '33', '44', '55', '66', '77', '88', '99'); brute($charset, 3); ?> Here's what the function does in english... It's called, and it begins looping through the charset... It defines the prefix as the the current entry in the charset, in this example, 'aa'. It checks to see how many characters are left to loop though, in this case, it's 3 Because it's 3, it calls itself again, but decreases the characters left to 2, and defines a prefix variables to 'aa' It repeats the steps above, the prefix is now 'aaaa' and it calls itself again, characters left is now 1.. It defines the prefix as 'aaaaaa' and because it's the last character left, it does not call itself again, but instead, echo's the prefix. The foreach loop continues for bb-99 with 'aaaa' as the prefix. The function ends, and the previous call comes in, and sets the prefix as 'aabb', and because the characters left is now 2 again, it calls itself with 'aabb' as the prefix... And it continues. I don't suggest using this script every time a user wants to see every hex value... but it's great for getting them once, and building a static page that will load much faster Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.