示例#1
0
    def test_norm_hash_name(self):
        """norm_hash_name()"""
        from itertools import chain
        from passlib.crypto.digest import norm_hash_name, _known_hash_names

        # snapshot warning state, ignore unknown hash warnings
        ctx = warnings.catch_warnings()
        ctx.__enter__()
        self.addCleanup(ctx.__exit__)
        warnings.filterwarnings("ignore", '.*unknown hash')
        warnings.filterwarnings("ignore", '.*unsupported hash')

        # test string types
        self.assertEqual(norm_hash_name(u("MD4")), "md4")
        self.assertEqual(norm_hash_name(b"MD4"), "md4")
        self.assertRaises(TypeError, norm_hash_name, None)

        # test selected results
        for row in chain(_known_hash_names, self.norm_hash_samples):
            for idx, format in enumerate(self.norm_hash_formats):
                correct = row[idx]
                for value in row:
                    result = norm_hash_name(value, format)
                    self.assertEqual(result, correct,
                                     "name=%r, format=%r:" % (value, format))
示例#2
0
    def test_norm_hash_name(self):
        """norm_hash_name()"""
        from itertools import chain
        from passlib.crypto.digest import norm_hash_name, _known_hash_names

        # snapshot warning state, ignore unknown hash warnings
        ctx = warnings.catch_warnings()
        ctx.__enter__()
        self.addCleanup(ctx.__exit__)
        warnings.filterwarnings("ignore", '.*unknown hash')

        # test string types
        self.assertEqual(norm_hash_name(u("MD4")), "md4")
        self.assertEqual(norm_hash_name(b"MD4"), "md4")
        self.assertRaises(TypeError, norm_hash_name, None)

        # test selected results
        for row in chain(_known_hash_names, self.norm_hash_samples):
            for idx, format in enumerate(self.norm_hash_formats):
                correct = row[idx]
                for value in row:
                    result = norm_hash_name(value, format)
                    self.assertEqual(result, correct,
                                     "name=%r, format=%r:" % (value,
                                                              format))
示例#3
0
 def _norm_algs(cls, algs):
     """normalize algs parameter"""
     if isinstance(algs, native_string_types):
         algs = splitcomma(algs)
     algs = sorted(norm_hash_name(alg, 'iana') for alg in algs)
     if any(len(alg) > 9 for alg in algs):
         raise ValueError("SCRAM limits alg names to max of 9 characters")
     if 'sha-1' not in algs:
         # NOTE: required because of SCRAM spec (rfc 5802)
         raise ValueError("sha-1 must be in algorithm list of scram hash")
     return algs
示例#4
0
文件: scram.py 项目: dragoncsc/HDsite
 def _norm_algs(cls, algs):
     """normalize algs parameter"""
     if isinstance(algs, native_string_types):
         algs = splitcomma(algs)
     algs = sorted(norm_hash_name(alg, 'iana') for alg in algs)
     if any(len(alg)>9 for alg in algs):
         raise ValueError("SCRAM limits alg names to max of 9 characters")
     if 'sha-1' not in algs:
         # NOTE: required because of SCRAM spec (rfc 5802)
         raise ValueError("sha-1 must be in algorithm list of scram hash")
     return algs
示例#5
0
 def _norm_checksum(self, checksum, relaxed=False):
     if not isinstance(checksum, dict):
         raise uh.exc.ExpectedTypeError(checksum, "dict", "checksum")
     for alg, digest in iteritems(checksum):
         if alg != norm_hash_name(alg, 'iana'):
             raise ValueError("malformed algorithm name in scram hash: %r" %
                              (alg,))
         if len(alg) > 9:
             raise ValueError("SCRAM limits algorithm names to "
                              "9 characters: %r" % (alg,))
         if not isinstance(digest, bytes):
             raise uh.exc.ExpectedTypeError(digest, "raw bytes", "digests")
         # TODO: verify digest size (if digest is known)
     if 'sha-1' not in checksum:
         # NOTE: required because of SCRAM spec.
         raise ValueError("sha-1 must be in algorithm list of scram hash")
     return checksum
示例#6
0
文件: scram.py 项目: dragoncsc/HDsite
 def _norm_checksum(self, checksum, relaxed=False):
     if not isinstance(checksum, dict):
         raise uh.exc.ExpectedTypeError(checksum, "dict", "checksum")
     for alg, digest in iteritems(checksum):
         if alg != norm_hash_name(alg, 'iana'):
             raise ValueError("malformed algorithm name in scram hash: %r" %
                              (alg,))
         if len(alg) > 9:
             raise ValueError("SCRAM limits algorithm names to "
                              "9 characters: %r" % (alg,))
         if not isinstance(digest, bytes):
             raise uh.exc.ExpectedTypeError(digest, "raw bytes", "digests")
         # TODO: verify digest size (if digest is known)
     if 'sha-1' not in checksum:
         # NOTE: required because of SCRAM spec.
         raise ValueError("sha-1 must be in algorithm list of scram hash")
     return checksum
