| 506 | return bcrypt.gensalt(self.rounds) |
| 507 | |
| 508 | def encode(self, password, salt): |
| 509 | bcrypt = self._load_library() |
| 510 | password = force_bytes(password) |
| 511 | salt = force_bytes(salt) |
| 512 | # Hash the password prior to using bcrypt to prevent password |
| 513 | # truncation as described in #20138. |
| 514 | if self.digest is not None: |
| 515 | # Use binascii.hexlify() because a hex encoded bytestring is str. |
| 516 | password = binascii.hexlify(self.digest(password).digest()) |
| 517 | |
| 518 | data = bcrypt.hashpw(password, salt) |
| 519 | return "%s$%s" % (self.algorithm, data.decode("ascii")) |
| 520 | |
| 521 | def decode(self, encoded): |
| 522 | algorithm, empty, algostr, work_factor, data = encoded.split("$", 4) |