Reputation: 41
I'm trying to assemble a byte array in Python for a signature that resembles the following:
String A + B are utf-8 which I converted to utf-8 using unicode(string, 'utf-8')
I've tried converting each item to a byte array and joining them using the plus symbol e.g.
bytearray(len(a)) + bytearray(a, "utf-8")...
I've also stried using struct.pack e.g.
struct.pack("i", len(a)) + bytearray(access_token, "utf-8")...
But nothing seems to generate a valid signature. Is this the right way to make the above byte array in Python?
Upvotes: 2
Views: 1703
Reputation: 148965
The last question is about endianness of the 4 byte lengths, but you can easily control it with the struct module.
I would use
def dopack(A, B, LongA):
fmt='!' # for network order, use < for little endian, > for big endian, = for native
fmt += 'i'
buf = pack(fmt, len(A))
buf += A
buf += pack(fmt, len(B))
buf += B
b = bytes(LongA)
buf += pack(fmt, len(b))
buf += B
In this way, the LongA value is coded in ASCII, but it is easier, and you can just do int(b)
do convert it back to a long.
Upvotes: 2