Skip to content

Instantly share code, notes, and snippets.

@AArnott
Created April 8, 2019 20:51
Show Gist options
  • Save AArnott/b4038ef7c6f5a001f1f62785f2faba74 to your computer and use it in GitHub Desktop.
Save AArnott/b4038ef7c6f5a001f1f62785f2faba74 to your computer and use it in GitHub Desktop.
Securely generates new PATs. Useful for a service that wants to issue PATs to users.
using System;
using System.Security.Cryptography;
public static class SecurityUtilities
{
static void Main(string[] args)
{
Console.WriteLine(GeneratePat());
Console.WriteLine(GeneratePat());
Console.WriteLine(GeneratePat());
}
public static string GeneratePat()
{
const int bitStrength = 256;
Span<byte> randomData = stackalloc byte[bitStrength / 8];
RandomNumberGenerator.Fill(randomData);
string pat = Convert.ToBase64String(randomData).Replace('+', '-').Replace('/', '_').TrimEnd('=');
return pat;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment