Class DateTimeExtensions
- Namespace
- Cuemon.Extensions.Data.Integrity
- Assembly
- Cuemon.Extensions.Data.Integrity.dll
Extension methods for the DateTime struct.
public static class DateTimeExtensions
- Inheritance
-
DateTimeExtensions
Examples
The following example demonstrates generating a
using System;
using Cuemon.Data.Integrity;
using Cuemon.Extensions.Data.Integrity;
using Cuemon.Security;
namespace MyApp.Examples;
public class DateTimeExtensionsExample
{
public static void Main()
{
// Define timestamps for when data was created and last modified
DateTime created = new DateTime(2025, 1, 15, 10, 0, 0, DateTimeKind.Utc);
DateTime modified = new DateTime(2025, 6, 10, 14, 30, 0, DateTimeKind.Utc);
// Basic timestamp-only validator (weak integrity based on timestamps)
CacheValidator timestampValidator = created.GetCacheValidator(modified);
Console.WriteLine($"Created (UTC): {timestampValidator.Created:O}");
Console.WriteLine($"Modified (UTC): {timestampValidator.Modified:O}");
Console.WriteLine($"Checksum (hex): {timestampValidator.Checksum.ToHexadecimalString()}");
// Create a validator using the Timestamp method
// (checksum is derived purely from timestamps)
CacheValidator timeBasedValidator = created.GetCacheValidator(modified,
hashFactory: () => HashFactory.CreateFnv128(),
method: EntityDataIntegrityMethod.Timestamp);
Console.WriteLine($"Time-based method checksum: {timeBasedValidator.Checksum.ToHexadecimalString()}");
// Create a validator with both timestamps and a content checksum
byte[] contentChecksum = HashFactory.CreateFnv128().ComputeHash("content-data").GetBytes();
CacheValidator strongValidator = created.GetCacheValidator(modified, contentChecksum,
validation: EntityDataIntegrityValidation.Strong);
Console.WriteLine($"Strong validator checksum: {strongValidator.Checksum.ToHexadecimalString()}");
Console.WriteLine($"Validation level: {strongValidator.Validation}");
// Create a validator with only the created timestamp (no modified date)
CacheValidator createdOnly = created.GetCacheValidator();
Console.WriteLine($"Created-only checksum: {createdOnly.Checksum.ToHexadecimalString()}");
}
}
Methods
GetCacheValidator(DateTime, DateTime, byte[], EntityDataIntegrityValidation, Func<Hash>, EntityDataIntegrityMethod)
Returns a CacheValidator from the specified parameters.
public static CacheValidator GetCacheValidator(this DateTime created, DateTime modified, byte[] checksum, EntityDataIntegrityValidation validation = EntityDataIntegrityValidation.Weak, Func<Hash> hashFactory = null, EntityDataIntegrityMethod method = EntityDataIntegrityMethod.Unaltered)
Parameters
createdDateTimeA DateTime value for when data this CacheValidator represents was first created.
modifiedDateTimeA DateTime value for when data this CacheValidator represents was last modified.
checksumbyte[]An array of bytes containing a checksum of the data this CacheValidator represents.
validationEntityDataIntegrityValidationA EntityDataIntegrityValidation enumeration value that indicates the validation strength of the specified
checksum. Default is Weak.hashFactoryFunc<Hash>The function delegate that is invoked to produce the HashResult. Default is CreateFnv128(Action<FowlerNollVoOptions>).
methodEntityDataIntegrityMethodA EntityDataIntegrityMethod enumeration value that indicates how a checksum is manipulated. Default is Unaltered.
Returns
- CacheValidator
A CacheValidator that represents the integrity of the specified parameters.
GetCacheValidator(DateTime, DateTime?, Func<Hash>, EntityDataIntegrityMethod)
Returns a CacheValidator from the specified parameters.
public static CacheValidator GetCacheValidator(this DateTime created, DateTime? modified = null, Func<Hash> hashFactory = null, EntityDataIntegrityMethod method = EntityDataIntegrityMethod.Unaltered)
Parameters
createdDateTimeA DateTime value for when data this CacheValidator represents was first created.
modifiedDateTime?A DateTime value for when data this CacheValidator represents was last modified.
hashFactoryFunc<Hash>The function delegate that is invoked to produce the HashResult. Default is CreateFnv128(Action<FowlerNollVoOptions>).
methodEntityDataIntegrityMethodA EntityDataIntegrityMethod enumeration value that indicates how a checksum is manipulated. Default is Unaltered.
Returns
- CacheValidator
A CacheValidator that represents the integrity of the specified parameters.