-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added cryptogrpahy utlity and API
- Loading branch information
1 parent
56c49ee
commit 7e64ce0
Showing
15 changed files
with
122 additions
and
4 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
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,12 @@ | ||
echo ----- | ||
curl --header "Content-Type: application/json" \ | ||
--request POST \ | ||
--data '{"message": "hello"}' \ | ||
http://localhost:5050/api/v1/encrypt-decrypt/encrypt | ||
|
||
echo ----- | ||
echo "Use results from previous request! Expect to fail otherwise!" | ||
curl --header "Content-Type: application/json" \ | ||
--request POST \ | ||
--data '{"key": "0ZeheiiSjKcKFCriQlvGkogDPCVD52xH2UXwZRzQJYA=", "encrypted_message": "gAAAAABgDhzULt1vg5lnn_Pu-G9mqgnQtwMY_dW3ZSzLtQ_vp0KMKU3hAj9-7We9F0e3SSkRPI1m6Nhvbmex6XQBOMk8bwthQQ=="}' \ | ||
http://localhost:5050/api/v1/encrypt-decrypt/decrypt |
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
Empty file.
49 changes: 49 additions & 0 deletions
49
utilities_for_me/utilities/_encrypt_decrypt/encrypt_decrypt.py
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,49 @@ | ||
from typing import Dict | ||
|
||
from cryptography.fernet import Fernet | ||
|
||
KEY_LENGTH = 32 | ||
SUCCESS = "success" | ||
|
||
UTF_8 = "utf8" | ||
|
||
|
||
def generate_key() -> bytes: | ||
return Fernet.generate_key() | ||
|
||
|
||
def check_key(key: str) -> str: | ||
if not isinstance(key, str): | ||
return "key was not a string" | ||
return SUCCESS | ||
|
||
|
||
def encrypt(message: str) -> Dict[str, str]: | ||
key = generate_key() | ||
endcoded_message = message.encode() | ||
|
||
f = Fernet(key) | ||
encrypted_message = f.encrypt(endcoded_message) | ||
|
||
result = { | ||
"key": str(key, UTF_8), | ||
"encrypted_message": str(encrypted_message, UTF_8), | ||
} | ||
|
||
return result | ||
|
||
|
||
def decrypt(key: str, encrypted_message: str) -> str: | ||
result = check_key(key) | ||
if result != SUCCESS: | ||
return result | ||
|
||
try: | ||
b_key = bytes(key, UTF_8) | ||
b_encrypted_message = bytes(encrypted_message, UTF_8) | ||
f = Fernet(b_key) | ||
decrypted_message = f.decrypt(b_encrypted_message) | ||
return str(decrypted_message, UTF_8) | ||
except ValueError as e: | ||
print(e) | ||
return f"key or message invalid: {e}" |
15 changes: 15 additions & 0 deletions
15
utilities_for_me/utilities/_encrypt_decrypt/test_encrypt_decrypt.py
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 . import encrypt_decrypt | ||
|
||
|
||
def test_encrypt_returns_key_and_encrypted_message(): | ||
result = encrypt_decrypt.encrypt("hello") | ||
assert len(result["key"]) | ||
assert len(result["encrypted_message"]) | ||
|
||
|
||
def test_decrypt_returns_decrypted_message(): | ||
encrypt_result = encrypt_decrypt.encrypt("hello") | ||
k = encrypt_result["key"] | ||
m = encrypt_result["encrypted_message"] | ||
result = encrypt_decrypt.decrypt(k, m) | ||
assert result == "hello" |
Empty file.
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,9 @@ | ||
import hashlib | ||
|
||
SHA_256 = "sha256" | ||
UTF_8 = "utf8" | ||
|
||
|
||
def hash(algorithm: str = SHA_256, data: str = ""): | ||
if algorithm == SHA_256: | ||
return hashlib.sha256(bytes(data, UTF_8)).hexdigest() |
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,5 @@ | ||
from . import hash_data | ||
|
||
|
||
def test_hash_data_hashed_messages_as_expected(): | ||
print(hash_data.hash("sha256", "hello")) |
Empty file.
20 changes: 20 additions & 0 deletions
20
utilities_for_me/web_app/blueprints/api/encrypt_decrypt/bp.py
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 @@ | ||
from flask import Blueprint, request | ||
|
||
from utilities_for_me.utilities._encrypt_decrypt.encrypt_decrypt import encrypt, decrypt | ||
|
||
bp = Blueprint("encrypt-decrypt", __name__, url_prefix="/api/v1/encrypt-decrypt") | ||
|
||
|
||
@bp.route("/encrypt", methods=["POST"]) | ||
def encrypt_handler(): | ||
message = request.get_json(silent=True).get("message", "") | ||
data = encrypt(message) | ||
return {"data": data} | ||
|
||
|
||
@bp.route("/decrypt", methods=["POST"]) | ||
def decrypt_handler(): | ||
key = request.get_json(silent=True).get("key", "") | ||
encrypted_message = request.get_json(silent=True).get("encrypted_message", "") | ||
data = decrypt(key, encrypted_message) | ||
return {"data": data} |
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