-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added module for unix compatibility #1
Draft
Barakudum
wants to merge
9
commits into
main
Choose a base branch
from
unix-format-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0b2c774
added module for unix compatibility
Barakudum a507811
added library exception classes
Barakudum 65251ed
added bcrypt
Barakudum b2e0d77
added bcrypt to __all__
Barakudum 2682502
code reformat for load_bcrypt
Barakudum cafea26
fixed typo in comparison operator
Barakudum e199aba
new unix.utils
Barakudum 437569d
code reformat
Barakudum 8633bc6
implemented unix-loading for sha256
Barakudum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding=utf-8 -*- | ||
r""" | ||
|
||
""" | ||
|
||
|
||
__all__ = ['PasswordlibError', 'PWLibSyntaxError', 'PWLibBadPrefixError'] | ||
|
||
|
||
class PasswordlibError(Exception): | ||
pass | ||
|
||
|
||
class PWLibSyntaxError(PasswordlibError): | ||
pass | ||
|
||
|
||
class PWLibBadPrefixError(PasswordlibError): | ||
pass |
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,6 @@ | ||
# -*- coding=utf-8 -*- | ||
r""" | ||
Compatibility for the widely used unix formats | ||
""" | ||
from . import encrypt | ||
from . import loading |
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,26 @@ | ||
# -*- coding=utf-8 -*- | ||
r""" | ||
|
||
""" | ||
import typing as t | ||
from ..core import functions as fn | ||
|
||
|
||
__all__ = ['encrypt_sha1'] | ||
|
||
|
||
BCRYPT_PREFIX: t.TypeAlias = t.Union[t.Literal[b"2a"], t.Literal[b"2b"]] | ||
|
||
|
||
def encrypt_bcrypt(password: t.AnyStr, rounds: int = 12, prefix: BCRYPT_PREFIX = "2b") -> str: | ||
import bcrypt | ||
password = fn.get_password_bytes(password) | ||
return bcrypt.hashpw(password, bcrypt.gensalt(rounds=rounds, prefix=prefix)).decode() | ||
|
||
|
||
def encrypt_sha1(password: t.AnyStr) -> str: | ||
from hashlib import sha1 | ||
from base64 import b64encode | ||
password = fn.get_password_bytes(password) | ||
hashed = sha1(password).digest() | ||
return "{SHA1}" + b64encode(hashed).decode() |
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 @@ | ||
# -*- coding=utf-8 -*- | ||
r""" | ||
|
||
""" | ||
from ..core import LoadedTuple | ||
from ..exceptions import * | ||
|
||
|
||
__all__ = ['load_sha1'] | ||
|
||
|
||
def load_bcrypt(dump: str) -> LoadedTuple: | ||
if dump[0] != "$": raise PWLibSyntaxError() | ||
prefix, sep, rest = dump[1:].partition("$") | ||
if sep is None: raise PWLibSyntaxError() | ||
if prefix not in {"2", "2a", "2b", "2x", "2y"}: raise PWLibBadPrefixError("unknown prefix") | ||
rounds, sep, rest = rest.partition("$") | ||
if sep is None: raise PWLibSyntaxError() | ||
iterations = int(rounds) | ||
salt = rest[:22].encode() # note: salt is in a base-64 encoded format | ||
hashed = rest[22:].encode() | ||
return LoadedTuple(algorithm="bcrypt", iterations=iterations, salt=salt, hashed=hashed) | ||
|
||
|
||
def load_sha1(dump: str) -> LoadedTuple: | ||
from base64 import b64decode | ||
if not dump.startswith("{SHA}"): raise PWLibBadPrefixError("Invalid dump format") | ||
b64encoded = dump[6:] | ||
hashed = b64decode(b64encoded) | ||
return LoadedTuple(algorithm="sha1", iterations=-1, salt=b'', hashed=hashed) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Use of a broken or weak cryptographic hashing algorithm on sensitive data High
Copilot Autofix AI 6 months ago
To fix the problem, we should replace the use of the SHA-1 hashing algorithm with a stronger, more secure algorithm suitable for password hashing. One of the best options is to use the
argon2
algorithm, which is designed to be computationally expensive and includes a per-password salt by default.Steps to fix:
PasswordHasher
class from theargon2
library.argon2
hashing logic.