Modifies a cryptographic string by either truncating it, mutating a byte at a specified position, or extending it with null bytes. Parameters: - input_string (str): The string to modify. - action (str): The action to perform ('truncate', 'mutate', 'extend').
(input_string, action="truncate", position=None, extension_length=1)
| 136 | |
| 137 | @staticmethod |
| 138 | def modify_string(input_string, action="truncate", position=None, extension_length=1): |
| 139 | """ |
| 140 | Modifies a cryptographic string by either truncating it, mutating a byte at a specified position, or extending it with null bytes. |
| 141 | |
| 142 | Parameters: |
| 143 | - input_string (str): The string to modify. |
| 144 | - action (str): The action to perform ('truncate', 'mutate', 'extend'). |
| 145 | - position (int): The position to mutate (only used if action is 'mutate'). |
| 146 | - extension_length (int): The number of null bytes to add if action is 'extend'. |
| 147 | |
| 148 | Returns: |
| 149 | - str: The modified string. |
| 150 | """ |
| 151 | if not isinstance(input_string, str): |
| 152 | input_string = str(input_string) |
| 153 | |
| 154 | data, encoding = crypto.format_agnostic_decode(input_string) |
| 155 | if encoding != "base64" and encoding != "hex": |
| 156 | raise ValueError("Input must be either hex or base64 encoded") |
| 157 | |
| 158 | if action == "truncate": |
| 159 | modified_data = data[:-1] # Remove the last byte |
| 160 | elif action == "mutate": |
| 161 | if not position: |
| 162 | position = len(data) // 2 |
| 163 | if position < 0 or position >= len(data): |
| 164 | raise ValueError("Position out of range") |
| 165 | byte_list = list(data) |
| 166 | byte_list[position] = (byte_list[position] + 1) % 256 |
| 167 | modified_data = bytes(byte_list) |
| 168 | elif action == "extend": |
| 169 | modified_data = data + (b"\x00" * extension_length) |
| 170 | elif action == "flip": |
| 171 | if not position: |
| 172 | position = len(data) // 2 |
| 173 | if position < 0 or position >= len(data): |
| 174 | raise ValueError("Position out of range") |
| 175 | byte_list = list(data) |
| 176 | byte_list[position] ^= 0xFF # Flip all bits in the byte at the specified position |
| 177 | modified_data = bytes(byte_list) |
| 178 | else: |
| 179 | raise ValueError("Unsupported action") |
| 180 | return crypto.format_agnostic_encode(modified_data, encoding) |
| 181 | |
| 182 | # Check if the entropy of the data is greater than the threshold, indicating it is likely encrypted |
| 183 | def is_likely_encrypted(self, data, threshold=4.5): |
no test coverage detected