Base64Decode

Decodes an input in Base64 format.

python · Filters And Transformers

Details

IDBase64Decode
Languagepython
From Version5.0.0
Docker Imagedemisto/python3:3.12.13.10404775
TagsUtility transformer string incident-action-button

README

Decodes an input in Base64 format.

Script Data


Name Description
Script Type python3
Tags Utility, transformer, string
Cortex XSOAR Version 5.0.0

Inputs


Argument Name Description
value The value to decode in Base64 format.

Outputs


Path Description Type
Base64.decoded The decoded output in Base64 format. string
Base64.originalValue The passed value that was decoded. string

Script Examples

Example command

!Base64Decode value=VGhpcyBpcyBhIHRlc3Q

Context Example

{
    "Base64": {
        "decoded": "This is a test",
        "originalValue": "VGhpcyBpcyBhIHRlc3Q"
    }
}
import pytest
from Base64Decode import HUMAN_READABLE_EMPTY_STRING


@pytest.mark.parametrize(
    "value, expected_result, expected_human_readable",
    [
        ("aGVsbG8=", "hello", "hello"),
        ("dGhpcw==", "this", "this"),
        ("VGhpcyBpcyBhIHRlc3Q", "This is a test", "This is a test"),
        ("VGhpcyBpcyBhIHRlc3Q=", "This is a test", "This is a test"),
        (" aGVs bG 8 ", "hello", "hello"),
        ("", "", HUMAN_READABLE_EMPTY_STRING),
        ("    ", "", HUMAN_READABLE_EMPTY_STRING),
        (" ", "", HUMAN_READABLE_EMPTY_STRING),
    ],
)
def test_decoding(value, expected_result, expected_human_readable):
    """
    Given:
        - A string to decode using Base64.

    When:
        - Running the function decode to decode the given string into a human readable string.

    Then:
        - Validating the value of the decoding process.
    """
    from Base64Decode import decode

    result = decode(value)
    to_context = result.to_context()
    assert to_context.get("EntryContext") == {"Base64": {"originalValue": value, "decoded": expected_result}}
    assert to_context.get("HumanReadable") == expected_human_readable


@pytest.mark.parametrize(
    "value, expected_result",
    [
        ("aGVsbG80l", "aGVsbG80l==="),
        ("dGhpcw", "dGhpcw=="),
        ("VGhpcyBpcyBhIHRlc3Q", "VGhpcyBpcyBhIHRlc3Q="),
        ("ERVKHBEK0ejbce8IUBninli0", "ERVKHBEK0ejbce8IUBninli0"),
        (" ", " "),
        ("", ""),
        ("   ", "   "),
    ],
)
def test_padding(value, expected_result):
    """
    Given:
        - A string to add padding to if necessary (since the text must be a multiplication of 4 in order to decode using Base64).

    When:
        - Running the function add_padding to append '=' to the string in order for the length to be a multiplication of 4.

    Then:
        - Validating the value of the padded string.
    """
    from Base64Decode import add_padding

    padded_result = add_padding(value=value)

    assert padded_result == expected_result