tags:

views:

166

answers:

3

I have a secret word (example. dirtydawg)

And using PHP I want to create the uppercase MD5 value of the ASCII equivalent of the secret word.

How do I do this????

+1  A: 

I don't know what you mean with "ASCII equivalent", but I'm assuming what you ask for is this:

$hash = strtoupper(md5('dirtydawg'));

Or am I missing something?

Emil Vikström
+4  A: 

Assuming by 'ASCII equivalent' you mean all chars in the word being ASCII values, you can do

strtoupper(md5(implode(array_map('ord', str_split('dirtydawg')))));

which is equivalent to

$secretWord = 'dirtydawg';
$hash = '';
for($i = 0; $i < strlen($secretWord); $i++) {
    $hash .= ord($secretWord[$i]);
}
echo strtoupper(md5($hash));

Also see the PHP Manual on

  • ord — Return ASCII value of character
Gordon
Whoops: >> explode('','dirtydawg');Exception (code: 0) got thrownexception 'Exception' with message '/usr/share/php/php-shell-cmd.php(121) : eval()'d code:1explode(): Empty delimiter' in /usr/share/php/php-shell-cmd.php:54
hurikhan77
@hurikhan77 fixed. changed to str_split.
Gordon
This works better: md5(join(array_map('ord',str_split('dirtydawg'))));
hurikhan77
+1 for 'functional' php :)
Yossarian
@Yoss +1 for commenting on this... Since I touched ruby I hate code using cluttered loops and other "unfunctional" php code. Too bad, always noone even knows about these array functions, or totally leak the knowlegde to use them to their full potential.
hurikhan77
+1  A: 

Depending on what exactly "the uppercase MD5 value of the ASCII equivalent" means, you presumably either want:

md5(strtoupper($secretword));

or

strtoupper(md5($secretword));

PHP has pretty good documentation - have a look at http://www.php.net/md5 and http://www.php.net/strtoupper

Chris Smith
there is nothing ASCII related in either of the two approaches. The first one is uc'ing the secretword and then md5'ing it, which is not what the OP asked for, since he asked for uppercase md5 value. The second delivers that, but it takes the word's characters for input, not their ASCII value equivalent. That's a different hash result.
Gordon
@Gordon Indeed not. It seemed quite a bizarre request to MD5 the ascii values; I think it makes more sense to assume the OP was a bit confused about charsets and by 'ASCII equivalent' he means the (ascii) text as-is rather than, say, something in ISO-8859-1 or whatever. Of course it still doesn't make much sense with a secret word like 'dirtydawg' :)
Chris Smith