1

I'm using the technique described here: https://pypi.python.org/pypi/scrypt/

This is the code for my registration function:

@app.route('/register', methods=['POST', 'GET'])
def register():
    result = db.users.find_one({"username" : flask.request.form['username']})
    if result is not None:
        return "That User Is Taken :("
    else:
        pw = scrypt.encrypt(random_token(), str(flask.request.form['password']))
        db.users.insert(
            {"username" : flask.request.form['username'],
             "password" : pw
            })
        return "Registration Successful"

But then I get the following error:

InvalidStringData: strings in documents must be valid UTF-8: "scrypt\x00\x11\x00\x00\x00\x08\x00\x00\x00\x03\xc3g\xfez\xf3y\x06\x11\x17\x14>\xb3\xc6\x87\xd7\xc0[\x12\xae\x04N/\x1b\xc0SpK]i\n\x1c\x99{\xbeQk~n\xce\xd3\x0b\xcc\x82\xc9B\xcec\xdd\x02 \xa0\t\xda\xc9\xce~\xfd\x9a!\xa9_\xc3\n\xcet\xfd\xaf\xd9\xe0iL\x85\xb9C\x07\xc1VD\xfa\x1d\xb7\xa6\x0f\xa5t\xfao\xa2w\xb8\xd5\xdf\x00\x9f&\x8a\x96\x0e\x9c\rGz\x1a\x80\x1c\xd6\x13T\xea\xc6\x16\x8a^\xc5_W+X86\x14\xc6T\xc4\xa1\x81[N\x14H\xc5\xc4\xab\x192z\xfcD'x\x19X\x8f\xe9m?\xab\xb9\xeb~-\xb2-\x06z\xa4,\x1cS\x82\x8a\xe2!\xe0\x01^\xd0\xc9\x9e\xbc\x94}\xbe\x8c\xbc\x9d"

Any idea what may be happening and how to fix it?

Bob Fred
  • 25
  • 4
  • 1
    maybe you could use base64. for example, base64.encodestring(str1.encode("UTF-8")). and decode if you want to use it. – Mark Sep 06 '13 at 02:10
  • `"password" : base64.encodestring(pw.encode("utf-8"))` gives me `UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 21: ordinal not in range(128)` I'm confused because I'm not decoding anything, why is there a decode error? – Bob Fred Sep 06 '13 at 02:19

1 Answers1

1

Your encrypt call is giving you back bytes that need to be encoded as UTF8 before you insert into mongodb. Call unicode(pw, "utf-8") to convert it.

tuckermi
  • 839
  • 6
  • 17
  • Also, note that if you are getting invalid UTF8 characters in there then you need to base64 or uuencode the output as Mark suggested. – tuckermi Sep 06 '13 at 02:20
  • When I call the encrypt function, it returns a string object. I tried `unicode(pw, 'utf-8')` along with `base64.encodestring(unicode(pw, 'utf-8')` and get`UnicodeDecodeError: 'utf8' codec can't decode byte 0x97 in position 16: invalid start byte` and `UnicodeDecodeError: 'utf8' codec can't decode byte 0x97 in position 16: invalid start byte` The input is a valid string, so I don't know why it would be crashing on the conversion. – Bob Fred Sep 06 '13 at 02:35
  • Gotcha. Since you have invalid utf-8 sequences, try encoding with something like latin1 instead. For an example, check out http://stackoverflow.com/questions/17303266/how-to-pickle-unicodes-and-save-them-in-utf-8-databases. In that question the asker also has binary encoded data that they wanted to store in a text field - different storage system, but same basic problem. – tuckermi Sep 06 '13 at 04:12
  • Thank you so much! That link was very helpful. My program works now :) – Bob Fred Sep 06 '13 at 07:49