ó
]`¾Tc           @@ së   d  Z  d d l m Z d d l m Z d Z d j e ƒ Z d Z d Z d	 Z	 d
 d d g Z
 d d l m Z y d d l Z Wn# e k
 r¡ Z e d ƒ e ‚ n Xd d l m Z e d Z d d „ Z d „  Z d
 e f d „  ƒ  YZ d S(   sÙ   
    flaskext.bcrypt
    ---------------
    
    A Flask extension providing bcrypt hasing and comparison facilities.
    
    :copyright: (c) 2011 by Max Countryman.
    :license: BSD, see LICENSE for more details.
i    (   t   absolute_import(   t   print_functiont   0t   6t   .s   Max Countrymant   BSDs   (c) 2011 by Max Countrymant   Bcryptt   check_password_hasht   generate_password_hash(   t   safe_str_cmpNs)   py-bcrypt is required to use Flask-Bcrypt(   t   version_infoc         C@ s   t  ƒ  j |  | ƒ S(   s¨  This helper function wraps the eponymous method of :class:`Bcrypt`. It 
    is intended to be used as a helper function at the expense of the 
    configuration variable provided when passing back the app object. In other 
    words this shortcut does not make use of the app object at all.
    
    To this this function, simple import it from the module and use it in a 
    similar fashion as the method would be used. Here is a quick example::
        
        from flask.ext.bcrypt import generate_password_hash
        pw_hash = generate_password_hash('hunter2', 10)
    
    :param password: The password to be hashed.
    :param rounds: The optional number of rounds.
    (   R   R   (   t   passwordt   rounds(    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_bcrypt.pyR       s    c         C@ s   t  ƒ  j |  | ƒ S(   s­  This helper function wraps the eponymous method of :class:`Bcrypt.` It 
    is intended to be used as a helper function at the expense of the 
    configuration variable provided when passing back the app object. In other 
    words this shortcut does not make use of the app object at all.
    
    To this this function, simple import it from the module and use it in a 
    similar fashion as the method would be used. Here is a quick example::
        
        from flask.ext.bcrypt import check_password_hash
        check_password_hash(pw_hash, 'hunter2') # returns True
    
    :param pw_hash: The hash to be compared against.
    :param password: The password to compare.
    (   R   R   (   t   pw_hashR   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_bcrypt.pyR   2   s    c           B@ s>   e  Z d  Z d Z d d „ Z d „  Z d d „ Z d „  Z RS(   sD	  Bcrypt class container for password hashing and checking logic using 
    bcrypt, of course. This class may be used to intialize your Flask app 
    object. The purpose is to provide a simple interface for overriding 
    Werkzeug's built-in password hashing utilities.
    
    Although such methods are not actually overriden, the API is intentionally 
    made similar so that existing applications which make use of the previous 
    hashing functions might be easily adapted to the stronger facility of 
    bcrypt.
    
    To get started you will wrap your application's app object something like 
    this::
        
        app = Flask(__name__)
        bcrypt = Bcrypt(app)
    
    Now the two primary utility methods are exposed via this object, `bcrypt`.
    So in the context of the application, important data, such as passwords, 
    could be hashed using this syntax::
        
        password = 'hunter2'
        pw_hash = bcrypt.generate_password_hash(password)
    
    Once hashed, the value is irreversible. However in the case of validating 
    logins a simple hashing of candidate password and subsequent comparison. 
    Importantly a comparison should be done in constant time. This helps 
    prevent timing attacks. A simple utility method is provided for this::
        
        candidate = 'secret'
        bcrypt.check_password_hash(pw_hash, candidate)
    
    If both the candidate and the existing password hash are a match 
    `check_password_hash` returns True. Otherwise, it returns False.
        
    .. admonition:: Namespacing Issues 
        
        It's worth noting that if you use the format, `bcrypt = Bcrypt(app)` 
        you are effectively overriding the bcrypt module. Though it's unlikely 
        you would need to access the module outside of the scope of the 
        extension be aware that it's overriden.
        
        Alternatively consider using a different name, such as `flask_bcrypt 
        = Bcrypt(app)` to prevent naming collisions.
    
    Additionally a configuration value for `BCRYPT_LOG_ROUNDS` may be set in 
    the configuration of the Flask app. If none is provided this will 
    internally be assigned to 12. (This value is used in determining the 
    complexity of the encryption, see bcrypt for more details.)
    
    :param app: The Flask application object. Defaults to None.
    i   c         C@ s    | d  k	 r |  j | ƒ n  d  S(   N(   t   Nonet   init_app(   t   selft   app(    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_bcrypt.pyt   __init__{   s    c         C@ s   | j  j d d ƒ |  _ d S(   sr   Initalizes the application with the extension.
        
        :param app: The Flask application object.
        t   BCRYPT_LOG_ROUNDSi   N(   t   configt   gett   _log_rounds(   R   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_bcrypt.pyR      s    c         C@ s¬   | s t  d ƒ ‚ n  | d k r- |  j } n  t d k  rZ t | t ƒ rZ | j d ƒ } n- t d k r‡ t | t ƒ r‡ | j d ƒ } n  t	 | ƒ } t
 j | t
 j | ƒ ƒ S(   sþ  Generates a password hash using bcrypt. Specifying `rounds` 
        sets the log_rounds parameter of `bcrypt.gensalt()` which determines 
        the complexity of the salt. 12 is the default value.
        
        Example usage of :class:`generate_password_hash` might look something 
        like this::
            
            pw_hash = bcrypt.generate_password_hash('secret', 10)
        
        :param password: The password to be hashed.
        :param rounds: The optional number of rounds.
        s   Password must be non-empty.i   t   u8s   utf-8N(   t
   ValueErrorR   R   t   PYVERt
   isinstancet   unicodet   encodet   bytest   decodet   strt   bcryptt   hashpwt   gensalt(   R   R   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_bcrypt.pyR   †   s    c         C@ s   t  d k  r- t | t ƒ r- | j d ƒ } n- t  d k rZ t | t ƒ rZ | j d ƒ } n  t | ƒ } t t j	 | | ƒ | ƒ S(   s[  Tests a password hash against a candidate password. The candidate 
        password is first hashed and then subsequently compared in constant 
        time to the existing hash. This will either return `True` or `False`.
        
        Example usage of :class:`check_password_hash` would look something 
        like this::
            
            pw_hash = bcrypt.generate_password_hash('secret', 10)
            bcrypt.check_password_hash(pw_hash, 'secret') # returns True
        
        :param pw_hash: The hash to be compared against.
        :param password: The password to compare.
        i   R   s   utf-8(
   R   R   R   R   R   R   R   R	   R    R!   (   R   R   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_bcrypt.pyR   ¡   s    N(	   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   (    (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_bcrypt.pyR   D   s   3	(   R   R   R   (   R%   t
   __future__R    R   t   __version_info__t   joint   __version__t
   __author__t   __license__t   __copyright__t   __all__t   werkzeug.securityR	   R    t   ImportErrort   et   printt   sysR
   R   R   R   R   t   objectR   (    (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_bcrypt.pyt   <module>	   s&   


	