11from itertools import cycle
22from pathlib import Path
3- from typing import Union
43
54
65class Eco2 :
@@ -15,7 +14,8 @@ class Eco2:
1514 (8 , 'unknown' ),
1615 )
1716 key = (172 , 41 , 85 , 66 )
18- encoding = 'UTF-8'
17+ header_encoding = 'EUC-KR'
18+ value_encoding = 'UTF-8'
1919 value_ext = '.xml'
2020
2121 @classmethod
@@ -43,8 +43,9 @@ def _decode_header(cls, data: bytes):
4343 b = data
4444 for length , name in cls .header :
4545 value , b = cls ._decode_chunk (b = b , length = length )
46+
4647 try :
47- value = value .decode ('euc-kr' )
48+ value = value .decode (cls . header_encoding )
4849 except ValueError :
4950 pass
5051
@@ -53,42 +54,39 @@ def _decode_header(cls, data: bytes):
5354 return header
5455
5556 @classmethod
56- def _decrypt_eco2_data (cls , data : bytes ):
57- decrypted = cls .decrypt_bytes (data )
58- hl = cls .header_length ()
57+ def _print_header_info (cls , header : bytes ):
58+ header_dict = cls ._decode_header (header )
5959
60- header_bytes = decrypted [:hl ]
61- value_bytes = decrypted [hl :]
62- value = value_bytes .decode ()
60+ print ('Header info:' )
6361
64- return header_bytes , value
62+ for key , value in header_dict .items ():
63+ if key == 'unknown' :
64+ continue
65+
66+ print (f' { key :10s} : { value } ' )
6567
6668 @classmethod
6769 def _write_value (cls , path : Path , value : str ):
68- path .write_text (value .replace ('\r \n ' , '\n ' ), encoding = cls .encoding )
70+ path .write_text (value .replace ('\r \n ' , '\n ' ),
71+ encoding = cls .value_encoding )
6972
7073 @classmethod
7174 def _read_value (cls , path : Path ):
72- return path .read_text (encoding = cls .encoding ).replace ('\n ' , '\r \n ' )
75+ return path .read_text (encoding = cls .value_encoding ).replace ('\n ' , '\r \n ' )
7376
7477 @classmethod
75- def _print_header_info (cls , header : bytes ):
76- header_dict = cls ._decode_header (header )
77-
78- print ('Header info:' )
78+ def _decrypt_eco2_data (cls , data : bytes ):
79+ decrypted = cls .decrypt_bytes (data )
80+ hl = cls .header_length ()
7981
80- for key , value in header_dict . items ():
81- if key == 'unknown' :
82- continue
82+ header_bytes = decrypted [: hl ]
83+ value_bytes = decrypted [ hl :]
84+ value = value_bytes . decode ()
8385
84- print ( f' { key :10s } : { value } ' )
86+ return header_bytes , value
8587
8688 @classmethod
87- def decrypt (cls ,
88- path : Union [str , Path ],
89- save_dir = None ,
90- header_name = None ,
91- value_name = None ):
89+ def decrypt (cls , path , save_dir = None , header_name = None , value_name = None ):
9290 path = Path (path )
9391 save_dir = path .parent if save_dir is None else Path (save_dir )
9492 if not save_dir .exists ():
@@ -110,6 +108,13 @@ def decrypt(cls,
110108 value_path = save_dir .joinpath (value_name + cls .value_ext )
111109 cls ._write_value (path = value_path , value = value )
112110
111+ @classmethod
112+ def _encrypt (cls , header : bytes , value_path : str , save_path : Path ):
113+ value = cls ._read_value (path = value_path )
114+ bvalue = value .encode (cls .value_encoding )
115+ encrypted = cls .encrypt_bytes (header + bvalue )
116+ save_path .write_bytes (encrypted )
117+
113118 @classmethod
114119 def encrypt (cls , header_path , value_path , save_path = None ):
115120 if save_path is None :
@@ -119,22 +124,19 @@ def encrypt(cls, header_path, value_path, save_path=None):
119124 value_path = Path (value_path )
120125 save_path = Path (save_path )
121126
122- bheader = header_path .read_bytes ()
123- cls ._print_header_info (bheader )
124-
125- value = cls ._read_value (path = value_path )
126- bvalue = value .encode ()
127+ header = header_path .read_bytes ()
128+ cls ._print_header_info (header )
127129
128- data = bheader + bvalue
129- encrypted = cls .encrypt_bytes (data )
130-
131- save_path .write_bytes (encrypted )
130+ cls ._encrypt (header = header , value_path = value_path , save_path = save_path )
132131
133132 @classmethod
134133 def encrypt_dir (cls , header_path , value_path , save_dir = None ):
135134 header_path = Path (header_path )
136135 value_path = Path (value_path )
137136
137+ header = header_path .read_bytes ()
138+ cls ._print_header_info (header )
139+
138140 save_dir = value_path if save_dir is None else Path (save_dir )
139141 if not save_dir .is_dir ():
140142 save_dir = save_dir .parent
@@ -145,6 +147,6 @@ def encrypt_dir(cls, header_path, value_path, save_dir=None):
145147 vps = [value_path ]
146148
147149 for vp in vps :
148- cls .encrypt ( header_path = header_path ,
149- value_path = vp ,
150- save_path = save_dir .joinpath (f'{ vp .stem } .eco' ))
150+ cls ._encrypt ( header = header ,
151+ value_path = vp ,
152+ save_path = save_dir .joinpath (f'{ vp .stem } .eco' ))
0 commit comments