Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate to weaker pseudo random number generator #9

Merged
merged 1 commit into from
Dec 29, 2022

Conversation

xaevik
Copy link
Contributor

@xaevik xaevik commented Dec 29, 2022

Since Cuid is akin to a Guid and should not be considered cryptographically secure, it makes no sense to have the structure rely on System.Security.Cryptography.RandomNumberGenerator for generating the value of Cuid._r.

Therefore SecureRandom() has been renamed to Random() and will call a static property in Cuid.Context that relies on System.Random instead which generates enough randomness to ensure uniqueness.

As a result, the performance of generating a Cuid improved to be much closer to Guid:

Before

Method Categories Mean Error StdDev Ratio RatioSD
Cuid_NewCuid New() 123.90 ms 1.091 ms 1.020 ms 1.92 0.02
Guid_NewGuid New() 64.57 ms 0.382 ms 0.357 ms 1.00 0.00
Cuid_ToString New()+ToString() 349.98 ms 3.485 ms 3.089 ms 1.99 0.02
Guid_ToString New()+ToString() 175.71 ms 1.298 ms 1.214 ms 1.00 0.00

After

Method Categories Mean Error StdDev Ratio
Cuid_NewCuid New() 44.88 ms 0.270 ms 0.253 ms 0.69
Guid_NewGuid New() 64.82 ms 0.451 ms 0.422 ms 1.00
Cuid_ToString New()+ToString() 264.79 ms 2.313 ms 2.050 ms 1.38
Guid_ToString New()+ToString() 191.59 ms 1.147 ms 1.073 ms 1.00

Signed-off-by: Alan Brault alan.brault@visus.io

@xaevik xaevik added this to the 1.1 milestone Dec 29, 2022
@xaevik xaevik self-assigned this Dec 29, 2022
Signed-off-by: Alan Brault <alan.brault@visus.io>
@sonarcloud
Copy link

sonarcloud bot commented Dec 29, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

88.9% 88.9% Coverage
0.0% 0.0% Duplication

@xaevik xaevik merged commit 85624d6 into main Dec 29, 2022
@xaevik xaevik deleted the feature/randomness-refactor branch December 29, 2022 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant