[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  sha256 to big integer

sha256 to big integer

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Fearless   (8 posts)  [Biography] bio
Date Sun 15 Nov 2020 11:32 PM (UTC)

Amended on Mon 16 Nov 2020 02:41 AM (UTC) by Nick Gammon

Message
How do I get the output of sha256 into a big integer? (Yes, I'm going to do math on it.) Everything I tried doesn't work.


str = "foo"

ihash = bc.number(utils.sha256(str))
Note("bc: " .. tostring(ihash))

shex = utils.tohex(utils.sha256(str))
Note("hex: " .. shex)

ihash = bc.number(shex)
Note("bc: " .. tostring(ihash))

shex = "0x" .. shex
ihash = bc.number(shex)
Note("bc: " .. tostring(ihash))
[Go to top] top

Posted by Nick Gammon   Australia  (23,016 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Mon 16 Nov 2020 02:41 AM (UTC)
Message
You can do it by breaking down the hex number into chunks, and then using a loop to build up the final number, multiplying each chunk by the appropriate power of 2, like this:


str = "foo"

shex = utils.tohex(utils.sha256(str))
Note("hex: " .. shex)

mul = 0
out = bc.number (0)


for i = 64, 1, -8 do
  local hexdigits = string.sub (shex, i - 7, i)
  local decdigits = tonumber (hexdigits, 16)
  out = out + bc.number (decdigits) * bc.pow (2, mul)
  mul = mul + 32
end -- for

print (out)



Result:


19970150736239713706088444570146546354146685096673408908105596072151101138862

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Fiendish   USA  (2,525 posts)  [Biography] bio   Global Moderator
Date Reply #2 on Mon 16 Nov 2020 02:44 AM (UTC)

Amended on Mon 16 Nov 2020 02:46 AM (UTC) by Fiendish

Message
[EDIT] Looks like Nick beat me to the answer!

bc.number doesn't accept hex formatted strings, so you have to break it apart and do some recombinant arithmetic.

Try this from the BC library test code:


function hex2bc(s)
   local x=bc.number(0)
   for i=1,#s do
      x=16*x+tonumber(s:sub(i,i),16)
   end
   return x
end


shex = utils.tohex(utils.sha256("foo"))
Note("Hex: " .. shex)
Note("BC: " .. bc.tostring(hex2bc(shex)))

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (23,016 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Mon 16 Nov 2020 03:17 AM (UTC)

Amended on Mon 16 Nov 2020 03:19 AM (UTC) by Nick Gammon

Message
My answer was specifically written for a 64-character hex string (256 bits) whereas the code Fiendish posted would be more general (for any length hex string).

It gives the same results, which is kind-of comforting.


Hex: 2C26B46B68FFC68FF99B453C1D30413413422D706483BFA0F98A5E886266E7AE
BC: 19970150736239713706088444570146546354146685096673408908105596072151101138862

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Fearless   (8 posts)  [Biography] bio
Date Reply #4 on Mon 16 Nov 2020 04:19 AM (UTC)
Message
That function is great. I could have written something a bit messier but I never would have found that one. Thanks.

Google knows exactly one URL with "function hex2bc(s)" but knowing the answer and then searching for it is cheating.
[Go to top] top

Posted by Fiendish   USA  (2,525 posts)  [Biography] bio   Global Moderator
Date Reply #5 on Mon 16 Nov 2020 06:18 AM (UTC)
Message
It came from https://github.com/LuaDist/lbc/blob/93ca33ce5cbc31b1bfe3e985e10afcfb9e9c8b92/test.lua#L153-L159

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,243 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]