Results 1 to 12 of 12

Thread: [RESOLVED] SHA-256 as decimal

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    53

    Resolved [RESOLVED] SHA-256 as decimal

    Hi,

    I'm trying to write some code to produce a checksum for a barcode. The check sum states "All decimal values of the SHA256 checksum are added", and "The decimal 2 digit modulo 100 of the calculated sum are the two checksum digits. E.g.: Checksum Bytes 90, 165, 234 (hex. 5A,A5,EA) -> 90+165+234=489 -> 489 mod 100 = 89"

    I have some code working to create a Hexadecimal number from a string and this produced the same hex sting as the online converters, but I am at a loss of how to get this to a decimal value to then work out the final checksum. Any ideas?

    eg hex = 84ee032df238e3026bcd8eadbfcff97861a8fad1ca8cd6c2865d89797ba4eea0

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SHA-256 as decimal

    I believe that this should do the job, although it's untested.
    vb.net Code:
    1. Private Function HexToBytes(hex As String) As Byte()
    2.     If hex.Length Mod 2 <> 0 Then
    3.         'Add a leading 0.
    4.         hex = "0" & hex
    5.     End If
    6.  
    7.     Dim index = 0
    8.     Dim bytes As New List(Of Byte)
    9.  
    10.     Do Until index = hex.Length
    11.         bytes.Add(Convert.ToByte(hex.Substring(index, 2), 16))
    12.     Loop
    13.  
    14.     bytes.Reverse()
    15.  
    16.     Return bytes.ToArray()
    17. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    53

    Re: SHA-256 as decimal

    Thanks.

    If I call the function - TextBox3.Text = HexToBytes(TextBox2.Text)

    I get an error - Value of type 'Byte()' cannot be converted to 'String'

    Is that me doing something wrong placing it in the text box?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SHA-256 as decimal

    Please put some thought into the problem for yourself. Did you even look at my code to see what it does? If my method returns a Byte array, why would you think that you could assign that result to the Text property of a TextBox? You indicated that you already know what to do with an array of Bytes but you don't know how to get an array of Bytes from a hexadecimal String. I just provided code to do the part that you don't know how to do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    53

    Re: SHA-256 as decimal

    Fair enough, I for some reason thought the function return a string but I see that isn't the case.

    I can send my hex string which is in a text box to the function HexToBytes(textbox2.text) and I see that the array "Bytes" holds the data I need. I can work on getting that as a decimal string.

    However, when I run the code to see what happens, it crashes with no error and I can't seem to work out why

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    53

    Re: SHA-256 as decimal

    Quote Originally Posted by NigeH View Post

    However, when I run the code to see what happens, it crashes with no error and I can't seem to work out why

    Seems to be looping in the Do Until loop infinitely. Can see the index is not increasing, so added and increment. Had to change - Do Until index = hex.Length - 1

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SHA-256 as decimal

    Quote Originally Posted by NigeH View Post
    Seems to be looping in the Do Until loop infinitely.
    Sorry, should have incremented the index in the loop:
    vb.net Code:
    1. Do Until index = hex.Length
    2.     bytes.Add(Convert.ToByte(hex.Substring(index, 2), 16))
    3.     index += 2
    4. Loop
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    53

    Re: SHA-256 as decimal

    No worries, that is working now and I have a result in the bytes array.

    If I print each element in the byte array, will that give me a decimal? Seems to give me a number out, haven't check if it is what I'm after yet.

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    53

    Re: SHA-256 as decimal

    So it seems as if I am slightly out with the decimal conversion. I have a few examples...

    HEx - 503fe8718d1e6ac380fa7529bfc01235345f63fa8f6af33af3ba1fe64212f65a

    My output - 9024618662303118624358243106143250999552531819219141117250128195106301411132326380
    online converter - 36297943518230324053527204388021767669526841412357582462543635623660018136666


    Hex - 68e656b251e67e8358bef8483ab0d51c6619f3e7a1a9f0e75838d41ff368f728

    My output - 4024710424331212568823124016916123124325102282131765872248190881311262308117886230104
    OnLine Converter - 47447509435240178963798524362534432113195114210189468302358324674552893339432

    Not sure why. All I am doing is.....
    Code:
            For Each elem As Byte In bytes
                TextBox3.Text &= elem
            Next

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    53

    Re: SHA-256 as decimal

    Scrap all that. I've sorted it

    Thanks for the help

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: SHA-256 as decimal

    I assume, and for anyone that might wonder what the solution probably was, is the decimal digits is a bit of a red herring, i.e. you don't need to convert the string to a bunch of decimal digits.

    Once you have the byte array, you just add the bytes together (i.e. loop through the array and add them to a Long type for instance), and then just do a Mod 100 on the resulting Long value, and use the result (not converted to hex).
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    53

    Re: SHA-256 as decimal

    Quote Originally Posted by passel View Post
    I assume, and for anyone that might wonder what the solution probably was, is the decimal digits is a bit of a red herring, i.e. you don't need to convert the string to a bunch of decimal digits.

    Once you have the byte array, you just add the bytes together (i.e. loop through the array and add them to a Long type for instance), and then just do a Mod 100 on the resulting Long value, and use the result (not converted to hex).
    Absolutely that. In fact I added them together in the loop I was pulling the hex string together in, as don't really need the hex string either.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width