This commit is contained in:
Mino 2020-03-26 21:55:33 +09:00
parent 16d806a2bb
commit 1775122d03
3 changed files with 41 additions and 16 deletions

View file

@ -15,6 +15,8 @@ namespace Dalamud.Bootstrap.Crypto
/// </remarks>
internal sealed class Blowfish
{
public static int BlockSize => 8;
private BlowfishState m_state;
/// <summary>
@ -50,7 +52,7 @@ namespace Dalamud.Bootstrap.Crypto
private static bool CheckBufferLength(int length) => length switch
{
_ when length % 8 == 0 => true,
_ when length % BlockSize == 0 => true,
_ => false,
};

View file

@ -26,6 +26,19 @@ namespace Dalamud.Bootstrap.SqexArg
return this;
}
public bool ContainsKey(string key) => m_dict.ContainsKey(key);
public bool ContainsValue(string value) => m_dict.ContainsValue(value);
public ArgumentBuilder Remove(string key)
{
m_dict.Remove(key);
return this;
}
public bool TryRemove(string key) => m_dict.Remove(key);
private static void Write(StringBuilder buffer, string key, string value)
{
var escapedKey = EscapeValue(key);

View file

@ -53,7 +53,7 @@ namespace Dalamud.Bootstrap.SqexArg
}
/// <summary>
///
/// Extracts the payload and checksum from the encrypted argument.
/// </summary>
/// <param name="argument"></param>
/// <param name="payload">An encrypted payload encoded in url-safe base64 string extracted. The value is undefined if the function fails.</param>
@ -83,6 +83,13 @@ namespace Dalamud.Bootstrap.SqexArg
Span<byte> keyBytes = stackalloc byte[8];
CreateKey(key, keyBytes);
if (!Extract(argument, out var encryptedStr, out var _))
{
throw new SqexArgException($"Could not extract the argument and checksum from {argument}");
}
var encryptedData = DecodeUrlSafeBase64(encryptedStr);
// Allocate the buffer to store decrypted data
var decryptedData = CreateAlignedBuffer(encryptedData.Length);
@ -101,25 +108,24 @@ namespace Dalamud.Bootstrap.SqexArg
return new EncryptedArgument(decryptedData);
}
private static IMemoryOwner<byte> CreateAlignedBuffer(int minimumSize)
{
// align (by padding) to block size if needed
throw new NotImplementedException("TODO");
}
/// <summary>
///
/// </summary>
/// <param name="decryptedData"></param>
/// <returns></returns>
private static bool CheckDecryptedData(ReadOnlySpan<byte> decryptedData)
/// <param name="minimumSize">Indicates that returned buffer must be larger than `minimumSize` bytes.</param>
/// <returns>
/// A buffer aligned to next multiple of block size.
/// Dispose() must be called when it's not used anymore.
/// </returns>
private static IMemoryOwner<byte> CreateAlignedBuffer(int minimumSize)
{
// TODO
return false;
// align to next multiple of block size.
var alignedSize = (minimumSize + Blowfish.BlockSize - 1) & (~(-Blowfish.BlockSize));
return MemoryPool<byte>.Shared.Rent(alignedSize);
}
/// <summary>
/// Formats the key.
/// Formats a key.
/// </summary>
/// <param name="key">A secret key.</param>
/// <param name="destination">A buffer where formatted key will be stored. This must be larger than 8 bytes.</param>
@ -127,8 +133,7 @@ namespace Dalamud.Bootstrap.SqexArg
{
if (!Utf8Formatter.TryFormat(key, destination, out var _, new StandardFormat('x', 8)))
{
var message = $"BUG: Could not create a key"; // This should not fail but..
throw new InvalidOperationException(message);
throw new InvalidOperationException("BUG: Could not create a key");
}
}
@ -144,5 +149,10 @@ namespace Dalamud.Bootstrap.SqexArg
return Convert.FromBase64String(base64Str);
}
public string Encrypt(uint key)
{
}
}
}