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