Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top