0

I got a packet from a UDP socket. The last 64 bytes of the packet are an RSA-512 SHA-256 digital signature. Using struct.unpack() I'm able to get it out into a string.

When I print the string, it looks like 'Output_1'

I want the string to look like 'Output_2'

from hashlib import sha256

h = sha256()
h.update("Some sample string here!")
print 'Output_1:', h.digest()
print 'Output_2:', h.hexdigest()

So, given the string that is the output of h.digest() can it be loaded into hashlib.sha256() and printed out like h.hexdigest() ?

sudhansh_
  • 125
  • 1
  • 2
  • 14
  • "When I print the string, it looks like 'Output_1'. I want the string to look like 'Output_2'". I guess there's something missing there? –  Feb 20 '18 at 02:03
  • Do you know what format those 64 input bytes are supposed to be? You're also not showing your unpacking code. –  Feb 20 '18 at 02:05
  • The suggested duplicate asks about Python 3, but the marked answer (albeit a bit outdated for Python 3) works for Python 2 just fine. –  Feb 20 '18 at 02:08
  • I tried the encode/decode utf-8 but I get an error that some value is out of range (greater than 128). The hash has byte values greater than 128 in it – sudhansh_ Feb 20 '18 at 02:47
  • The answer you gave on using hexlify worked by removing the encode/decode methods from the end. I passed the raw data to hexlify and it worked as expected. – sudhansh_ Feb 20 '18 at 02:55
  • Whoops; I had overlooked the encode/decode bits. Technically, you could then put the final Python 2 solution as an answer to this question, since it's only half a duplicate of sorts. With a reference to the proposed duplicate answer. –  Feb 20 '18 at 03:01
  • NB: this has of course nothing to do with sha256 and the like; your question is essentially about how to convert bytes to a hexadecimal representation. –  Feb 20 '18 at 03:02

0 Answers0