示例#7
0
    def extract_digest_algs(cls, hash, format="iana"):
        """Return names of all algorithms stored in a given hash.

        :type hash: str
        :arg hash:
            The :class:`!scram` hash to parse

        :type format: str
        :param format:
            This changes the naming convention used by the
            returned algorithm names. By default the names
            are IANA-compatible; possible values are ``"iana"`` or ``"hashlib"``.

        :returns:
            Returns a list of digest algorithms; e.g. ``["sha-1"]``
        """
        # XXX: this could be sped up by writing custom parsing routine
        # that just picks out relevant names, and doesn't bother
        # with full structure validation each time it's called.
        algs = cls.from_string(hash).algs
        if format == "iana":
            return algs
        else:
            return [norm_hash_name(alg, format) for alg in algs]
示例#8
0
    def extract_digest_info(cls, hash, alg):
        """return (salt, rounds, digest) for specific hash algorithm.

        :type hash: str
        :arg hash:
            :class:`!scram` hash stored for desired user

        :type alg: str
        :arg alg:
            Name of digest algorithm (e.g. ``"sha-1"``) requested by client.

            This value is run through :func:`~passlib.crypto.digest.norm_hash_name`,
            so it is case-insensitive, and can be the raw SCRAM
            mechanism name (e.g. ``"SCRAM-SHA-1"``), the IANA name,
            or the hashlib name.

        :raises KeyError:
            If the hash does not contain an entry for the requested digest
            algorithm.

        :returns:
            A tuple containing ``(salt, rounds, digest)``,
            where *digest* matches the raw bytes returned by
            SCRAM's :func:`Hi` function for the stored password,
            the provided *salt*, and the iteration count (*rounds*).
            *salt* and *digest* are both raw (unencoded) bytes.
        """
        # XXX: this could be sped up by writing custom parsing routine
        # that just picks out relevant digest, and doesn't bother
        # with full structure validation each time it's called.
        alg = norm_hash_name(alg, 'iana')
        self = cls.from_string(hash)
        chkmap = self.checksum
        if not chkmap:
            raise ValueError("scram hash contains no digests")
        return self.salt, self.rounds, chkmap[alg]
示例#9
0
文件: scram.py 项目: dragoncsc/HDsite
    def extract_digest_algs(cls, hash, format="iana"):
        """Return names of all algorithms stored in a given hash.

        :type hash: str
        :arg hash:
            The :class:`!scram` hash to parse

        :type format: str
        :param format:
            This changes the naming convention used by the
            returned algorithm names. By default the names
            are IANA-compatible; possible values are ``"iana"`` or ``"hashlib"``.

        :returns:
            Returns a list of digest algorithms; e.g. ``["sha-1"]``
        """
        # XXX: this could be sped up by writing custom parsing routine
        # that just picks out relevant names, and doesn't bother
        # with full structure validation each time it's called.
        algs = cls.from_string(hash).algs
        if format == "iana":
            return algs
        else:
            return [norm_hash_name(alg, format) for alg in algs]
示例#10
0
文件: scram.py 项目: dragoncsc/HDsite
    def extract_digest_info(cls, hash, alg):
        """return (salt, rounds, digest) for specific hash algorithm.

        :type hash: str
        :arg hash:
            :class:`!scram` hash stored for desired user

        :type alg: str
        :arg alg:
            Name of digest algorithm (e.g. ``"sha-1"``) requested by client.

            This value is run through :func:`~passlib.crypto.digest.norm_hash_name`,
            so it is case-insensitive, and can be the raw SCRAM
            mechanism name (e.g. ``"SCRAM-SHA-1"``), the IANA name,
            or the hashlib name.

        :raises KeyError:
            If the hash does not contain an entry for the requested digest
            algorithm.

        :returns:
            A tuple containing ``(salt, rounds, digest)``,
            where *digest* matches the raw bytes returned by
            SCRAM's :func:`Hi` function for the stored password,
            the provided *salt*, and the iteration count (*rounds*).
            *salt* and *digest* are both raw (unencoded) bytes.
        """
        # XXX: this could be sped up by writing custom parsing routine
        # that just picks out relevant digest, and doesn't bother
        # with full structure validation each time it's called.
        alg = norm_hash_name(alg, 'iana')
        self = cls.from_string(hash)
        chkmap = self.checksum
        if not chkmap:
            raise ValueError("scram hash contains no digests")
        return self.salt, self.rounds, chkmap[alg]