db_init. Database helper for connecting to or initializing the SQLite database. This docstring was expanded to make future maintenance easier. Returns: Varies.
()
| 675 | This docstring was added automatically to improve maintainability. |
| 676 | |
| 677 | Args: |
| 678 | plaintext: Parameter. |
| 679 | |
| 680 | Returns: |
| 681 | Varies. |
| 682 | """ |
| 683 | nonce = os.urandom(NONCE_LEN) |
| 684 | cipher = Cipher(algorithms.AES(MASTER_KEY), modes.GCM(nonce)) |
| 685 | enc = cipher.encryptor() |
| 686 | ct = enc.update(plaintext.encode("utf-8")) + enc.finalize() |
| 687 | blob = nonce + enc.tag + ct |
| 688 | return base64.urlsafe_b64encode(blob).decode("ascii") |
| 689 | |
| 690 | def aesgcm_decrypt_text(blob_b64: str) -> str: |
| 691 | """aesgcm_decrypt_text. |
| 692 | |
| 693 | Internal helper function. |
| 694 | |
| 695 | This docstring was added automatically to improve maintainability. |
| 696 | |
| 697 | Args: |
| 698 | blob_b64: Parameter. |
| 699 | |
| 700 | Returns: |
| 701 | Varies. |
| 702 | """ |
| 703 | blob = base64.urlsafe_b64decode(blob_b64.encode("ascii")) |
| 704 | nonce = blob[:NONCE_LEN] |
| 705 | tag = blob[NONCE_LEN:NONCE_LEN+TAG_LEN] |
| 706 | ct = blob[NONCE_LEN+TAG_LEN:] |
| 707 | cipher = Cipher(algorithms.AES(MASTER_KEY), modes.GCM(nonce, tag)) |
| 708 | dec = cipher.decryptor() |
| 709 | pt = dec.update(ct) + dec.finalize() |
| 710 | return pt.decode("utf-8", errors="replace") |
| 711 | |
| 712 | # --------------------------- |
| 713 | # Database |
| 714 | # --------------------------- |
| 715 | |
| 716 | def db_connect(): |
| 717 | """db_connect. |
| 718 | |
| 719 | Database helper for connecting to or initializing the SQLite database. |
| 720 | |
| 721 | This docstring was expanded to make future maintenance easier. |
| 722 | |
| 723 | Returns: |
| 724 | Varies. |
| 725 | """ |
| 726 | conn = sqlite3.connect(DB_PATH, check_same_thread=False) |
| 727 | conn.row_factory = sqlite3.Row |
| 728 | conn.execute("PRAGMA foreign_keys = ON;") |
| 729 | return conn |
| 730 | |
| 731 | def now_z() -> str: |
| 732 | """now_z. |
| 733 | |
| 734 | Internal helper function. |
no test coverage detected