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:
parent
4592eb1fd4
commit
0a4dad6c3d
@ -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()
|
||||
|
@ -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())
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user