views:

358

answers:

6

I have no clue how to validate this string. I am simply supplying an IV for an encryption, but can find no "is_hex()" or similar function, I can't wrap my head around it! I read on a comment in the php documentation (user contrib. notes) this:

if($iv == dechex(hexdec($iv))) {
  //True
} else {
  //False
}

But that doesn't seem to work at all.. It only says false. If it helps my input of my IV would be this:

92bff433cc639a6d

+2  A: 

Is there any reason not to match against a simple RE like "[0-9A-Fa-f]+"? (edit: possibly with a '^' at the beginning and '$' at the end to assure you've matched the whole string).

Jerry Coffin
I tried, $iv = "92bff433cc63da6W" if(preg_match('/[0-9A-Fa-f]+/', $iv)) { $enc = mcrypt_encrypt( MCRYPT_BLOWFISH, $key, $data, MCRYPT_MODE_CBC, pack("H*",$iv)); $ENCRSLT = bin2hex($enc); } else { $ENCRSLT = "Error: Key or IV is not Hexidecimal. Valid characters: 0-9a-f."; } But it doesn't validate (the obvious W at the end)
oni-kun
Oops, comments wouldn't format, sorry. lol
oni-kun
Go for the regexp: "^[0-9a-f]$" as suggested by Jerry. And drop the upper-case A-F as it is not supported according to the error message you pasted.
bjarkef
Alrighty, It works perfectly. I was actually going to try that myself, guess I'm not too bad at regex myself. Thanks
oni-kun
better to find alone non-range character than check whole sting.
Col. Shrapnel
A: 

Maybe this comment will help you:

http://php.net/manual/en/function.http-chunked-decode.php

He provides his is_Hex function.

breiti
+6  A: 

use function : ctype_xdigit

http://php.net/ctype_xdigit

<?php
$strings = array('AB10BC99', 'AR1012', 'ab12bc99');
foreach ($strings as $testcase) {
    if (ctype_xdigit($testcase)) {
        echo "The string $testcase consists of all hexadecimal digits.\n";
    } else {
        echo "The string $testcase does not consist of all hexadecimal digits.\n";
    }
}
?> 

The above example will output:

The string AB10BC99 consists of all hexadecimal digits.

The string AR1012 does not consist of all hexadecimal digits.

The string ab12bc99 consists of all hexadecimal digits.

Haim Evgi
Thank you! I just dislike using regex sometimes, I'm glad there was something barebone there. Must look more in the list of functions next time.
oni-kun
+1  A: 

Your input is too large. From the PHP manual of dexhex

The largest number that can be converted is 4294967295 in decimal resulting to "ffffffff"

So you'll be better off using a RegEx, which have already been supplied here by others.

nikc
The hex was an 8 byte string for the IV rather than a number, regex and better ctype_xdigit did the job.
oni-kun
A: 

maybe this helps:

http://codeigniter.com/wiki/hexadecimal_validation/

Auro
A: 

And because there is always more than one way to do it (even in PHP), you could try something like this:

if ( 0 == strcspn($iv,'0123456789abcdef') ) {
    // TRUE
}
else {
    // FALSE
}

Should be roughly equivalent to, but faster than, preg_match('{[^0-9a-f]}, $iv), although in terms of learninging curves, preg_match is significantly faster to understand ...

Duncan