1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-14 06:50:47 -05:00

Moved ComputeHash out of the licenses and into an extension method

This commit is contained in:
Conner Turnbull 2025-06-09 15:09:33 -04:00
parent 4592eb1fd4
commit 0a4dad6c3d
No known key found for this signature in database
5 changed files with 12 additions and 12 deletions

View File

@ -1,5 +1,6 @@
using System.Reflection;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Billing.Enums;
@ -12,6 +13,14 @@ namespace Bit.Core.Billing.Licenses.Extensions;
public static class LicenseExtensions
{
public static byte[] ComputeHash(this ILicense license)
{
using (var alg = SHA256.Create())
{
return alg.ComputeHash(license.GetDataBytes(true));
}
}
public static byte[] GetDataBytesWithAttributes(this ILicense license, bool forHash = false)
{
var props = license.GetType()

View File

@ -49,14 +49,6 @@ public abstract class BaseLicense : ILicense
public abstract byte[] GetDataBytes(bool forHash = false);
public byte[] ComputeHash()
{
using (var alg = SHA256.Create())
{
return alg.ComputeHash(GetDataBytes(true));
}
}
public bool VerifySignature(X509Certificate2 certificate)
{
using (var rsa = certificate.GetRSAPublicKey())

View File

@ -16,7 +16,6 @@ public interface ILicense
string Signature { get; set; }
string Token { get; set; }
byte[] GetDataBytes(bool forHash = false);
byte[] ComputeHash();
bool VerifySignature(X509Certificate2 certificate);
byte[] Sign(X509Certificate2 certificate);
}

View File

@ -94,7 +94,7 @@ public class OrganizationLicense : BaseLicense
Trial = org.IsTrialing(subscriptionInfo);
UseAdminSponsoredFamilies = org.UseAdminSponsoredFamilies;
Hash = Convert.ToBase64String(ComputeHash());
Hash = Convert.ToBase64String(this.ComputeHash());
Signature = Convert.ToBase64String(licenseService.SignLicense(this));
}

View File

@ -28,7 +28,7 @@ public class UserLicense : BaseLicense
Refresh = user.CalculateFreshRefreshDate(subscriptionInfo);
Trial = user.IsTrialing(subscriptionInfo);
Hash = Convert.ToBase64String(ComputeHash());
Hash = Convert.ToBase64String(this.ComputeHash());
Signature = Convert.ToBase64String(licenseService.SignLicense(this));
}
@ -47,7 +47,7 @@ public class UserLicense : BaseLicense
Refresh = user.CalculateFreshRefreshDate(null);
Trial = user.IsTrialing(null);
Hash = Convert.ToBase64String(ComputeHash());
Hash = Convert.ToBase64String(this.ComputeHash());
Signature = Convert.ToBase64String(licenseService.SignLicense(this));
}