<NME> base.py
<BEF> """
Base password hashers.

Contains weak hashers (the original ones) available with Modoboa.
"""

import base64
import crypt
import hashlib
import string
from random import Random

from django.utils.crypto import constant_time_compare
from django.utils.encoding import force_bytes, force_text


class MetaHasher(type):
    """
    PasswordHasher Metaclass
    Allow classmethod to be properties
    """
    @property
    def name(cls):
        """Returns the name of the hasher"""
        return cls.__name__.rstrip('Hasher').lower()

    @property
    def label(cls):
        """Returns the label of the hasher"""
        return cls.name if not cls._weak else "{} (weak)".format(cls.name)


class PasswordHasher(metaclass=MetaHasher):
    """
    Base class of all hashers.
    """
    _weak = False

    def __init__(self, target="local"):
        self._target = target

    def _encrypt(self, clearvalue, salt=None):
        raise NotImplementedError

    def _b64encode(self, pwhash):
        """Encode :keyword:`pwhash` using base64 if needed.

        :param str pwhash: password hash
        :return: base64 encoded hash or original hash
        """
        if self._target == "ldap":
            return base64.b64encode(pwhash)
        return pwhash

    def encrypt(self, clearvalue):
        """Encrypt a password.

        The format used to store passwords is the same than dovecot's one::

          {SCHEME}<hash>

        <hash> may differ according to the used scheme.

        :param str clearvalue: clear password
        :rtype: str
        :return: encrypted password
        """
            hashed_value
        )


class PLAINHasher(PasswordHasher):

    """
    Plain (ie. clear) password hasher.
    """
    @property
    def scheme(self):
        return "{PLAIN}"
        :param str hashed_value: hased password
        :rtype bool
        :return: True if the password needs rehash, false otherwise
        """
        return False

    @classmethod
    def get_password_hashers(cls):
        """Return all the PasswordHasher supported by Modoboa"""

    Uses python `crypt` standard module.
    """
    @property
    def scheme(self):
        return "{CRYPT}"
    @property
    def scheme(self):
        return "{PLAIN}"

    def _encrypt(self, clearvalue, salt=None):
        return clearvalue


class CRYPTHasher(PasswordHasher):
    """
    crypt password hasher.

    Uses python `crypt` standard module.
    """
    _weak = True

    @property

    Uses python `hashlib` standard module.
    """
    @property
    def scheme(self):
        return "{MD5}"
                    2
                )
            )
        return crypt.crypt(clearvalue, salt)


class MD5Hasher(PasswordHasher):
    """
    MD5 password hasher.


    Uses python `hashlib` and `base64` standard modules.
    """
    @property
    def scheme(self):
        return "{SHA256}"
        obj = hashlib.md5(force_bytes(clearvalue))
        return obj.hexdigest()


class SHA256Hasher(PasswordHasher):
    """
    SHA256 password hasher.

    Uses python `hashlib` and `base64` standard modules.
    """
    _weak = True

    @property
    def scheme(self):
        return "{SHA256}"

    def _encrypt(self, clearvalue, salt=None):
        return hashlib.sha256(force_bytes(clearvalue)).digest()

    def _b64encode(self, pwhash):
        """Encode :keyword:`pwhash` using base64.

        :param str pwhash: password hash
        :return: base64 encoded hash
        """
        return base64.b64encode(pwhash)

<MSG> check for dovecot supported password schemes

call doveadm pw -l to list password schemes
only show supported password schemes

<DFF> @@ -68,12 +68,19 @@ class PasswordHasher(object):
             hashed_value
         )
 
+    @classmethod
+    def get_password_hashers(cls):
+        return cls.__subclasses__()
+
 
 class PLAINHasher(PasswordHasher):
 
     """
     Plain (ie. clear) password hasher.
     """
+    name = "plain"
+    label = "plain (weak)"
+
     @property
     def scheme(self):
         return "{PLAIN}"
@@ -89,6 +96,9 @@ class CRYPTHasher(PasswordHasher):
 
     Uses python `crypt` standard module.
     """
+    name = "crypt"
+    label = "crypt (weak)"
+
     @property
     def scheme(self):
         return "{CRYPT}"
@@ -112,6 +122,9 @@ class MD5Hasher(PasswordHasher):
 
     Uses python `hashlib` standard module.
     """
+    name = "md5"
+    label = "md5 (weak)"
+
     @property
     def scheme(self):
         return "{MD5}"
@@ -128,6 +141,9 @@ class SHA256Hasher(PasswordHasher):
 
     Uses python `hashlib` and `base64` standard modules.
     """
+    name = "sha256"
+    label = "sha256 (weak)"
+
     @property
     def scheme(self):
         return "{SHA256}"

check for dovecot supported password schemes