137 lines
3.9 KiB
C#
137 lines
3.9 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Xml;
|
|
|
|
namespace CinemaLib.Webshare;
|
|
|
|
static class ApiExtensions {
|
|
private static readonly BaseEncoding UnixMD5 = new BaseEncoding("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", false);
|
|
|
|
public static Exception? GetExceptionIfStatusNotOK(this XmlReader r, out string? code)
|
|
{
|
|
string status = r.ReadElementContentAsString("status", "");
|
|
if (status == "OK") {
|
|
code = null;
|
|
return null;
|
|
}
|
|
|
|
code = r.ReadElementContentAsString("code", "");
|
|
string message = r.ReadElementContentAsString("message", "");
|
|
return new IOException(code + ": " + message);
|
|
}
|
|
|
|
public static void ThrowIfStatusNotOK(this XmlReader r)
|
|
{
|
|
Exception? e = GetExceptionIfStatusNotOK(r, out _);
|
|
if (e != null)
|
|
throw e;
|
|
}
|
|
|
|
public static string HashPassword(this string password, string salt) {
|
|
string crypt = MD5Crypt(password, salt);
|
|
byte[] result = SHA1.HashData(Encoding.ASCII.GetBytes(crypt));
|
|
return Convert.ToHexString(result).ToLowerInvariant();
|
|
}
|
|
|
|
private static string MD5Crypt(string password, string salt)
|
|
{
|
|
string prefixString = "$1$";
|
|
byte[] prefixBytes = Encoding.ASCII.GetBytes(prefixString);
|
|
byte[] saltBytes = Encoding.ASCII.GetBytes(salt);
|
|
|
|
byte[] key = Encoding.ASCII.GetBytes(password);
|
|
byte[] truncatedSalt = TruncateAndCopy(saltBytes, 8);
|
|
byte[] crypt = Crypt(key, truncatedSalt, prefixBytes, MD5.Create());
|
|
|
|
string result = prefixString
|
|
+ salt + '$'
|
|
+ UnixMD5.GetString(crypt);
|
|
|
|
return result.TrimEnd('=');
|
|
}
|
|
|
|
private static byte[] Crypt(byte[] key, byte[] salt, byte[] prefix, HashAlgorithm A)
|
|
{
|
|
byte[] H = null, I = null;
|
|
|
|
A.Initialize();
|
|
AddToDigest(A, key);
|
|
AddToDigest(A, salt);
|
|
AddToDigest(A, key);
|
|
FinishDigest(A);
|
|
|
|
I = (byte[])A.Hash.Clone();
|
|
|
|
A.Initialize();
|
|
AddToDigest(A, key);
|
|
AddToDigest(A, prefix);
|
|
AddToDigest(A, salt);
|
|
|
|
AddToDigestRolling(A, I, 0, I.Length, key.Length);
|
|
|
|
int length = key.Length;
|
|
for (int i = 0; i < 31 && length != 0; i++)
|
|
{
|
|
AddToDigest(A, new[] { (length & (1 << i)) != 0 ? (byte)0 : key[0] });
|
|
length &= ~(1 << i);
|
|
}
|
|
FinishDigest(A);
|
|
|
|
H = (byte[])A.Hash.Clone();
|
|
|
|
for (int i = 0; i < 1000; i++)
|
|
{
|
|
A.Initialize();
|
|
if ((i & 1) != 0) { AddToDigest(A, key); }
|
|
if ((i & 1) == 0) { AddToDigest(A, H); }
|
|
if ((i % 3) != 0) { AddToDigest(A, salt); }
|
|
if ((i % 7) != 0) { AddToDigest(A, key); }
|
|
if ((i & 1) != 0) { AddToDigest(A, H); }
|
|
if ((i & 1) == 0) { AddToDigest(A, key); }
|
|
FinishDigest(A);
|
|
|
|
Array.Copy(A.Hash, H, H.Length);
|
|
}
|
|
|
|
byte[] crypt = new byte[H.Length];
|
|
int[] permutation = new[] { 11, 4, 10, 5, 3, 9, 15, 2, 8, 14, 1, 7, 13, 0, 6, 12 };
|
|
Array.Reverse(permutation);
|
|
for (int i = 0; i < crypt.Length; i++)
|
|
{
|
|
crypt[i] = H[permutation[i]];
|
|
}
|
|
|
|
return crypt;
|
|
}
|
|
|
|
private static void AddToDigest(HashAlgorithm algorithm, byte[] buffer)
|
|
{
|
|
AddToDigest(algorithm, buffer, 0, buffer.Length);
|
|
}
|
|
|
|
private static void AddToDigest(HashAlgorithm algorithm, byte[] buffer, int offset, int count)
|
|
{
|
|
algorithm.TransformBlock(buffer, offset, count, buffer, offset);
|
|
}
|
|
|
|
private static void AddToDigestRolling(HashAlgorithm algorithm, byte[] buffer, int offset, int inputCount, int outputCount)
|
|
{
|
|
int count;
|
|
for (count = 0; count < outputCount; count += inputCount)
|
|
{
|
|
AddToDigest(algorithm, buffer, offset, Math.Min(outputCount - count, inputCount));
|
|
}
|
|
}
|
|
|
|
private static void FinishDigest(HashAlgorithm algorithm)
|
|
{
|
|
algorithm.TransformFinalBlock(new byte[0], 0, 0);
|
|
}
|
|
|
|
private static byte[] TruncateAndCopy(byte[] buffer, int maxLength)
|
|
{
|
|
byte[] truncatedBuffer = new byte[Math.Min(buffer.Length, maxLength)];
|
|
Array.Copy(buffer, truncatedBuffer, truncatedBuffer.Length);
|
|
return truncatedBuffer;
|
|
}
|
|
} |