|
-
Jun 23rd, 2020, 03:04 AM
#1
Thread Starter
Member
[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
-
Jun 23rd, 2020, 03:16 AM
#2
Re: SHA-256 as decimal
I believe that this should do the job, although it's untested.
vb.net Code:
Private Function HexToBytes(hex As String) As Byte()
If hex.Length Mod 2 <> 0 Then
'Add a leading 0.
hex = "0" & hex
End If
Dim index = 0
Dim bytes As New List(Of Byte)
Do Until index = hex.Length
bytes.Add(Convert.ToByte(hex.Substring(index, 2), 16))
Loop
bytes.Reverse()
Return bytes.ToArray()
End Function
-
Jun 23rd, 2020, 03:34 AM
#3
Thread Starter
Member
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?
-
Jun 23rd, 2020, 03:47 AM
#4
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.
-
Jun 23rd, 2020, 06:47 AM
#5
Thread Starter
Member
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
-
Jun 23rd, 2020, 06:55 AM
#6
Thread Starter
Member
Re: SHA-256 as decimal
 Originally Posted by NigeH
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
-
Jun 23rd, 2020, 07:00 AM
#7
Re: SHA-256 as decimal
 Originally Posted by NigeH
Seems to be looping in the Do Until loop infinitely.
Sorry, should have incremented the index in the loop:
vb.net Code:
Do Until index = hex.Length
bytes.Add(Convert.ToByte(hex.Substring(index, 2), 16))
index += 2
Loop
-
Jun 23rd, 2020, 07:09 AM
#8
Thread Starter
Member
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.
-
Jun 23rd, 2020, 08:56 AM
#9
Thread Starter
Member
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
-
Jun 23rd, 2020, 11:00 AM
#10
Thread Starter
Member
Re: SHA-256 as decimal
Scrap all that. I've sorted it
Thanks for the help
-
Jun 23rd, 2020, 01:21 PM
#11
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
-
Jun 23rd, 2020, 01:50 PM
#12
Thread Starter
Member
Re: SHA-256 as decimal
 Originally Posted by passel
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|