Nmap Development mailing list archives

Re: Detecting/parsing a hex string


From: "Patrick Donnelly" <batrick.donnelly () gmail com>
Date: Wed, 8 Oct 2008 20:23:52 -0600

On Wed, Oct 8, 2008 at 12:22 PM, David Fifield <david () bamsoftware com> wrote:
On Wed, Oct 08, 2008 at 11:41:36AM -0600, Patrick Donnelly wrote:
On Wed, Oct 8, 2008 at 5:25 AM, Ron <ron () skullsecurity net> wrote:
Hi all,

I need to detect whether or not a string is an ascii representation of a hex
string (eg, "12ab3d1a3badabeea1...."), and, if it is, to convert it to a
binary string. Is there an easy way to do that (maybe with
bin.pack/bin.unpack)?

Thanks,
Ron

a = "414243"
if string.find(a, "^%x+$") then
  a = string.gsub(a, "%x%x", function(a) return
string.char(tonumber(a, 16)) end) --> a = "ABC"
end

I suspect this won't work when the string is long enough to overflow the
capacity of a double, which a 128-bit key will be.

Better than checking first whether the operation will produce an error
is just to do the operation and catch any error produced. However (from
reading the source code), bin.unpack appears to ignore non-hex
characters in a hex conversion:

     for (ii = 0; ii < l; ii++) {
       if (isxdigit(a[ii])) {
         ...
       } else if (isspace(a[ii])) {
         /* ignore */
       } else {
         /* err ... ignore too*/
       }
     }

I think that's a bug that should be fixed.

The code I gave works on two characters at a time...


-- 
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant

_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://SecLists.Org


Current thread: