Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help needed converting string hash values into binary...

Help needed converting string hash values into binary...

Scheduled Pinned Locked Moved C#
data-structurescryptographyhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rvp717y
    wrote on last edited by
    #1

    Hi there. I'm writing a hash file converter to convert a set of file hashes from one vendors format to another. As input I have a 32 byte long string containing a hash value stored in a byte[32] array: 6A87A34023132CF8F6D7FDFFB269E3FA I want to convert this into a 16 byte value such that the hex representation is the same, ie: byte[16] = {0x6A,0x87,0xA3 ... }. I hope this makes sense, I'm really struggling with this as my binary skills are not so sharp... Any assistance woud be greatly appreciated. Kind regards, John.

    G 1 Reply Last reply
    0
    • R rvp717y

      Hi there. I'm writing a hash file converter to convert a set of file hashes from one vendors format to another. As input I have a 32 byte long string containing a hash value stored in a byte[32] array: 6A87A34023132CF8F6D7FDFFB269E3FA I want to convert this into a 16 byte value such that the hex representation is the same, ie: byte[16] = {0x6A,0x87,0xA3 ... }. I hope this makes sense, I'm really struggling with this as my binary skills are not so sharp... Any assistance woud be greatly appreciated. Kind regards, John.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      No, it doesn't really make sense... What is a "32 byte long string"? A string consists of characters, not bytes. It looks like you have a string containing 32 characters, but that will occupy 64 bytes. Have you really stored the string in a byte[32] array? How? Have you encoded the string? Then you should decode the string using the same encoding. Once you have the data as a string, you can split it up into 16 separate strings using the Substring method. Then use the Int32.Parse method with NumberStyles.AllowHexSpecifier to parse each two character string into a byte value.

      --- Year happy = new Year(2007);

      R 1 Reply Last reply
      0
      • G Guffa

        No, it doesn't really make sense... What is a "32 byte long string"? A string consists of characters, not bytes. It looks like you have a string containing 32 characters, but that will occupy 64 bytes. Have you really stored the string in a byte[32] array? How? Have you encoded the string? Then you should decode the string using the same encoding. Once you have the data as a string, you can split it up into 16 separate strings using the Substring method. Then use the Int32.Parse method with NumberStyles.AllowHexSpecifier to parse each two character string into a byte value.

        --- Year happy = new Year(2007);

        R Offline
        R Offline
        rvp717y
        wrote on last edited by
        #3

        Yup, you're right - it's a 32 character long string, however it does only occupy 32 bytes - it's not unicode. No encoding required - it's pure ASCII and is stored as such. Anyway after much head scratching, I've got the following code to work: // sHash[32] is a byte array length 32 // eg: the hex value 'A6' is split into sHash[0] & sHash[1] // the following code will amalgamate this character data into a // byte array length 16 int l = 0, iFirst, iSecond; for (int k = 0; k < 32; k += 2) { if (sHash[k] > '9') iFirst = (sHash[k] - 'A') + 10; else iFirst = (sHash[k] - '0'); if (sHash[k+1] > '9') iSecond = (sHash[k+1] - 'A') + 10; else iSecond = (sHash[k+1] - '0'); EnHash[l++] = Convert.ToByte(((iFirst * 16) + iSecond)); } Thanks for your comments above - it's an interesting idea to use int32.parse - I'll give that a go just to see how that works - ultimately it's not too diferent from what I worked out. Kind regards, John.

        L 1 Reply Last reply
        0
        • R rvp717y

          Yup, you're right - it's a 32 character long string, however it does only occupy 32 bytes - it's not unicode. No encoding required - it's pure ASCII and is stored as such. Anyway after much head scratching, I've got the following code to work: // sHash[32] is a byte array length 32 // eg: the hex value 'A6' is split into sHash[0] & sHash[1] // the following code will amalgamate this character data into a // byte array length 16 int l = 0, iFirst, iSecond; for (int k = 0; k < 32; k += 2) { if (sHash[k] > '9') iFirst = (sHash[k] - 'A') + 10; else iFirst = (sHash[k] - '0'); if (sHash[k+1] > '9') iSecond = (sHash[k+1] - 'A') + 10; else iSecond = (sHash[k+1] - '0'); EnHash[l++] = Convert.ToByte(((iFirst * 16) + iSecond)); } Thanks for your comments above - it's an interesting idea to use int32.parse - I'll give that a go just to see how that works - ultimately it's not too diferent from what I worked out. Kind regards, John.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          You did it the hard way, you could use System.Globalization.NumberStyles example: int i=int.Parse("12",NumberStyles.AllowHexSpecifier); sets i to 18 (or 0x12). So you simply could pass a two-char substring to byte.Parse and reduce your for-loop body to a single statement. :)

          Luc Pattyn

          R 1 Reply Last reply
          0
          • L Luc Pattyn

            You did it the hard way, you could use System.Globalization.NumberStyles example: int i=int.Parse("12",NumberStyles.AllowHexSpecifier); sets i to 18 (or 0x12). So you simply could pass a two-char substring to byte.Parse and reduce your for-loop body to a single statement. :)

            Luc Pattyn

            R Offline
            R Offline
            rvp717y
            wrote on last edited by
            #5

            That's very cool Luc, thank you. I think I'll implement that as I guess it will make the program run much faster! Kind regards, John.

            R 1 Reply Last reply
            0
            • R rvp717y

              That's very cool Luc, thank you. I think I'll implement that as I guess it will make the program run much faster! Kind regards, John.

              R Offline
              R Offline
              rvp717y
              wrote on last edited by
              #6

              Okay, having implemented your code I've got a saving of three seconds with 2755 hashes processed. My code took 2 mins 33 secs to process, whereas yours took 2 minutes 30 - that's 18.3 hash conversions per second! This is a small sample file so I'm sure that in the real world application of this the savings will be very significant. Many thanks for your help. Kind regards, John.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups