1

Please explain me what does this piece of code do.

h should be 32Byte result from sha256 calculation.

I am rewriting parts of this code for my project in C++ and I'm not sure if this switches byte order per 4byte chunk or change byte order on whole 32byte number.

def reverse_hash(h):
    return struct.pack('>IIIIIIII', *struct.unpack('>IIIIIIII', h)[::-1])[::-1]

And, how does this array index work ?

   [::-1]

Thanks for any and all info

BENARD Patrick
  • 30,363
  • 16
  • 99
  • 105
semtexzv
  • 129
  • 2
  • 10

2 Answers2

3

[::-1] creates a new list with reversed order of elements

volcano
  • 3,578
  • 21
  • 28
0

[::-1] reverse order of elements in a list, so this script change order of each 4-bytes subsequence (in order to change endiannes, I suppose):

>>> h = ''.join(map(str, range(0,21)))
>>> h
'01234567891011121314151617181920'
>>> struct.pack('>IIIIIIII', *struct.unpack('>IIIIIIII', h)[::-1])[::-1]
'32107654019821114131615181710291'

Equivalent expression:

>>> struct.pack('<IIIIIIII', *struct.unpack('>IIIIIIII', h))
'32107654019821114131615181710291'
alko
  • 46,136
  • 12
  • 94
  • 102
  • Thank you, totally answered my question. – semtexzv Jan 02 '14 at 13:10
  • @user3153405 You're welcome. Feel free to [accept](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) appropriate answer if you feel your question is resolved. – alko Jan 02 '14 at 13:13