Skip to content

Commit 233f932

Browse files
committed
Secure token generate method added
1 parent 0ace076 commit 233f932

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ var isPasswordValid = _argon2Id.VerifyHash("yourPassword", hashedPassword);
152152
```csharp
153153
var randomBytes = Random.GenerateBytes(16);
154154
var aesKey = Random.GenerateAes256KeyString();
155+
var unimaginableUniqueAndRandomToken = Random.GenerateSecureToken() //256-bit token in string format
155156
```
156157

157158
### 1.4.5. Password Class

src/Pandatech.Crypto/Pandatech.Crypto.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
<Copyright>MIT</Copyright>
99
<PackageIcon>pandatech.png</PackageIcon>
1010
<PackageReadmeFile>Readme.md</PackageReadmeFile>
11-
<Version>2.5.0</Version>
11+
<Version>2.5.1</Version>
1212
<Title>Pandatech.Crypto</Title>
1313
<PackageTags>Pandatech, library, encryption, hash, algorythms, security</PackageTags>
1414
<Description>PandaTech.Crypto is a .NET library simplifying common cryptograhic functions.</Description>
1515
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-pandatech-crypto</RepositoryUrl>
16-
<PackageReleaseNotes>RandomId generator logic change</PackageReleaseNotes>
16+
<PackageReleaseNotes>Secure token generate method added</PackageReleaseNotes>
1717
</PropertyGroup>
1818

1919
<ItemGroup>
@@ -24,8 +24,8 @@
2424
<ItemGroup>
2525
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" />
2626
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.1" />
27-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
28-
<PackageReference Include="Pandatech.RegexBox" Version="1.2.4" />
27+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
28+
<PackageReference Include="Pandatech.RegexBox" Version="2.0.1" />
2929
</ItemGroup>
3030

3131
</Project>

src/Pandatech.Crypto/Random.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ public static long GenerateIdWithVariableSequence(long previousId, int approxima
2525
var minimumRandRange = approximateSequenceVariability / 25;
2626
var random = System.Random.Shared.NextInt64(minimumRandRange, approximateSequenceVariability + 1);
2727

28-
return (previousId + random);
28+
return previousId + random;
29+
}
30+
31+
public static string GenerateSecureToken()
32+
{
33+
const int length = 32; // 32 bytes = 256 bits
34+
var bytes = new byte[length];
35+
using (var rng = RandomNumberGenerator.Create())
36+
{
37+
rng.GetBytes(bytes);
38+
}
39+
return Convert.ToBase64String(bytes)
40+
.Replace("+", "-") // Make URL-safe
41+
.Replace("/", "_") // Make URL-safe
42+
.TrimEnd('='); // Remove padding
2943
}
3044
}

test/Pandatech.Crypto.Tests/Pandatech.Crypto.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="FluentAssertions" Version="6.12.0" />
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
15-
<PackageReference Include="xunit" Version="2.8.1" />
16-
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
13+
<PackageReference Include="FluentAssertions" Version="6.12.1" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
15+
<PackageReference Include="xunit" Version="2.9.2" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
1717
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1818
<PrivateAssets>all</PrivateAssets>
1919
</PackageReference>

test/Pandatech.Crypto.Tests/RandomTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ public void GeneratePandaId_WithinReasonableIterations_DoesNotProduceDuplicates(
3737
previousId = id;
3838
}
3939
}
40+
41+
[Fact]
42+
public void GenerateSecureToken_ShouldReturnValidUrlSafeString()
43+
{
44+
var token = Random.GenerateSecureToken();
45+
46+
Assert.NotNull(token);
47+
Assert.Equal(43, token.Length); // 32 bytes => 43 Base64 characters (without padding)
48+
Assert.DoesNotContain("+", token);
49+
Assert.DoesNotContain("/", token);
50+
Assert.DoesNotContain("=", token);
51+
}
4052
}

0 commit comments

Comments
 (0)