Kavita/Kavita.Common/HashUtil.cs
Joseph Milazzo 51b9d1a45a
OPDS Cleanup (#534)
* Fixed opds url display

* Rewrote how stat collection works, now we check in multiple places and always run stat collection in a background thread, to not block main thread.

* Cleaned up the ParseInfoTest to be more verbose

* Added benchmarking
2021-08-28 15:32:24 -07:00

56 lines
1.6 KiB
C#

using System;
using System.Text;
namespace Kavita.Common
{
public static class HashUtil
{
private static string CalculateCrc(string input)
{
uint mCrc = 0xffffffff;
byte[] bytes = Encoding.UTF8.GetBytes(input);
foreach (byte myByte in bytes)
{
mCrc ^= (uint)myByte << 24;
for (var i = 0; i < 8; i++)
{
if ((Convert.ToUInt32(mCrc) & 0x80000000) == 0x80000000)
{
mCrc = (mCrc << 1) ^ 0x04C11DB7;
}
else
{
mCrc <<= 1;
}
}
}
return $"{mCrc:x8}";
}
/// <summary>
/// Calculates a unique, Anonymous Token that will represent this unique Kavita installation.
/// </summary>
/// <returns></returns>
public static string AnonymousToken()
{
var seed = $"{Environment.ProcessorCount}_{Environment.OSVersion.Platform}_{Configuration.JwtToken}_{Environment.UserName}";
return CalculateCrc(seed);
}
/// <summary>
/// Generates a unique API key to this server instance
/// </summary>
/// <returns></returns>
public static string ApiKey()
{
var id = Guid.NewGuid();
if (id.Equals(Guid.Empty))
{
id = Guid.NewGuid();
}
return id.ToString();
}
}
}