Spaces:
Running
Running
Update utils/encryption_utils.py
Browse files- utils/encryption_utils.py +36 -10
utils/encryption_utils.py
CHANGED
@@ -14,10 +14,20 @@ import sys
|
|
14 |
import argparse
|
15 |
from typing import Optional
|
16 |
from cryptography.fernet import Fernet, InvalidToken
|
17 |
-
from .logger import log_error, log_warning
|
18 |
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
|
|
21 |
|
22 |
def _get_key() -> Fernet:
|
23 |
"""Get encryption key with better error messages"""
|
@@ -53,7 +63,6 @@ def _get_key() -> Fernet:
|
|
53 |
log_error(error_msg, error=str(e))
|
54 |
raise RuntimeError(error_msg)
|
55 |
|
56 |
-
|
57 |
def encrypt(plain: str, key: Optional[str] = None) -> str:
|
58 |
"""düz string → enc:..."""
|
59 |
if not plain:
|
@@ -72,7 +81,6 @@ def encrypt(plain: str, key: Optional[str] = None) -> str:
|
|
72 |
log_error("Encryption failed", error=str(e))
|
73 |
raise
|
74 |
|
75 |
-
|
76 |
def decrypt(value: Optional[str], key: Optional[str] = None) -> Optional[str]:
|
77 |
"""enc:... ise çözer, değilse aynen döndürür"""
|
78 |
if value is None or not isinstance(value, str):
|
@@ -104,6 +112,9 @@ def decrypt(value: Optional[str], key: Optional[str] = None) -> Optional[str]:
|
|
104 |
log_error("Decryption error", error=str(e))
|
105 |
raise
|
106 |
|
|
|
|
|
|
|
107 |
|
108 |
def main():
|
109 |
"""CLI entry point"""
|
@@ -112,6 +123,13 @@ def main():
|
|
112 |
formatter_class=argparse.RawDescriptionHelpFormatter,
|
113 |
epilog="""
|
114 |
Examples:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
# Encrypt with environment key
|
116 |
python encryption_utils.py enc "secret message"
|
117 |
|
@@ -121,20 +139,21 @@ Examples:
|
|
121 |
# Decrypt
|
122 |
python encryption_utils.py dec "enc:gAAAAABh..."
|
123 |
|
124 |
-
#
|
125 |
-
python
|
126 |
"""
|
127 |
)
|
128 |
|
129 |
parser.add_argument(
|
130 |
"command",
|
131 |
-
choices=["enc", "dec"],
|
132 |
-
help="Command to execute: 'enc' for encrypt, 'dec' for decrypt"
|
133 |
)
|
134 |
|
135 |
parser.add_argument(
|
136 |
"text",
|
137 |
-
|
|
|
138 |
)
|
139 |
|
140 |
parser.add_argument(
|
@@ -145,10 +164,17 @@ Examples:
|
|
145 |
args = parser.parse_args()
|
146 |
|
147 |
try:
|
148 |
-
if args.command == "
|
|
|
|
|
|
|
|
|
|
|
149 |
result = encrypt(args.text, args.key)
|
150 |
print(result)
|
151 |
else: # dec
|
|
|
|
|
152 |
result = decrypt(args.text, args.key)
|
153 |
print(result)
|
154 |
except Exception as e:
|
|
|
14 |
import argparse
|
15 |
from typing import Optional
|
16 |
from cryptography.fernet import Fernet, InvalidToken
|
|
|
17 |
|
18 |
+
try:
|
19 |
+
from .logger import log_error, log_warning
|
20 |
+
except ImportError:
|
21 |
+
# Fallback to simple print
|
22 |
+
def log_error(msg, error=None):
|
23 |
+
print(f"ERROR: {msg}", file=sys.stderr)
|
24 |
+
if error:
|
25 |
+
print(f"Details: {error}", file=sys.stderr)
|
26 |
+
|
27 |
+
def log_warning(msg):
|
28 |
+
print(f"WARNING: {msg}", file=sys.stderr)
|
29 |
|
30 |
+
_ENV_KEY = "FLARE_TOKEN_KEY"
|
31 |
|
32 |
def _get_key() -> Fernet:
|
33 |
"""Get encryption key with better error messages"""
|
|
|
63 |
log_error(error_msg, error=str(e))
|
64 |
raise RuntimeError(error_msg)
|
65 |
|
|
|
66 |
def encrypt(plain: str, key: Optional[str] = None) -> str:
|
67 |
"""düz string → enc:..."""
|
68 |
if not plain:
|
|
|
81 |
log_error("Encryption failed", error=str(e))
|
82 |
raise
|
83 |
|
|
|
84 |
def decrypt(value: Optional[str], key: Optional[str] = None) -> Optional[str]:
|
85 |
"""enc:... ise çözer, değilse aynen döndürür"""
|
86 |
if value is None or not isinstance(value, str):
|
|
|
112 |
log_error("Decryption error", error=str(e))
|
113 |
raise
|
114 |
|
115 |
+
def generate_key() -> str:
|
116 |
+
"""Generate a new Fernet encryption key"""
|
117 |
+
return Fernet.generate_key().decode()
|
118 |
|
119 |
def main():
|
120 |
"""CLI entry point"""
|
|
|
123 |
formatter_class=argparse.RawDescriptionHelpFormatter,
|
124 |
epilog="""
|
125 |
Examples:
|
126 |
+
# Generate a new key
|
127 |
+
python encryption_utils.py keygen
|
128 |
+
|
129 |
+
# Save generated key to .env file
|
130 |
+
python encryption_utils.py keygen >> .env
|
131 |
+
# Then edit .env to add: FLARE_TOKEN_KEY=<generated-key>
|
132 |
+
|
133 |
# Encrypt with environment key
|
134 |
python encryption_utils.py enc "secret message"
|
135 |
|
|
|
139 |
# Decrypt
|
140 |
python encryption_utils.py dec "enc:gAAAAABh..."
|
141 |
|
142 |
+
# Decrypt with custom key
|
143 |
+
python encryption_utils.py dec "enc:gAAAAABh..." --key "your-32-byte-base64-key"
|
144 |
"""
|
145 |
)
|
146 |
|
147 |
parser.add_argument(
|
148 |
"command",
|
149 |
+
choices=["enc", "dec", "keygen"],
|
150 |
+
help="Command to execute: 'enc' for encrypt, 'dec' for decrypt, 'keygen' to generate new key"
|
151 |
)
|
152 |
|
153 |
parser.add_argument(
|
154 |
"text",
|
155 |
+
nargs="?",
|
156 |
+
help="Text to encrypt or decrypt (not needed for keygen)"
|
157 |
)
|
158 |
|
159 |
parser.add_argument(
|
|
|
164 |
args = parser.parse_args()
|
165 |
|
166 |
try:
|
167 |
+
if args.command == "keygen":
|
168 |
+
key = generate_key()
|
169 |
+
print(key)
|
170 |
+
elif args.command == "enc":
|
171 |
+
if not args.text:
|
172 |
+
parser.error("Text is required for encryption")
|
173 |
result = encrypt(args.text, args.key)
|
174 |
print(result)
|
175 |
else: # dec
|
176 |
+
if not args.text:
|
177 |
+
parser.error("Text is required for decryption")
|
178 |
result = decrypt(args.text, args.key)
|
179 |
print(result)
|
180 |
except Exception as e:
|