mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-10 18:14:36 +01:00
wip
This commit is contained in:
parent
4a60a5f8a2
commit
33145ea0f2
5 changed files with 106 additions and 62 deletions
87
Dalamud.Bootstrap/SqexArg/EncodedArgument.cs
Normal file
87
Dalamud.Bootstrap/SqexArg/EncodedArgument.cs
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Buffers.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Dalamud.Bootstrap.Crypto;
|
||||
|
||||
namespace Dalamud.Bootstrap.SqexArg
|
||||
{
|
||||
internal sealed class EncodedArgument
|
||||
{
|
||||
private static char[] ChecksumTable = new char[]
|
||||
{
|
||||
'f', 'X', '1', 'p', 'G', 't', 'd', 'S',
|
||||
'5', 'C', 'A', 'P', '4', '_', 'V', 'L'
|
||||
};
|
||||
|
||||
private readonly ReadOnlyMemory<byte> m_data;
|
||||
|
||||
private readonly uint m_key;
|
||||
|
||||
//private readonly Blowfish m_cachedBlowfish;
|
||||
|
||||
public EncodedArgument(ReadOnlyMemory<byte> data, uint key)
|
||||
{
|
||||
m_data = data;
|
||||
m_key = key;
|
||||
}
|
||||
|
||||
public static bool TryParse(string argument, out EncodedArgument? value)
|
||||
{
|
||||
if (argument.Length <= 17)
|
||||
{
|
||||
// does not contain: //**sqex0003 + payload + checksum + **//
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!argument.StartsWith("//**sqex0003") || !argument.EndsWith("**//"))
|
||||
{
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var checksum = argument[^5];
|
||||
var payload = DecodeUrlSafeBase64(argument.Substring(12, argument.Length - 1 - 12 - 4)); // //**sqex0003, checksum, **//
|
||||
|
||||
if (!FindPartialKey(checksum, out var partialKey))
|
||||
{
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var i = 0u; i <= 0xFFF; i++)
|
||||
{
|
||||
var key = (i << 20) | partialKey;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool FindPartialKey(char checksum, out uint recoveredKey)
|
||||
{
|
||||
var index = MemoryExtensions.IndexOf(ChecksumTable, checksum);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
recoveredKey = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
recoveredKey = (uint) (index << 16);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts url safe variant of base64 string to bytes.
|
||||
/// </summary>
|
||||
/// <param name="payload">A url-safe variant of base64 string.</param>
|
||||
private static byte[] DecodeUrlSafeBase64(string payload)
|
||||
{
|
||||
var base64Str = payload
|
||||
.Replace('-', '+')
|
||||
.Replace('_', '/');
|
||||
|
||||
return Convert.FromBase64String(base64Str);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue