Changeset - 74251f8004d2
[Not reviewed]
beta
0 1 0
Marcin Kuzminski - 15 years ago 2011-04-02 00:44:13
marcin@python-works.com
merged freebsd support issue from default
1 file changed with 9 insertions and 12 deletions:
0 comments (0 inline, 0 general)
rhodecode/lib/auth.py
Show inline comments
 
@@ -27,29 +27,29 @@
 
import random
 
import logging
 
import traceback
 
import hashlib
 

	
 
from tempfile import _RandomNameSequence
 
from decorator import decorator
 

	
 
from pylons import config, session, url, request
 
from pylons.controllers.util import abort, redirect
 
from pylons.i18n.translation import _
 

	
 
from rhodecode import __platform__
 
from rhodecode import __platform__, PLATFORM_WIN, PLATFORM_OTHERS
 

	
 
if __platform__ == 'Windows':
 
if __platform__ in PLATFORM_WIN:
 
    from hashlib import sha256
 
if __platform__ in ('Linux', 'Darwin'):
 
if __platform__ in PLATFORM_OTHERS:
 
    import bcrypt
 

	
 
from rhodecode.lib import str2bool
 
from rhodecode.lib.exceptions import LdapPasswordError, LdapUsernameError
 
from rhodecode.lib.utils import get_repo_slug
 
from rhodecode.lib.auth_ldap import AuthLdap
 

	
 
from rhodecode.model import meta
 
from rhodecode.model.user import UserModel
 
from rhodecode.model.db import Permission
 

	
 

	
 
@@ -81,50 +81,47 @@ class PasswordGenerator(object):
 
        return self.passwd
 

	
 
class RhodeCodeCrypto(object):
 

	
 
    @classmethod
 
    def hash_string(cls, str_):
 
        """
 
        Cryptographic function used for password hashing based on pybcrypt
 
        or pycrypto in windows
 
        
 
        :param password: password to hash
 
        """
 
        if __platform__ == 'Windows':
 
        if __platform__ in PLATFORM_WIN:
 
            return sha256(str_).hexdigest()
 
        elif __platform__ in ('Linux', 'Darwin'):
 
        elif __platform__ in PLATFORM_OTHERS:
 
            return bcrypt.hashpw(str_, bcrypt.gensalt(10))
 
        else:
 
            raise Exception('Unknown or unsupoprted platform %s' % __platform__)
 
            raise Exception('Unknown or unsupported platform %s' % __platform__)
 

	
 
    @classmethod
 
    def hash_check(cls, password, hashed):
 
        """
 
        Checks matching password with it's hashed value, runs different
 
        implementation based on platform it runs on
 
        
 
        :param password: password
 
        :param hashed: password in hashed form
 
        """
 

	
 
        if __platform__ == 'Windows':
 
        if __platform__ in PLATFORM_WIN:
 
            return sha256(password).hexdigest() == hashed
 
        elif __platform__ in ('Linux', 'Darwin'):
 
        elif __platform__ in PLATFORM_OTHERS:
 
            return bcrypt.hashpw(password, hashed) == hashed
 
        else:
 
            raise Exception('Unknown or unsupoprted platform %s' % __platform__)
 

	
 

	
 

	
 
            raise Exception('Unknown or unsupported platform %s' % __platform__)
 

	
 

	
 
def get_crypt_password(password):
 
    return RhodeCodeCrypto.hash_string(password)
 

	
 
def check_password(password, hashed):
 
    return RhodeCodeCrypto.hash_check(password, hashed)
 

	
 
def generate_api_key(username, salt=None):
 
    if salt is None:
 
        salt = _RandomNameSequence().next()
 

	
0 comments (0 inline, 0 general)