laget.Sqids
2.0.1
Prefix Reserved
This version contains a critical bug when using multiple alphabets that mapped all alphabet versions to the same alphabet. This leads to all alphabet versions encoding to the same hash.
We recommend updating to version 3.1.9 of this package when using multiple alphabets.
See the version list below for details.
dotnet add package laget.Sqids --version 2.0.1
NuGet\Install-Package laget.Sqids -Version 2.0.1
<PackageReference Include="laget.Sqids" Version="2.0.1" />
paket add laget.Sqids --version 2.0.1
#r "nuget: laget.Sqids, 2.0.1"
// Install laget.Sqids as a Cake Addin #addin nuget:?package=laget.Sqids&version=2.0.1 // Install laget.Sqids as a Cake Tool #tool nuget:?package=laget.Sqids&version=2.0.1
laget.Sqids
Note The IDs that Sqids generates are unique and always decode into the same numbers. You can also make them unique to your application (so that they're not the same as everyone else who uses Sqids) by providing a shuffled alphabet.
Default alphabet: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Simple library for using hashed ids in DTOs
Based on https://www.sqids.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.RegisterSqids();
})
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.Build()
.RunAsync();
Salts for the hashes will be loaded from the config file expecting the following format
{
/* ..., */
"Sqids": {
"DefaultAlphabetVersion": "xx",
"Alphabets": {
"xx": "yyyy..."
}
}
}
Where 'xx' is a 2 character version code and 'yyyy' is the alphabet (of unlimited size but must be larger than 5) used by that version.
Warning Sqids needs an alphabet that contains at least 5 unique characters.
Usage
Note You can use any integral numeric type (e.g.
long
,byte
,short
, etc.) as the type argument.int
is just the most common one, but if you need to encode/decode larger numbers, for example, you could uselong
/ulong
instead.
If you're targeting an older framework than .NET 7, SqidsEncoder only supports int, and there is no generic type parameter you need to supply, so just:
Basic usage
var sqid = Sqid.FromInt(42);
var int = sqid.ToInt();
var sqid = Sqid.FromLong(42);
var int = sqid.ToLong();
Usage in a class
public class Dto
{
public Sqid 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.ToSqid() };
[MapperMethod]
public Model DtoToModel(Dto dto) => new() { Id = dto.Id.ToInt() };
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Autofac.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 7.0.4)
- Sqids (>= 2.0.0)
-
.NETStandard 2.1
- Autofac.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 7.0.4)
- Newtonsoft.Json (>= 13.0.3)
- Sqids (>= 2.0.0)
-
net6.0
- Autofac.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 7.0.4)
- Newtonsoft.Json (>= 13.0.3)
- Sqids (>= 2.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.