-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added src/hashpw/algs/PBKDF2.py (not yet used)
- Loading branch information
Showing
24 changed files
with
719 additions
and
603 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[bumpversion] | ||
current_version = 2.3.0 | ||
current_version = 2.4.0 | ||
commit = False | ||
tag = False | ||
allow_dirty = True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import passlib.hash | ||
|
||
from ..structure import PLSaltedAlgorithm | ||
|
||
|
||
class ApacheMD5(PLSaltedAlgorithm): | ||
name = "apache-md5" | ||
option = "a" | ||
prefix = "$apr1$" | ||
suffix = "" | ||
min_length = 37 | ||
salt_length = 8 | ||
|
||
|
||
def __init__(self, salt): | ||
super().__init__(salt) | ||
|
||
self.hasher = passlib.hash.apr_md5_crypt.using(salt=self.salt[6:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import hashlib | ||
|
||
from .. import utils | ||
from ..structure import Algorithm | ||
|
||
|
||
class ApacheSHA1(Algorithm): | ||
name = "apache-sha-1" | ||
option = "A" | ||
prefix = "{SHA}" | ||
suffix = "" | ||
min_length = 33 | ||
|
||
|
||
def hash(self, plaintext): | ||
input_byte_str = plaintext.encode("UTF-8") | ||
round_output = hashlib.sha1(input_byte_str).digest() | ||
return self.prefix + utils.base64encode(round_output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import binascii | ||
import hashlib | ||
|
||
from ..structure import Algorithm | ||
|
||
|
||
class BasicMD5(Algorithm): | ||
name = "basic-md5" | ||
option = "M" | ||
prefix = "" | ||
extra_prefix = "{PLAIN-MD5}" | ||
suffix = "" | ||
min_length = 32 | ||
|
||
|
||
def hash(self, plaintext): | ||
input_byte_str = plaintext.encode("UTF-8") | ||
first_round_output = hashlib.md5(input_byte_str).digest() | ||
output_byte_str = binascii.hexlify(first_round_output) | ||
return output_byte_str.decode('ascii') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from ..structure import SaltedAlgorithm | ||
|
||
|
||
class Blowfish(SaltedAlgorithm): | ||
"""See https://pypi.org/project/py-bcrypt/""" | ||
name = "blowfish" | ||
option = "b" | ||
prefix = "$2a$" | ||
extra_prefix = "{BLF-CRYPT}" | ||
suffix = "" | ||
min_length = 57 | ||
salt_length = 16 | ||
|
||
|
||
@classmethod | ||
def final_prep(c): | ||
"""[Override]""" | ||
c.rounds=13 | ||
|
||
# Pass it up the hierarchy | ||
SaltedAlgorithm.final_prep() | ||
|
||
global bcrypt | ||
import bcrypt | ||
|
||
|
||
## def __init__(self, salt): | ||
## super().__init__(salt) | ||
|
||
|
||
def hash(self, plaintext): | ||
return bcrypt.hashpw(plaintext, self.salt) | ||
|
||
|
||
@classmethod | ||
def generate_salt(c): | ||
"""Calculates an encoded salt string, including prefix, for this algorithm.""" | ||
return bcrypt.gensalt(log_rounds=c.rounds) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from ..structure import SaltedAlgorithm | ||
|
||
|
||
class Crypt(SaltedAlgorithm): | ||
name = "crypt" | ||
option = "c" | ||
prefix = "" | ||
suffix = "" | ||
min_length = 13 | ||
salt_length = 2 | ||
|
||
|
||
@classmethod | ||
def recognise_full(c, s): | ||
return len(s) == c.min_length |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from ..structure import SaltedAlgorithm | ||
|
||
|
||
class ExtDes(SaltedAlgorithm): | ||
name = "ext-des" | ||
option = "x" | ||
prefix = "_" | ||
suffix = "" | ||
min_length = 20 | ||
salt_length = 8 | ||
|
||
|
||
## @classmethod | ||
## def recognise_salt(c, s): | ||
## return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from ..structure import SaltedAlgorithm | ||
|
||
|
||
class MD5(SaltedAlgorithm): | ||
name = "md5" | ||
option = "x" | ||
prefix = "$1$" | ||
suffix = "" | ||
min_length = 34 | ||
salt_length = 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import binascii | ||
import hashlib | ||
|
||
from ..structure import Algorithm | ||
|
||
|
||
class MySqlSHA1(Algorithm): | ||
name = "mysql-sha-1" | ||
option = "p" | ||
prefix = "*" | ||
suffix = "" | ||
min_length = 41 | ||
|
||
|
||
def hash(self, plaintext): | ||
input_byte_str = plaintext.encode("UTF-8") | ||
first_round_output = hashlib.sha1(input_byte_str).digest() | ||
second_round_output = hashlib.sha1(first_round_output).digest() | ||
output_byte_str = binascii.hexlify(second_round_output) | ||
return "*" + output_byte_str.decode('ascii').upper() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from ..structure import Algorithm | ||
|
||
|
||
class OldPassword(Algorithm): | ||
"""Pre-v4.1 MySQL, and also newer with the 'old-passwords' setting on | ||
http://djangosnippets.org/snippets/1508/""" | ||
name = "old-password" | ||
option = "o" | ||
prefix = "" | ||
suffix = "" | ||
min_length = 16 | ||
|
||
|
||
@classmethod | ||
def final_prep(c): | ||
"""[Override]""" | ||
# Pass it up the hierarchy | ||
Algorithm.final_prep() | ||
|
||
global mysql_hash_password | ||
from hashpw.contrib.tback import mysql_hash_password | ||
|
||
|
||
def recognise(self, s): | ||
return False | ||
|
||
|
||
def hash(self, plaintext): | ||
return mysql_hash_password(plaintext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import passlib.hash | ||
import passlib.utils | ||
|
||
from ..structure import PLSaltedAlgorithm | ||
|
||
|
||
class PBKDF2(PLSaltedAlgorithm): | ||
name = "django-pbkdf2" | ||
option = "d" | ||
prefix = "pbkdf2_sha256" | ||
suffix = "" | ||
min_length = 70 # prefix + '$' + rounds(2 chars) + '$' + 44 chars | ||
|
||
|
||
# This can't be a @classmethod because parent classes have to work with its properties | ||
@staticmethod | ||
def init(c, *, long_salt): | ||
"""Ensure that check_salt() checks the length of the whole hash.""" | ||
|
||
if long_salt: | ||
c.salt_length = 16 | ||
else: | ||
c.salt_length = 8 | ||
PLSaltedAlgorithm.init(c) | ||
|
||
## c.rounds=260000 | ||
c.rounds=300000 | ||
c.salt_prefix_len = 21 # pbkdf2_sha256$260000$ | ||
c.comp_len = c.salt_prefix_len + c.salt_length | ||
|
||
|
||
@classmethod | ||
def generate_salt(c): | ||
""" | ||
Calculates an encoded salt string, including prefix, for this algorithm. | ||
[Override] | ||
""" | ||
|
||
salt_chars = passlib.utils.getrandstr(passlib.utils.rng, | ||
passlib.hash.django_pbkdf2_sha256.salt_chars, | ||
c.salt_length) | ||
s = "%s$%d$%s$" % (c.prefix, c.rounds, salt_chars) | ||
return s | ||
|
||
|
||
def __init__(self, salt): | ||
super().__init__(salt) | ||
|
||
## print(self.salt[self.salt_prefix_len:]) | ||
startidx = self.salt_prefix_len | ||
endidx = self.salt_prefix_len + self.salt_length | ||
info = { 'salt': self.salt[startidx:endidx], | ||
'rounds': self.rounds } | ||
self.hasher = passlib.hash.django_pbkdf2_sha256.using(**info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from .Phpass import Phpass | ||
from .. import errors | ||
|
||
|
||
class PhpBB3(Phpass): | ||
name = "phpBB3" | ||
option = "B" | ||
prefix = "$H$" | ||
suffix = "" | ||
|
||
|
||
def hash(self, plaintext): | ||
raise errors.BadAlgException(self.name + " not implemented") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import passlib.hash | ||
import passlib.utils.binary | ||
|
||
from ..structure import PLSaltedAlgorithm | ||
|
||
|
||
class Phpass(PLSaltedAlgorithm): | ||
"""https://github.com/exavolt/python-phpass | ||
e.g. portable (safe MD5): $P$Bnvt73R2AZ9NwrY8agFUwI1YUYQEW5/ | ||
Blowfish: $2a$08$iys2/e7hwWyX2YbWtjCyY.tmGy2Y.mGlV9KwIAi9AUPgBuc9rdJVe""" | ||
|
||
name = "phpass" | ||
option = "P" | ||
prefix = "$P$" | ||
suffix = "" | ||
min_length = 34 | ||
salt_length = 9 # includes the round count | ||
|
||
|
||
@classmethod | ||
def final_prep(c): | ||
"""[Override]""" | ||
c.rounds=17 | ||
## c.round_id_chars = "23456789ABCDEFGHIJKLMNOP" | ||
## c.round_id_chars = "789ABCDEFGHIJKLMNOPQRSTU" | ||
|
||
# Pass it up the hierarchy | ||
PLSaltedAlgorithm.final_prep() | ||
|
||
|
||
def __init__(self, salt): | ||
super().__init__(salt) | ||
|
||
self.hasher = passlib.hash.phpass.using(salt=self.salt[4:], rounds=self.rounds) | ||
|
||
|
||
@classmethod | ||
def generate_salt(c): | ||
"""Calculates an encoded salt string, including prefix, for this algorithm.""" | ||
salt_chars = PLSaltedAlgorithm.generate_raw_salt()[0:8] | ||
round_char = passlib.utils.binary.h64.encode_int6(c.rounds).decode("ascii") | ||
s = c.prefix + round_char + salt_chars | ||
return s |
Oops, something went wrong.