@@ -138,56 +138,6 @@ def open_mylogin_cnf(name: str) -> TextIOWrapper | None:
138138 return TextIOWrapper (plaintext )
139139
140140
141- # TODO reuse code between encryption an decryption
142- def encrypt_mylogin_cnf (plaintext : IO [str ]) -> BytesIO :
143- """Encryption of .mylogin.cnf file, analogous to calling
144- mysql_config_editor.
145-
146- Code is based on the python implementation by Kristian Koehntopp
147- https://github.com/isotopp/mysql-config-coder
148-
149- """
150-
151- def realkey (key : bytes ) -> bytes :
152- """Create the AES key from the login key."""
153- rkey = bytearray (16 )
154- for i in range (len (key )):
155- rkey [i % 16 ] ^= key [i ]
156- return bytes (rkey )
157-
158- def encode_line (plaintext : str , real_key : bytes , buf_len : int ) -> bytes :
159- aes = AES .new (real_key , AES .MODE_ECB )
160- text_len = len (plaintext )
161- pad_len = buf_len - text_len
162- pad_chr = bytes (chr (pad_len ), "utf8" )
163- plaintext_b = plaintext .encode () + pad_chr * pad_len
164- encrypted_text = b"" .join ([aes .encrypt (plaintext_b [i : i + 16 ]) for i in range (0 , len (plaintext_b ), 16 )])
165- return encrypted_text
166-
167- LOGIN_KEY_LENGTH = 20
168- key = os .urandom (LOGIN_KEY_LENGTH )
169- real_key = realkey (key )
170-
171- outfile = BytesIO ()
172-
173- outfile .write (struct .pack ("i" , 0 ))
174- outfile .write (key )
175-
176- while True :
177- line = plaintext .readline ()
178- if not line :
179- break
180- real_len = len (line )
181- pad_len = (int (real_len / 16 ) + 1 ) * 16
182-
183- outfile .write (struct .pack ("i" , pad_len ))
184- x = encode_line (line , real_key , pad_len )
185- outfile .write (x )
186-
187- outfile .seek (0 )
188- return outfile
189-
190-
191141def read_and_decrypt_mylogin_cnf (f : BinaryIO ) -> BytesIO | None :
192142 """Read and decrypt the contents of .mylogin.cnf.
193143
0 commit comments