Ruby unpack to Python -


I'm trying to convert some Ruby code into Python. I'm having trouble with this line:

  digest :: MD5 digist (message) .mpack ('L *')   

I think Is that I should use the struct module and hashlib , but if I do:

  struct.unpack ('L', Hashlib.md5 (message) .digest ())   

I get this error:

  struct.error: unpack length of 4 bytes of a object Required   

What should I do? Thanks,

Rubik

P.S. Output should be a list of 4 x 32 bits:

  irb (main): 039: 0> Digest :: MD5 digest ('hash'). Unpack ('L *') = & gt; [631892218, 19671 99 614, 3683860, 94, 4130231798]  "post-text" itemprop = "text"> 

There is no support for arbitrary length ( * Operator). You must specify a criminal manually.

Fortunately, the Composition module assigns you a certain length, and the Hashlib module tells you how many bytes are expected before By entering L you can specify the number of times to apply the pattern. And there is a special hash in the hashlib library that tells you how many bytes is a specific hash.

To combine these:

  Structspec = '% iL'% (hashlib.hash ('md5'). Digest_size / 4) struct.unpack (structspec , Hashlib.md5 (message) .digest ())   

If you do not want to cut code here 4 , then you struct.calcsize < / Code> to L size: unsigned long:

  structspec = '% iL'% (hashlib.hash ('md5' ). Digest_size / struct.calcsize ('L')) struct.unpack (structspec, hashlib.md5 (message) .digest ()) In fact, on 64-bit platforms such as my Mac,  L  is 8 bytes, so calculate the latter Ehd is important if you have different architectures.   

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -