HashInt 1.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package HashInt --version 1.4.0
NuGet\Install-Package HashInt -Version 1.4.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HashInt" Version="1.4.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HashInt --version 1.4.0
#r "nuget: HashInt, 1.4.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HashInt as a Cake Addin
#addin nuget:?package=HashInt&version=1.4.0

// Install HashInt as a Cake Tool
#tool nuget:?package=HashInt&version=1.4.0

HashInt

TR "HashInt" adını verdiğimiz bu özel veri tipi, uygulamanızda kullanılan ID değerlerini daha güvenli hale getirmeyi amaçlar. Bu tip, ID değerlerini arayüzde görünmez hale getirerek verilerin gizliliğini ve güvenliğini artırır. Ayrıca, bu yaklaşımı kullanarak tüm ID değerlerini otomatik olarak şifrelemiş olursunuz. Özetle, "HashInt" veri tipi kullanımı, hassas ID verilerini koruma altına almak ve kullanıcıların veya kötü niyetli kişilerin bu verilere erişimini zorlaştırmak için kullanılır. Bu, veri gizliliğini artırmanıza ve uygulamanızın güvenliğini sağlamanıza yardımcı olabilir.

EN The custom data type named "HashInt" is designed to enhance the security of the ID values used within your application. This type conceals ID values from the user interface, thereby bolstering data privacy and security. Additionally, by adopting this approach, all ID values are automatically encrypted. In summary, the use of the "HashInt" data type serves the purpose of safeguarding sensitive ID data and making it challenging for users or malicious actors to access these data. This can help you strengthen data privacy and ensure the security of your application.

Installation

Install the package with [NuGet]

Install-Package hashids.net

Usage

using HashidsNet;
app.UseHashInt(new Hashids("your.Salt", 8));

Source

[JsonConverter(typeof(HashIntJsonConverter))]
[ModelBinder(BinderType = typeof(HashIntBinder))]
public class HashInt
{
    public static Hashids Hasher { get; set; }

    public string Value { get; }
    
    public override string ToString() => Value;
    public int ToInt () => GetId(Value);

    public HashInt() { }
    public HashInt(string value)
    {
        Value = value;
    }
    public HashInt(int? value)
    {
        Value = GetHash(value);
    }

    private static int GetId(string value) => string.IsNullOrWhiteSpace(value) ? 0 : Hasher.DecodeSingle(value);
    private static string GetHash(int? value) => value == null ? null : Hasher.Encode(value.Value);

    public static implicit operator int(HashInt hashInt) => GetId(hashInt?.Value);
    public static implicit operator HashInt(int value) => new(GetHash(value));

    public static implicit operator string(HashInt hashInt) => hashInt.Value;
    public static implicit operator HashInt(string value) => new(value);
}

public class HashIntBinder : IModelBinder
{
    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        if (bindingContext == null)
            throw new ArgumentNullException(nameof(bindingContext));

        var modelName = bindingContext.ModelName;

        var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);
        if (valueProviderResult == ValueProviderResult.None)
            return Task.CompletedTask;

        bindingContext.ModelState.SetModelValue(modelName, valueProviderResult);

        var value = valueProviderResult.FirstValue;

        if (string.IsNullOrEmpty(value))
            return Task.CompletedTask;

        bindingContext.Result = ModelBindingResult.Success(new HashInt(value));
        return Task.CompletedTask;
    }
}

public class HashIntJsonConverter : JsonConverter<HashInt>
{
    public override HashInt Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        return new HashInt(reader.GetString()!);
    }
    
    public override void Write(Utf8JsonWriter writer, HashInt hashInt, JsonSerializerOptions options)
    {
        writer.WriteStringValue(hashInt.ToString());
    }
}

public static class HastIntExtensions
{
    public static void UseHashInt(this IApplicationBuilder app, Hashids hashids)
    {
       HashInt.Hasher = hashids;
    }
}

Sample Model

public class MyEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class MyModel
{
    public HashInt Id { get; set; }
    public string Name { get; set; }
}
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on HashInt:

Package Downloads
HiMapper

HiMapper, Mapper

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 139 2/12/2024
1.4.0 219 11/24/2023
1.0.1 118 10/24/2023