forked from Azure/PyRIT
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT license. | ||
|
||
import base64 | ||
|
||
from pyrit.models import PromptDataType | ||
from pyrit.prompt_converter import ConverterResult, PromptConverter | ||
|
||
|
||
class TextToHexConverter(PromptConverter): | ||
|
||
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult: | ||
""" | ||
Converts text to a hexadecimal encoded utf-8 string. | ||
""" | ||
hex_representation = "" | ||
|
||
if not self.input_supported(input_type): | ||
raise ValueError("Input type not supported") | ||
|
||
hex_representation += prompt.encode("utf-8").hex().upper() | ||
|
||
return ConverterResult(output_text=hex_representation, output_type="text") | ||
|
||
def input_supported(self, input_type: PromptDataType) -> bool: | ||
return input_type == "text" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT license. | ||
|
||
import pytest | ||
|
||
from pyrit.prompt_converter import TextToHexConverter, ConverterResult | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_text_to_hex_converter_ascii(): | ||
converter = TextToHexConverter() | ||
prompt = "Test random string[#$!; > 18% \n" # String of ascii characters | ||
expected_output = "546573742072616E646F6D20737472696E675B2324213B203E20313825200A" # hex representation of prompt | ||
result = await converter.convert_async(prompt=prompt, input_type="text") | ||
assert isinstance(result, ConverterResult) | ||
assert result.output_text == expected_output | ||
assert result.output_type == "text" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_text_to_hex_converter_extended_ascii(): | ||
converter = TextToHexConverter() | ||
prompt = "éħæ" # String of extended ascii characters | ||
expected_output = "C3A9C384C2A7C3A6" # hex representation of extended ascii characters | ||
result = await converter.convert_async(prompt=prompt, input_type="text") | ||
assert isinstance(result, ConverterResult) | ||
assert result.output_text == expected_output | ||
assert result.output_type == "text" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_text_to_hex_converter_empty_string(): | ||
converter = TextToHexConverter() | ||
prompt = "" # Empty input string | ||
expected_output = "" # Empty output string | ||
result = await converter.convert_async(prompt=prompt, input_type="text") | ||
assert result.output_text == expected_output | ||
assert result.output_type == "text" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_text_to_hex_converter_multilingual(): | ||
converter = TextToHexConverter() | ||
prompt = "বাংলা 日本語 ᬅᬓ᭄ᬱᬭᬩᬮᬶ" # Bengali, Japanese, Balinese | ||
expected_output = "E0A6ACE0A6BEE0A682E0A6B2E0A6BE20E697A5E69CACE8AA9E20E1AC85E1AC93E1" \ | ||
"AD84E1ACB1E1ACADE1ACA9E1ACAEE1ACB6" # hex representation of multilingual string | ||
result = await converter.convert_async(prompt=prompt, input_type="text") | ||
assert result.output_text == expected_output | ||
assert result.output_type == "text" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_text_to_hex_converter_emoji(): | ||
converter = TextToHexConverter() | ||
prompt = "😊" # Emoji character with code point U+1F60A | ||
expected_output = "F09F988A" # hex representation of '😊' | ||
result = await converter.convert_async(prompt=prompt, input_type="text") | ||
assert isinstance(result, ConverterResult) | ||
assert result.output_text == expected_output | ||
assert result.output_type == "text" |