IdGenerator.Net 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package IdGenerator.Net --version 1.0.0
                    
NuGet\Install-Package IdGenerator.Net -Version 1.0.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="IdGenerator.Net" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="IdGenerator.Net" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="IdGenerator.Net" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add IdGenerator.Net --version 1.0.0
                    
#r "nuget: IdGenerator.Net, 1.0.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.
#:package IdGenerator.Net@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=IdGenerator.Net&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=IdGenerator.Net&version=1.0.0
                    
Install as a Cake Tool

IdGenerator

id生成器, 包含雪花算法和id混淆器

雪花算法

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 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

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,814 12/23/2020
1.0.1 611 12/19/2020
1.0.0 560 12/8/2020