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/
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
var hashId = HashId.FromLong(42);
var longId = hash.ToLong();
public class Dto
{
public HashId Id { get; set; }
}
// Serializes to { Id: "somehash" }
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() };
}