python passlib verify doesn't match
<p>I am working with Flask and MongoDB, trying to implement username / password verification with passlib.</p>

<p>The class from models.py : </p>

<pre><code>from passlib.hash import sha256_crypt

class Users(object):
    def __init__(self, username='', password='', collection=db.users, articles=''):
    self.collection = collection
    self.username = username
    self.password = password
    self.articles = []

def addUser(self):
    self.collection.insert({'username': self.username, 'password': sha256_crypt.encrypt('self.password'), 'articles': self.articles})
</code></pre>

<p>From the python shell, I create the user alice with the password 'secret' :</p>

<pre><code>&gt;&gt;&gt; import models
&gt;&gt;&gt; alice = models.Users()
&gt;&gt;&gt; alice.username = 'alice'
&gt;&gt;&gt; alice.password = 'secret'
&gt;&gt;&gt; alice.addUser()
</code></pre>

<p>From the mongo shell, I check that the document has been well created with a hash instead of the clear password :</p>

<pre><code>&gt; db.users.find()
{ "_id" : ObjectId("57f15d9f815a0b6c1533427f"), "username" : "alice", "articles" : [ ], "password" : "$5$rounds=535000$Oq1hy1INzO59nu0q$FzMz1DtBWDwM.sw0AhrlVA8esgE30n8rr/NjOltB8.7" }
</code></pre>

<p>From now on, we should be able to verify the password from the python shell using the hash stored in the document, isn't it ?</p>

<pre><code>&gt;&gt;&gt; sha256_crypt.verify('secret','$5$rounds=535000$Oq1hy1INzO59nu0q$FzMz1DtBWDwM.sw0AhrlVA8esgE30n8rr/NjOltB8.7')
False
</code></pre>

<p>But it doesn't, someone can explain to me why ?</p>

<p>That is happening because you're encrypting not <code>self.password</code> but <code>'self.password'</code></p>

<p>So you need to change your <code>addUser</code> method to the following:</p>

<pre><code>def addUser(self):
    self.collection.insert({'username': self.username, 'password': sha256_crypt.encrypt(self.password), 'articles': self.articles})
</code></pre>

python mongodb passlib