Jump to content

So I am trying to make a script that will login my mega account upload some files.  I am using this as a guide https://github.com/richardasaurus/mega.py/blob/master/mega/mega.py

 

but one line keeps breaking for me.

password_aes = prepare_key(str_to_a32(password))
uh = stringhash(email, password_aes) #this line throws error in title
def stringhash(str, aeskey):
    s32 = str_to_a32(str)
    h32 = [0, 0, 0, 0]
    for i in range(len(s32)):
        h32[i % 4] ^= s32[i]
    for r in range(0x4000):
        h32 = aes_cbc_encrypt_a32(h32, aeskey)
    return a32_to_base64((h32[0], h32[2]))


def str_to_a32(b):
    if len(b) % 4:
        # pad to multiple of 4
        b += '\0' * (4 - len(b) % 4)
    return struct.unpack('>%dI' % (len(b) / 4), b)
(1677932914, 1584315667, 2724134994, 312442033) #"password" encoded
Traceback (most recent call last):
  File "./check.py", line 29, in <module>
    uh = stringhash(email, password_aes)
  File "/mnt/Documents/python/MEGAmail2/crypto.py", line 28, in stringhash
    s32 = str_to_a32(str)
  File "/mnt/Documents/python/MEGAmail2/crypto.py", line 80, in str_to_a32
    b += '\0' * (4 - len(b) % 4)
TypeError: can't concat bytes to str

why above my head I have no idea how to fix this :L 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/810059-cant-concat-bytes-to-str/
Share on other sites

Link to post
Share on other sites

The email you're passing is a bytes object, not a string. 

In the line 

b += '\0' * (4 - len(b) % 4)

you're trying to concatenate a string onto that bytes object, which you can't do, hence the error.

 

The fix is to change the string to a bytes by using the 'b' prefix.

b += b'\0' * (4 - len(b) % 4)

 

1474412270.2748842

Link to post
Share on other sites

3 minutes ago, fizzlesticks said:

The email you're passing is a bytes object, not a string. 

In the line 


b += '\0' * (4 - len(b) % 4)

you're trying to concatenate a string onto that bytes object, which you can't do, hence the error.

 

The fix is to change the string to a bytes by using the 'b' prefix.


b += b'\0' * (4 - len(b) % 4)

 

if I past the email as a string I get 

 

TypeError: a bytes-like object is required, not 'str'

Which is why I encoded as a byte then got the error above. This is getting way above my head so gonna go back and lok again. 

 

thanks

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

1 minute ago, vorticalbox said:

if I past the email as a string I get 

 


TypeError: a bytes-like object is required, not 'str'

Which is why I encoded as a byte then got the error above. This is getting way above my head so gonna go back and lok again. 

 

thanks

It looks like the code you're using as an example is written for Python 2 and you're using Python 3. In Py2 struct.unpack worked on strings and in Py3 it uses bytes. Unfortunately it's one of those annoyances for converting between versions.

1474412270.2748842

Link to post
Share on other sites

22 minutes ago, fizzlesticks said:

It looks like the code you're using as an example is written for Python 2 and you're using Python 3. In Py2 struct.unpack worked on strings and in Py3 it uses bytes. Unfortunately it's one of those annoyances for converting between versions.

maybe thats my problem i#ll give it a go in python 2 ^_^

 

Edit: used python 3 and it worked -.- feel silly now xD thanks

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×