fix: fallback to getpass.getuser() when os.getlogin() raises OSError

This commit is contained in:
Liang Jiaqing committed 2026-04-25 22:55:17 +08:00
1 parent 8c0a3d3378
commit f581c5fb18
1 file changed
+4 -2
+4 -2
View File
@@ -1,8 +1,10 @@
"""Keychain: save key to a file, then keys.set("name", file="path"); keys.name.use() to retrieve (use but no print)."""
import json, os, hashlib, pathlib
import json, os, hashlib, pathlib, getpass
_PATH = pathlib.Path.home() / "ga_keychain.enc"
_MASK = hashlib.sha256(f"{os.getlogin()}@ga_keychain".encode()).digest()
try: _user = os.getlogin()
except OSError: _user = getpass.getuser()
_MASK = hashlib.sha256(f"{_user}@ga_keychain".encode()).digest()
def _xor(data: bytes) -> bytes:
return bytes(b ^ _MASK[i % len(_MASK)] for i, b in enumerate(data))