37 lines
1.3 KiB
C#
37 lines
1.3 KiB
C#
using System.Globalization;
|
|
using System.Net.Http.Headers;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Manager.YouTube.Util;
|
|
|
|
public static class AuthenticationUtilities
|
|
{
|
|
private const string HeaderScheme = "SAPISIDHASH";
|
|
|
|
// Dave Thomas @ https://stackoverflow.com/a/32065323/9948300
|
|
public static AuthenticationHeaderValue? GetSapisidHashHeader(string sapisid, string origin)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(sapisid) || string.IsNullOrWhiteSpace(origin))
|
|
return null;
|
|
var time = GetTime();
|
|
var sha1 = HashString($"{time} {sapisid} {origin}");
|
|
var completeHash = $"{time}_{sha1}";
|
|
return new AuthenticationHeaderValue(HeaderScheme, completeHash);
|
|
}
|
|
|
|
private static string HashString(string stringData)
|
|
{
|
|
var dataBytes = Encoding.ASCII.GetBytes(stringData);
|
|
var hashData = SHA1.HashData(dataBytes);
|
|
return hashData.Aggregate(string.Empty, (current, item) => current + item.ToString("x2"));
|
|
}
|
|
|
|
private static string GetTime()
|
|
{
|
|
var st = new DateTime(1970, 1, 1);
|
|
var t = DateTime.Now.ToUniversalTime() - st;
|
|
var time = (t.TotalMilliseconds + 0.5).ToString(CultureInfo.InvariantCulture);
|
|
return time[..10];
|
|
}
|
|
} |