ขอแนะนำการสร้าง Nuget Packages ด้วย GitHub เพื่อที่เราจะสามารถบริหารจัดการ Package ที่เราต้องการใช้ร่วมกันได้
- new project
dotnet new console -o Macus.Helper
2. New File CryptographyProcessor.cs
using System.Security.Cryptography;
using System.Text;
namespace Macus.HashHelper;public class CryptographyProcessor
{
public string CreateSalt(int size)
{
RNGCryptoServiceProvider? rng = new RNGCryptoServiceProvider();
byte[] buff = new byte[size];
rng.GetBytes(buff);
return Convert.ToBase64String(buff);
} public string GenerateHash(string input, string salt)
{
byte[] bytes = Encoding.UTF8.GetBytes(input + salt);
var sHA256ManagedString = new SHA256Managed();
byte[] hash = sHA256ManagedString.ComputeHash(bytes);
return Convert.ToBase64String(hash);
} public bool AreEqual(string plainTextInput, string hashedInput, string salt){
string newHashedPin = GenerateHash(plainTextInput, salt);
return newHashedPin.Equals(hashedInput);
}
}
3. Generate personal access tokens [In the “note” field, enter the description of your personal token, then select checkboxes: **write:packages**, **read:packages** and **delete:packages**.]
4. Create nuget.config File (สามารถใช้วิธีอื่นได้)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<packageSources>
<clear />
<add key="github" value="https://nuget.pkg.github.com/YOURCOMPANY/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="Username" />
<add key="ClearTextPassword" value="personal_access_tokens" />
</github>
</packageSourceCredentials>
</configuration>
5. Edit Macus.HashHelper.csproj
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<PackageId>Macus.HashHelper</PackageId>
<Version>1.0.0</Version>
<Authors>Pichayean</Authors>
<Company>GitHub</Company>
<PackageDescription>This package adds an YourCompany!</PackageDescription>
<RepositoryUrl>https://github.com/YOURREPOSITORY/PackagesNAME.git</RepositoryUrl>
</PropertyGroup></Project>
6. pack packages
dotnet pack --configuration Release
7. Push packages
dotnet nuget push "bin/Release/Macus.HashHelper.1.0.0.nupkg" --source "github"
8. Check Publish packages
https://github.com/yourrepository?tab=packages
การใช้งาน
- Generate personal access tokens [In the “note” field, enter the description of your personal token, then select checkboxes:**read:packages**]
- Create nuget.config File
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<packageSources>
<clear />
<add key="github" value="https://nuget.pkg.github.com/YOURCOMPANY/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="Username" />
<add key="ClearTextPassword" value="access_tokens_readonly" />
</github>
</packageSourceCredentials>
</configuration>
3. Edit .csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup> <ItemGroup>
<PackageReference Include="Macus.HashHelper" Version="1.0.0" />
</ItemGroup>
</Project>
4. Restore Packages ถือว่าสำเร็จ
dotnet restore
Note: มันมีวิธี การจัดการกับ access_tokens อีกหลายวิธี ขอบคุณที่เข้ามาอ่านนะครับ ผิดพลาดตรงไหนสามารถ แนะนำได้ครับ 🤞🤞
Ref: github.com/packages
Source Code: Macus.HashHelper.git