gooderp18绿色标准版
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.

125 line
3.3KB

  1. ##########################################################################
  2. #
  3. # pgAdmin 4 - PostgreSQL Tools
  4. #
  5. # Copyright (C) 2013 - 2020, The pgAdmin Development Team
  6. # This software is released under the PostgreSQL Licence
  7. #
  8. #########################################################################
  9. """This File Provides Cryptography."""
  10. from __future__ import division
  11. import base64
  12. import hashlib
  13. import os
  14. import six
  15. from cryptography.hazmat.backends import default_backend
  16. from cryptography.hazmat.primitives.ciphers import Cipher
  17. from cryptography.hazmat.primitives.ciphers.algorithms import AES
  18. from cryptography.hazmat.primitives.ciphers.modes import CFB8
  19. padding_string = b'}'
  20. iv_size = AES.block_size // 8
  21. def encrypt(plaintext, key):
  22. """
  23. Encrypt the plaintext with AES method.
  24. Parameters:
  25. plaintext -- String to be encrypted.
  26. key -- Key for encryption.
  27. """
  28. iv = os.urandom(iv_size)
  29. cipher = Cipher(AES(pad(key)), CFB8(iv), default_backend())
  30. encryptor = cipher.encryptor()
  31. # If user has entered non ascii password (Python2)
  32. # we have to encode it first
  33. if isinstance(plaintext, six.text_type):
  34. plaintext = plaintext.encode()
  35. return base64.b64encode(iv + encryptor.update(plaintext) +
  36. encryptor.finalize())
  37. def decrypt(ciphertext, key):
  38. """
  39. Decrypt the AES encrypted string.
  40. Parameters:
  41. ciphertext -- Encrypted string with AES method.
  42. key -- key to decrypt the encrypted string.
  43. """
  44. ciphertext = base64.b64decode(ciphertext)
  45. iv = ciphertext[:iv_size]
  46. cipher = Cipher(AES(pad(key)), CFB8(iv), default_backend())
  47. decryptor = cipher.decryptor()
  48. return decryptor.update(ciphertext[iv_size:]) + decryptor.finalize()
  49. def pad(key):
  50. """Add padding to the key."""
  51. if isinstance(key, six.text_type):
  52. key = key.encode()
  53. # Key must be maximum 32 bytes long, so take first 32 bytes
  54. key = key[:32]
  55. # If key size is 16, 24 or 32 bytes then padding is not required
  56. if len(key) in (16, 24, 32):
  57. return key
  58. # Add padding to make key 32 bytes long
  59. return key.ljust(32, padding_string)
  60. def pqencryptpassword(password, user):
  61. """
  62. pqencryptpassword -- to encrypt a password
  63. This is intended to be used by client applications that wish to send
  64. commands like ALTER USER joe PASSWORD 'pwd'. The password need not
  65. be sent in cleartext if it is encrypted on the client side. This is
  66. good because it ensures the cleartext password won't end up in logs,
  67. pg_stat displays, etc. We export the function so that clients won't
  68. be dependent on low-level details like whether the enceyption is MD5
  69. or something else.
  70. Arguments are the cleartext password, and the SQL name of the user it
  71. is for.
  72. Return value is "md5" followed by a 32-hex-digit MD5 checksum..
  73. Args:
  74. password:
  75. user:
  76. Returns:
  77. """
  78. m = hashlib.md5()
  79. # Place salt at the end because it may be known by users trying to crack
  80. # the MD5 output.
  81. # Handling of non-ascii password (Python2)
  82. if hasattr(str, 'decode'):
  83. password = password.encode('utf-8')
  84. user = user.encode('utf-8')
  85. else:
  86. password = password.encode()
  87. user = user.encode()
  88. m.update(password)
  89. m.update(user)
  90. return "md5" + m.hexdigest()
上海开阖软件有限公司 沪ICP备12045867号-1