WCP.Tools 8.0.0

dotnet add package WCP.Tools --version 8.0.0
                    
NuGet\Install-Package WCP.Tools -Version 8.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="WCP.Tools" Version="8.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WCP.Tools" Version="8.0.0" />
                    
Directory.Packages.props
<PackageReference Include="WCP.Tools" />
                    
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 WCP.Tools --version 8.0.0
                    
#r "nuget: WCP.Tools, 8.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 WCP.Tools@8.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=WCP.Tools&version=8.0.0
                    
Install as a Cake Addin
#tool nuget:?package=WCP.Tools&version=8.0.0
                    
Install as a Cake Tool

WCP.Tools

WCP.Tools 是一组常用的工具类集合,覆盖分布式 ID 生成、随机数据生成(数字、浮点数、手机号、姓名、身份证等)、图片处理、密码哈希等场景,可用于测试造数、业务开发和通用基础能力封装。

  • Target Framework: net8.0
  • License: Apache-2.0
  • 作者 / Author: 吴存平

安装 (Install)

在 NuGet 中搜索并安装:

  • 包名:WCP.Tools

使用 .NET CLI:

dotnet add package WCP.Tools

使用 Package Manager:

Install-Package WCP.Tools

PackageReference 示例:

<ItemGroup>
  <PackageReference Include="WCP.Tools" Version="8.0.0" />
</ItemGroup>

核心组件 (Core Components)

命名空间:WCP.Tools

  • IdWorker:雪花算法风格的 64 位整型 ID 生成器;
  • RandomHelper:随机整数、小数、布尔、手机号、中文姓名、身份证号等;
  • PasswordHasher:基于 Microsoft.AspNetCore.Cryptography.KeyDerivation 的密码哈希与验证;
  • ImageHandler:依赖 System.Drawing.Common 的图片处理帮助类(缩放/裁剪/水印等,具体功能请参阅源码与 XML 注释);
  • Utilities / PointHelper:其他常用小工具。

示例:分布式 ID 生成 (IdWorker)

using WCP.Tools;

// 可选:在应用启动时设置 WorkerId(1~14)
IdWorker.SetWorkerId(1);

// 在业务中生成全局唯一 long 型 ID
long id1 = IdWorker.NewId();
long id2 = IdWorker.NewId();

IdWorker 基于时间戳 + workerId + 序列号组合,适合日志、业务主键等需要高并发生成的场景。


示例:随机数与测试数据 (RandomHelper)

using WCP.Tools;

var random = new RandomHelper();

// 随机整数
int n1 = random.GenerateIntegerRandom(100);        // [0,100)
int n2 = random.GenerateIntegerRandom(10, 20);      // [10,20)

// 随机布尔
bool flag = random.GenerateBoolRandom();

// 随机小数
double d1 = random.GenerateDoubleRandom();          // 0~1 之间
double d2 = random.GenerateDoubleRandom(3);         // 保留 3 位小数

// 指定范围随机小数
double d3 = random.GenerateRangeDoubleRandom(1.5, 3.5, 2);

// 指定数组中的随机元素
string color = random.GenerateRangeArrayRandom(new[] { "Red", "Green", "Blue" });

// 生成不重复随机整数数组
int[] ids = random.GenerateUniqueIntegerRandom(1, 100, 5);

// 随机手机号
string phone = random.GenerateRandomTel();

// 随机中文姓名(男女)
string manName = random.GenerateManName();
string womanName = random.GenerateWoManName();

// 批量生成随机姓名
var names = random.GenerateName(RandomHelper.Sex.All, 10);

// 随机身份证号
string idCard = random.GenerateRandomIdCard();

示例:密码哈希 (PasswordHasher)

PasswordHasher 使用 Microsoft.AspNetCore.Cryptography.KeyDerivation 提供的 PBKDF2 进行密码哈希和验证(具体方法签名请以源码为准,下面为典型用法示意):

using WCP.Tools;

string password = "P@ssw0rd!";

// 计算哈希(返回包含盐值与算法参数的字符串)
string hash = PasswordHasher.HashPassword(password);

// 验证密码
bool ok = PasswordHasher.VerifyHashedPassword(hash, password);

示例:图片工具 (ImageHandler)

ImageHandler 依赖 System.Drawing.Common,适合在 Windows 环境下进行简单图片处理,例如缩放、裁剪、水印等。下面示例仅为示意,具体方法名请以源码为准:

using System.IO;
using WCP.Tools;

byte[] sourceBytes = File.ReadAllBytes("input.jpg");

// 例如:生成缩略图
byte[] thumbBytes = ImageHandler.Resize(sourceBytes, width: 200, height: 200);

File.WriteAllBytes("thumb.jpg", thumbBytes);

注意事项 (Notes)

  • IdWorker 依赖系统时间,请确保服务器时间单调递增;如出现时间回拨,将抛出异常。
  • RandomHelper 适用于一般业务与测试数据生成,不适用于对安全性要求极高的场景(如验证码、防伪码等)。
  • System.Drawing.Common 在非 Windows 环境支持有限,建议在 Windows 或带完整 GDI+ 支持的环境使用图片相关功能。
  • 处理真实用户数据(如手机号、身份证号)时,请注意合规与隐私保护,随机数据应仅用于测试或演示。

License

本项目使用 Apache-2.0 许可证。详情请参阅 NuGet 包或源码中的 LICENSE 说明。

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.

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
8.0.0 207 11/28/2025
1.0.6 259 11/1/2023
1.0.5 579 7/31/2022
1.0.4 557 6/16/2022
1.0.3 535 10/31/2021
1.0.2 538 7/26/2021
1.0.1 481 7/19/2021
1.0.0 500 5/24/2021