Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
/ laget.HashId Public archive

Simple library for using hashed ids in DTOs

License

Notifications You must be signed in to change notification settings

laget-se/laget.HashId

Repository files navigation

laget.HashIds

Note Hashids have been upgraded & rebranded as Sqids and we've created a separate package for this here!. This means that this package will no longer be maintained and have been archived as of 2024-04-05.

Simple library for using hashed ids in DTOs

Based on https://hashids.org/

Configuration

This example is shown using Autofac since this is the go-to IoC for us.

await Host.CreateDefaultBuilder()
    .ConfigureContainer<ContainerBuilder>((context, builder) =>
    {
        builder.RegisterHashId();
    })
    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .Build()
    .RunAsync();

Salts for the hashes will be loaded from the config file expecting the following format

{
    /* ..., */
    "HashIds": {
        "DefaultHashVersion": "xx",
        "SaltVersions": {
            "xx": "yyyy..."
        }
    }
}

Where 'xx' is a 2 character version code and 'yyyy' is the salt (of unlimited size) used by that version

Usage

Basic usage

    var hashId = HashId.FromLong(42);

    var longId = hash.ToLong();

Usage in a class

    public class Dto 
    {
        public HashId Id { get; set; }
    }

    // Serializes to { Id: "somehash" }

Example when used with the laget.Mapper nuget package

    public class ModelMapper : IMapper
    {
        [MapperMethod]
        public Dto ModelToDto(Model model) => new() { Id = model.Id.ToHashId() };

        [MapperMethod]
        public Model DtoToModel(Dto dto) => new() { Id = dto.Id.ToLong() };
    }

About

Simple library for using hashed ids in DTOs

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages