PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/cryptacular/bcrypt/__init__.py

https://bitbucket.org/dholth/cryptacular/
Python | 74 lines | 30 code | 16 blank | 28 comment | 6 complexity | 98c07f4a0f0fa450c043756453cdcfef MD5 | raw file
  1. # Copyright (c) 2009 Daniel Holth <dholth@fastmail.fm>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the 'Software'), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. # THE SOFTWARE.
  20. __all__ = ['BCRYPTPasswordManager']
  21. import os
  22. import re
  23. from cryptacular.bcrypt._bcrypt import crypt_rn, crypt_gensalt_rn
  24. from cryptacular.core import _cmp, check_unicode
  25. class BCRYPTPasswordManager(object):
  26. # for testing
  27. crypt_rn = crypt_rn
  28. crypt_gensalt_rn = crypt_gensalt_rn
  29. SCHEME = 'BCRYPT'
  30. PREFIX = '$2a$'
  31. _rounds = 10
  32. _bcrypt_syntax = re.compile('\$2a\$[0-9]{2}\$[./A-Za-z0-9]{53}')
  33. def encode(self, text, rounds=None):
  34. '''Hash a password using bcrypt.
  35. Note: only the first 72 characters of password are significant.
  36. '''
  37. rounds = rounds or self._rounds
  38. settings = self.crypt_gensalt_rn(self.PREFIX, rounds, os.urandom(16))
  39. if settings is None:
  40. raise ValueError('_bcrypt.crypt_gensalt_rn returned None')
  41. encoded = self.crypt_rn(check_unicode(text), settings)
  42. if encoded is None:
  43. raise ValueError('_bcrypt.crypt_rn returned None')
  44. return encoded
  45. def check(self, encoded, text):
  46. '''Check a bcrypt password hash against a password.
  47. '''
  48. if not self.match(encoded):
  49. return False
  50. encoded_text = self.crypt_rn(check_unicode(text), encoded)
  51. if encoded_text is None:
  52. raise ValueError('_bcrypt.crypt_rn returned None')
  53. return _cmp(encoded_text, check_unicode(encoded))
  54. def match(self, hash):
  55. '''Return True if hash looks like a BCRYPT password hash.
  56. '''
  57. return self._bcrypt_syntax.match(hash) is not None