Iain Buchanan
Buchanan.dev

Buchanan.dev

Super Simple .NET 6

Photo by Lucas Santos on Unsplash

Super Simple .NET 6

BCrypt and crypto-random numbers

Iain Buchanan's photo
Iain Buchanan
·Aug 4, 2022·

1 min read

Table of contents

  • Random Numbers
  • BCrypt

To create a blank .NET program, run the following:

dotnet new console

Random Numbers

This is a stupidly simple .NET program for generating some crypto-random numbers using the operating system's entropy:

Program.cs (the entire file is shown here, using top-level statements from C# 9)

using System.Security.Cryptography;

Console.WriteLine("Here's some random 32 byte base64 strings\n");
var r = RandomNumberGenerator.Create();
byte[] rand = new byte[32];
for (int i = 0; i < 5; i++)
{
    r.GetBytes(rand);
    Console.WriteLine(Convert.ToBase64String(rand));
}
Console.WriteLine();

BCrypt

This is a stupidly simple .NET program for creating BCrypt hashes, which you can use to populate a development database with known hashes.

After running dotnet new console, run dotnet add package BCrypt.Net-Next to install BCrypt.

Program.cs (the entire file is shown here, using top-level statements from C# 9)

Console.WriteLine("Here's some bcrypt hashes for the password 1234\n");
string hash;
for (int i = 0; i < 5; i++)
{
    hash = BCrypt.Net.BCrypt.HashPassword("1234");
    Console.WriteLine(hash);
}
Console.WriteLine();
 
Share this