Author Topic: [PHP] A more dynamic approach?  (Read 2341 times)

0 Members and 1 Guest are viewing this topic.

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
[PHP] A more dynamic approach?
« on: May 31, 2013, 05:20:52 am »
Sup fools. I had a question about more efficient PHP-ing. I just whipped up this little encoding script a few minutes ago:

Code: (php) [Select]
<table><tr><td>Input String Here: <form method='post' action='?crypt'>
                   <textarea rows='20' cols='40' name='crypt'></textarea><br />
               <input type='submit' value='Calculate' /></form></td></tr>

<?php

function strtoheX($str)
{
    
$h '';
    for(
$i 0$i strlen($str); $i++)
    {
        
utf8_decode($str);
        
$h .= dechex(ord($str[$i]));
    }
    return 
'0x'.$h;
}

if(isset(
$_POST['crypt']))
{
?>

    <table>
        <tr><td>md5: <input type='text' size='45' value='<?php echo md5($_POST['crypt']); ?>' /></td></tr>
        <tr><td>sha1: <input type='text' size='45' value='<?php echo sha1($_POST['crypt']);?>' /></td></tr>
        <tr><td>sha256: <input type='text' size='45' value='<?php echo hash('sha256'$_POST['crypt']); ?>' /></td></tr>
        <tr><td>crypt: <input type='text' size='45' value='<?php echo crypt($_POST['crypt']); ?>' /></td></tr>
        <tr><td>crc32: <input type='text' size='45' value='<?php echo crc32($_POST['crypt']); ?>' /></td></tr>
        <tr><td>urlencode: <input type='text' size='45' value='<?php echo urlencode($_POST['crypt']); ?>' /><input type='submit' name='udec' value='Decode' /></td></tr>
        <tr><td>base64 encode: <input type='text' size='45' value='<?php echo base64_encode($_POST['crypt']); ?>'/><input type='submit' name='basedec' value='Decode' /></td></tr>
        <tr><td>hexadecimal: <input type='text' size='45' value='<?php echo strtoheX($_POST['crypt']); ?>' /><input type='submit' name='hexdec' value='Decode' /></td></tr>
    </table><?php
}
else
{
?>

       
        <table>
            <tr><td>md5: <input type='text' size='45' /></td></tr>
            <tr><td>sha1: <input type='text' size='45' /></td></tr>
            <tr><td>sha256: <input type='text' size='45' /></td></tr>
            <tr><td>crypt: <input type='text' size='45' /></td></tr>
            <tr><td>crc32: <input type='text' size='45' /></td></tr>
            <tr><td>urlencode: <input type='text' size='45' /><input type='submit' name='urldec' value='Decode' /></td></tr>
            <tr><td>base64 encode: <input type='text' size='45' /><input type='submit' name='basedec' value='Decode' /></td></tr>
            <tr><td>hexadecimal: <input type='text' size='45' /><input type='submit' name='hexdec' value='Decode' /></td></tr>
        </table><?php
}
    
?>


Now I know for a fact that there is a more efficient way to go about this but I'm not sure how. I want:

1- For the text input in the textarea to stay after I click the submit button

2 - As you can see I want it to calculate a string to a variety of different hashes etc. I want to find a more dynamic way to input all the hashes to their input boxes.

Thanks for any help given. I'm aware this code is sloppy like I said I just wrote it in a few minutes.
« Last Edit: May 31, 2013, 09:22:17 pm by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline Snayler

  • Baron
  • ****
  • Posts: 812
  • Cookies: 135
    • View Profile
Re: [PHP]A more dynamic approach?
« Reply #1 on: May 31, 2013, 10:28:01 am »
This is how you accomplish #1:
Code: (php) [Select]
<table><tr><td>Input String Here: <form method='post' action='?crypt'>
                   <textarea rows='20' cols='40' name='crypt'>
<?php
                  
IF (isset($_POST['crypt'])) {
                     echo 
$_POST['crypt'];
                  }
?>

                    </textarea><br />
               <input type='submit' value='Calculate' /></form></td></tr>

As for #2, I've no idea how to make it more dynamic from the top of my head. I would do it the same way as you did, but I'm still very green at php, TBH.

EDIT: My code had an error, fixed now.
« Last Edit: May 31, 2013, 01:50:03 pm by Snayler »

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [PHP]A more dynamic approach?
« Reply #2 on: May 31, 2013, 11:06:50 am »
quick mockup:
Code: (php) [Select]

<?php


class Hasher {


public function bringMeTheHash($plain_text)
{
$hash_types = ['md2''md4''md5''sha1''sha256''sha384''sha384''sha512''sex'];
 
echo '<ul>';
foreach($hash_types as $hash_type)
{
$hash hash($hash_type$plain_text);
if($hash === false)
{
$hash "Hash type " $hash_type " is not found";
}
echo '<li>' $hash_type ' : ' $hash '</li>';
}
echo '</ul>';
}
}


$hash = new Hasher;
$hash->bringMeTheHash("I don't know why i use OOP");
« Last Edit: May 31, 2013, 11:07:42 am by Factionwars »
~Factionwars

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [PHP]A more dynamic approach?
« Reply #3 on: May 31, 2013, 11:20:22 am »
DOUBLE POST :D  I reprogrammed it because i am bored and want to learn you some things :)

Code: (php) [Select]
<?php

