using System.Text.RegularExpressions;
namespace Dalamud.Bootstrap.SqexArg
{
internal sealed class EncryptedArgument
{
///
/// A data that is encrypted and encoded in url-safe variant of base64.
///
public string Data { get; }
///
/// A checksum that is used to validate the encryption key.
///
public char Checksum { get; }
public EncryptedArgument(string data, char checksum)
{
Data = data;
Checksum = checksum;
}
///
///
///
/// An argument that is encrypted and usually starts with //**sqex0003 and ends with **//
/// Returns true if successful, false otherwise.
public static bool TryParse(string argument, out EncryptedArgument output)
{
if (!Extract(argument, out var data, out var checksum))
{
output = null!;
return false;
}
output = new EncryptedArgument(data, checksum);
return true;
}
///
/// Extracts the payload and checksum from the encrypted argument.
///
///
/// An encrypted payload extracted. The value is undefined if the function fails.
/// A checksum of the key extracted. The value is undefined if the function fails.
/// Returns true on success, false otherwise.
private static bool Extract(string argument, out string payload, out char checksum)
{
// must start with //**sqex0003, some characters, one checksum character and end with **//
var regex = new Regex(@"^\/\/\*\*sqex0003(?.+)(?.)\*\*\/\/$");
var match = regex.Match(argument);
if (!match.Success)
{
payload = null!;
checksum = '\0';
return false;
}
// Extract
checksum = match.Groups["checksum"].Value[0];
payload = match.Groups["payload"].Value;
return true;
}
public override string ToString() => $"//**sqex0003{Data}{Checksum}**//";
}
}