Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hashed.py 3.5KB

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