Skip to content

Commit

Permalink
feat: error handling + various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
filipporomani committed Nov 29, 2024
1 parent 06aad5c commit 72fbeef
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*.gz
zerokno/zerokno.egg-info/PKG-INFO
*.txt
zerokno/__pycache__/zerokno.cpython-312.pyc
test
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# ZeroKno

ZeroKno is a simple, easy to use and lightweight zero-knowledge password storage method for Python.
The keys are stored in plain text, while the passwords are stored as base58-encoded SHA-256 hashes.

This makes it impossible for the application to know the password, while still being able to verify it.
If you want to use this in a production environment, please make sure to use a secure storage method for the keys (e.g. a secure database).

Note that the passwords or values are not encrypted, but hashed. This means that the password cannot be retrieved from the hash, but the hash can be used to verify the password. Basically, you can't recover the password if you lose it.

## Installation

Expand All @@ -13,10 +19,10 @@ pip install zerokno
```python
from zerokno import ZeroKno

# Create a new ZeroKno instance
zk = ZeroKno()
# Create a new ZeroKno instance -- app_secret is a secret key for the application and storage is a directory to store the password hashes
zk = ZeroKno(app_secret, storage)

# Add a new password
# Add a new password -- please note that the password is stored as a hash, while the user id is stored as plain text
zk.store("password", "userid")

# Check password match
Expand All @@ -28,3 +34,8 @@ zk.validate("password", "userid1")
# Error: Userid not found

```

## Errors

- `Key not found` - This error is raised when the user id is not found in the storage
- Any other error is raised when the storage file is not found or the storage file is corrupted or in any other way not accessible
11 changes: 0 additions & 11 deletions main.py

This file was deleted.

12 changes: 12 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import zerokno.zerokno as zerokno # local import

z = zerokno.ZeroKno(storage="./")

pwd1 = input("Enter password: ")

z.store(pwd1, "test")

pwd2 = input("Enter password: ")

print(z.validate(pwd2, "test"))
# if this prints True, then the code is working as expected
21 changes: 15 additions & 6 deletions zerokno/zerokno.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@


class ZeroKno:
def __init__(self, app_secret: str = None, storage: str = None):
def __init__(self, app_secret: str = "", storage: str = None):
self.path = storage
self.secret = app_secret

@staticmethod
def algo(password: str, secret: str) -> str:
return b58encode(sha256(password.encode()).digest())
try: return b58encode(sha256(password.encode()).digest()).decode()
except Exception as e: raise Exception(e)

def store(self, password: str, uid: str) -> bool:
with open(self.path+uid, "w+") as f:
f.write(self.algo(password, self.secret))
try:
with open(self.path+uid, "w+") as f:
f.write(self.algo(password, self.secret))
return True
except Exception as e:
raise Exception(e)


def validate(self, password: str, uid: str) -> bool:
with open(self.path+uid, "r") as f:
return self.algo(password, self.secret) == f.read()
try:
with open(self.path+uid, "r") as f:
return self.algo(password, self.secret) == f.read()
except FileNotFoundError:
raise Exception("Key not found")

0 comments on commit 72fbeef

Please sign in to comment.