Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
hashed.py95 linesDownload Raw Back to utils
1"""
2"""
3
4# Created on 2015.07.16
5#
6# Author: Giovanni Cannata
7#
8# Copyright 2015 - 2020 Giovanni Cannata
9#
10# This file is part of ldap3.
11#
12# ldap3 is free software: you can redistribute it and/or modify
13# it under the terms of the GNU Lesser General Public License as published
14# by the Free Software Foundation, either version 3 of the License, or
15# (at your option) any later version.
16#
17# ldap3 is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU Lesser General Public License for more details.
21#
22# You should have received a copy of the GNU Lesser General Public License
23# along with ldap3 in the COPYING and COPYING.LESSER files.
24# If not, see <http://www.gnu.org/licenses/>.
25
26from .. import HASHED_NONE, HASHED_MD5, HASHED_SALTED_MD5, HASHED_SALTED_SHA, HASHED_SALTED_SHA256, \
27    HASHED_SALTED_SHA384, HASHED_SALTED_SHA512, HASHED_SHA, HASHED_SHA256, HASHED_SHA384, HASHED_SHA512
28
29import hashlib
30from os import urandom
31from base64 import b64encode
32
33from ..core.exceptions import LDAPInvalidHashAlgorithmError
34
35# each tuple: (the string to include between braces in the digest, the name of the algorithm to invoke with the new() function)
36
37algorithms_table = {
38    HASHED_MD5: ('md5', 'MD5'),
39    HASHED_SHA: ('sha', 'SHA1'),
40    HASHED_SHA256: ('sha256', 'SHA256'),
41    HASHED_SHA384: ('sha384', 'SHA384'),
42    HASHED_SHA512: ('sha512', 'SHA512')
43}
44
45
46salted_table = {
47    HASHED_SALTED_MD5: ('smd5', HASHED_MD5),
48    HASHED_SALTED_SHA: ('ssha', HASHED_SHA),
49    HASHED_SALTED_SHA256: ('ssha256', HASHED_SHA256),
50    HASHED_SALTED_SHA384: ('ssha384', HASHED_SHA384),
51    HASHED_SALTED_SHA512: ('ssha512', HASHED_SHA512)
52}
53
54
55def hashed(algorithm, value, salt=None, raw=False, encoding='utf-8'):
56    if str is not bytes and not isinstance(value, bytes):  # Python 3
57        value = value.encode(encoding)
58
59    if algorithm is None or algorithm == HASHED_NONE:
60        return value
61
62    # algorithm name can be already coded in the ldap3 constants or can be any value passed in the 'algorithm' parameter
63
64    if algorithm in algorithms_table:
65        try:
66            digest = hashlib.new(algorithms_table[algorithm][1], value).digest()
67        except ValueError:
68            raise LDAPInvalidHashAlgorithmError('Hash algorithm ' + str(algorithm) + ' not available')
69
70        if raw:
71            return digest
72        return ('{%s}' % algorithms_table[algorithm][0]) + b64encode(digest).decode('ascii')
73    elif algorithm in salted_table:
74        if not salt:
75            salt = urandom(8)
76        digest = hashed(salted_table[algorithm][1], value + salt, raw=True) + salt
77        if raw:
78            return digest
79        return ('{%s}' % salted_table[algorithm][0]) + b64encode(digest).decode('ascii')
80    else:
81        # if an unknown (to the library) algorithm is requested passes the name as the string in braces and as the algorithm name
82        # if salt is present uses it to salt the digest
83        try:
84            if not salt:
85                digest = hashlib.new(algorithm, value).digest()
86            else:
87                digest = hashlib.new(algorithm, value + salt).digest() + salt
88        except ValueError:
89            raise LDAPInvalidHashAlgorithmError('Hash algorithm ' + str(algorithm) + ' not available')
90
91        if raw:
92            return digest
93        return ('{%s}' % algorithm) + b64encode(digest).decode('ascii')
94
95 
codekingpro/portable-devtools · Team Ai