@@ -76,27 +76,13 @@ def shift_left_one_bit(data):
7676 return result
7777
7878
79- def pad_prefix_0 ():
80- """Pad prefix for prefix_len_bits=0 (IPv6).
81- Sets separator bit at position 0 (LSB of byte 15).
82- """
83- padded = bytearray (16 )
84- padded [15 ] = 0x01 # Set bit at position 0 (LSB of byte 15)
85- return padded
86-
87-
88- def pad_prefix_96 (data ):
89- """Pad prefix for prefix_len_bits=96 (IPv4).
90- For IPv4, the data always has format: 00...00 ffff xxxx (IPv4-mapped)
91- Result: 00000001 00...00 0000ffff (separator at pos 96, then 96 bits)
92- """
93- # The result is always the same for IPv4 addresses since they all have
94- # the same IPv4-mapped prefix (00...00 ffff)
95- padded = bytearray (16 )
96- padded [3 ] = 0x01 # Set bit at position 96 (bit 0 of byte 4)
97- padded [14 ] = 0xFF
98- padded [15 ] = 0xFF
99- return padded
79+ # Constants for padding prefixes
80+ PAD_PREFIX_0 = bytearray (
81+ [0 ] * 15 + [0x01 ]
82+ ) # Separator bit at position 0 (LSB of byte 15)
83+ PAD_PREFIX_96 = bytearray (
84+ [0 , 0 , 0 , 0x01 ] + [0 ] * 10 + [0xFF , 0xFF ]
85+ ) # Separator at pos 96, then IPv4-mapped prefix
10086
10187
10288def encrypt (ip , key ):
@@ -132,9 +118,9 @@ def encrypt(ip, key):
132118
133119 # Initialize padded_prefix for the starting prefix length
134120 if is_ipv4_mapped (bytes16 ):
135- padded_prefix = pad_prefix_96 ( bytes16 )
121+ padded_prefix = PAD_PREFIX_96 . copy ( )
136122 else : # prefix_start == 0
137- padded_prefix = pad_prefix_0 ()
123+ padded_prefix = PAD_PREFIX_0 . copy ()
138124
139125 # Process each bit position
140126 for prefix_len_bits in range (prefix_start , 128 ):
@@ -196,9 +182,9 @@ def decrypt(encrypted_ip, key):
196182
197183 # Initialize padded_prefix for the starting prefix length
198184 if prefix_start == 0 :
199- padded_prefix = pad_prefix_0 ()
185+ padded_prefix = PAD_PREFIX_0 . copy ()
200186 else : # prefix_start == 96
201- padded_prefix = pad_prefix_96 ( decrypted )
187+ padded_prefix = PAD_PREFIX_96 . copy ( )
202188
203189 # Process each bit position
204190 for prefix_len_bits in range (prefix_start , 128 ):
0 commit comments