IdGenerator.Net 1.0.2

dotnet add package IdGenerator.Net --version 1.0.2
NuGet\Install-Package IdGenerator.Net -Version 1.0.2
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="IdGenerator.Net" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IdGenerator.Net --version 1.0.2
#r "nuget: IdGenerator.Net, 1.0.2"
#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 IdGenerator.Net as a Cake Addin
#addin nuget:?package=IdGenerator.Net&version=1.0.2

// Install IdGenerator.Net as a Cake Tool
#tool nuget:?package=IdGenerator.Net&version=1.0.2

IdGenerator

IdGenerator, include SnowFlake and Id Obfuscator id生成器, 包含雪花算法和id混淆器,

项目地址: https://github.com/choby/IdGenerator

Install-Package IdGenerator.Net 

雪花算法

SnowFlake是Twitter公司采用的一种算法,目的是在分布式系统中产生全局唯一且趋势递增的ID。

组成部分(64bit) 1.第一位 占用1bit,其值始终是0,没有实际作用。 2.时间戳 占用41bit,精确到毫秒,总共可以容纳约69年的时间。 3.工作机器id 占用10bit,其中高位5bit是数据中心ID,低位5bit是工作节点ID,做多可以容纳1024个节点。 4.序列号 占用12bit,每个节点每毫秒0开始不断累加,最多可以累加到4095,一共可以产生4096个ID。

SnowFlake算法在同一毫秒内最多可以生成多少个全局唯一ID呢:: 同一毫秒的ID数量 = 1024 X 4096 = 4194304

var worker = new IdWorker(1, 1);
var id = worker.NextId();

id混淆器 (可用于混淆自增id)

自增id问题分析:

生成短连接的场景中,采用给每个长链接一个ID号,解决了有损压缩和重复的问题,但是因为递增id会造成很大的缺陷。同时因为在大数据量的情况下,新来一个长连接我们不可能通过查询数据库的方式来确认是否已经在数据库存在,因为超长字段在数据库很难被索引,这样就会造成长链接重复。

优点:根据有限的长链生成有限的id,不会重复。

缺点:自增id暴露在外,有很大的安全风险,造成链接信息泄露;不支持长链接重复查询。

解决方案

隐藏递增规律的核心就是使用Feistel 密码结构:

Feistel加密算法的输入是长为2w的明文和一个密钥K=(K1,K2...,Kn)。将明文分组分成左右两半L和R,然后进行n轮迭代,迭代完成后,再将左右两半合并到一起以产生密文分组。

Feistel加密算法能够产生一个非常好用的特点,那就是,在一定的数字范围内(2的n次方),每一个数字都能根据密钥找到唯一的一个匹配对象,这就达到了我们隐藏递增规律的目的。

var idObfuscator = new IdObfuscator();
ulong i = ulong.MaxValue; // 18446744073709551615
var feistelID = idObfuscator.Permute(id); //14585380100699608688
var reFeistelID = idObfuscator.Permute(feistelID); // 18446744073709551615
var base62 = idObfuscator.PermuteToBase62(id); // 62进制:dLNS46oypRo
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.0 is compatible.  netstandard1.1 was computed.  netstandard1.2 was computed.  netstandard1.3 was computed.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  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 tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wp8 was computed.  wp81 was computed.  wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 1,396 12/23/2020
1.0.1 420 12/19/2020
1.0.0 326 12/8/2020