abstract class Hasher {

public function base64($text
{
return base64_encode($text);
}

public function json($text
{
return json_encode($text);
}

public function serialize($text)
{
return serialize($text);
}

public function sex($text)
{
return 'i am sexy and i know it pummmadada';
}


}

$string 'Dynamic as shit';

$hash_types = ['md2''md4''md5''sha1''sha256''sha384''sha384''sha512''sex''base64''json''serialize'];
echo 
'<ul>';
foreach(
$hash_types as $hash_type)
{
$hash '';
if(!($hash = @call_user_func('Hasher::' $hash_type$string)))
{
$hash hash($hash_type$string);
if($hash === false)
{
$hash "Hash type " $hash_type " is not found";
}
}
echo '<li>' $hash_type ' : ' $hash '</li>';
}
echo 
'</ul>';
~Factionwars

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: [PHP]A more dynamic approach?
« Reply #4 on: May 31, 2013, 11:41:06 am »
I also decided to write one.
Code: (PHP) [Select]

function string_length($input) {
   // Example function.
   return strlen($input);
}


$input = 'hello!';
$hash_names = array('md5', 'crc32', 'RIPEMD160', 'sha1', 'string_length', 'cat_in_the_hat');


foreach ($hash_names as $hash_name) {
   $hash = @hash($hash_name, $input);
   if ($hash === false && ($hash = @call_user_func($hash_name, $input)) === null) {
      continue;
      // $hash = 'Not a function.';
   }
   
   echo '<tr><td>';
   echo $hash_name . ": <input type='text' size='" . (strlen($hash) + 1) . "' value='" . $hash . "'>";
   echo '</td></tr>';
   echo '<br>';
}
Halve asleep so yeah.
May improve it when I'm fully awake.
« Last Edit: May 31, 2013, 12:21:11 pm by Fur »

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [PHP]A more dynamic approach?
« Reply #5 on: May 31, 2013, 07:15:01 pm »
Thanks guys. Will try this out when I have free time. Factionwars, I can't tell just by reading but how does your code output the encoded string? Does it input all the different types of hashes into text boxes like my original code?
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [PHP]A more dynamic approach?
« Reply #6 on: May 31, 2013, 07:22:37 pm »
Thanks guys. Will try this out when I have free time. Factionwars, I can't tell just by reading but how does your code output the encoded string? Does it input all the different types of hashes into text boxes like my original code?
I did not comment it because I want you to reverse the ideas :
~Factionwars

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [PHP]A more dynamic approach?
« Reply #7 on: May 31, 2013, 08:28:38 pm »
Oh I see. Cool thanks, looks like I'm going to have some fun with this.

EDIT: I noticed this is only with hashes. I couldn't include things like urlencode, binary, or hex into the foreach loop. That's for the first one you coded Factionwars. The second one I have yet to test although looking at it again I bet that one would be more useful for my purposes.

EDIT AGAIN: Nevermind I got it. Thanks for posting it to me this way by the way. It's so much more enjoyable to break down code in order to understand it instead of just being told. You've given me a fun day. I've added urlencode, binary, ripemd160, hex, crypt, and crc32 algos. Now I just need to get it to use my textarea input.
« Last Edit: May 31, 2013, 09:38:42 pm by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline wookie

  • Peasant
  • *
  • Posts: 68
  • Cookies: -4
    • View Profile
Re: [PHP]A more dynamic approach?
« Reply #8 on: June 02, 2013, 07:56:07 am »
EDIT: I noticed this is only with hashes. I couldn't include things like urlencode, binary, or hex into the foreach loop. That's for the first one you coded Factionwars. The second one I have yet to test although looking at it again I bet that one would be more useful for my purposes.


[/size]Yes you can, add a switch case in.
[size=78%]

[/size]
[/size]
Code: [Select]
<?php
if(function_exists($enc_type)){
   
$hash=$enc_type($str);
}else{
switch(
$enc_type){
  case 
'binary':
     
// code here
  
break;
  case 
'hex':
     
// code here
  
break;
  default:
    
// handle
}
}

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [PHP] A more dynamic approach?
« Reply #9 on: June 02, 2013, 01:22:24 pm »
I've actually since found a way to incorporate other encryption methods, but I think your way might be more elegant. Thanks for the input. 
« Last Edit: June 02, 2013, 01:26:02 pm by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: [PHP] A more dynamic approach?
« Reply #10 on: June 02, 2013, 01:38:59 pm »
other encryption methods
I believe these aren't encryption methods, they're encoding methods (with the exception of hashes, they're hash functions).

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [PHP] A more dynamic approach?
« Reply #11 on: June 03, 2013, 04:16:21 am »
Oops! Was tired as shit last night....and also a little less than sober.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [PHP] A more dynamic approach?
« Reply #12 on: June 03, 2013, 09:32:00 am »
Oh I see. Cool thanks, looks like I'm going to have some fun with this.

EDIT: I noticed this is only with hashes. I couldn't include things like urlencode, binary, or hex into the foreach loop. That's for the first one you coded Factionwars. The second one I have yet to test although looking at it again I bet that one would be more useful for my purposes.

EDIT AGAIN: Nevermind I got it. Thanks for posting it to me this way by the way. It's so much more enjoyable to break down code in order to understand it instead of just being told. You've given me a fun day. I've added urlencode, binary, ripemd160, hex, crypt, and crc32 algos. Now I just need to get it to use my textarea input.
Great to see you are having fun. And yes after creating the first example i thought about not having encoding methods, that's why i implemented it that way in the second one.

Also note a few little things, the class is abstract ("abstract class MyClass")  this means that the class can never be instantiated and will only server as function wrapper, base class (to be extended by other classes) or a template. Second thing is that because the class is abstract you can only call the functions in a static way that being MyClass::function(); and this is also the only way to use the call_user_func, because that will not work with a object class in this dynamic method.  So the class in the second example really only serves as a function wrapper or as a front-end for the functions where you can declare some more logic and formatting for some purposes :D.  So for example you could override the MD5 hashing function in the class.
~Factionwars