Skip to content

Instantly share code, notes, and snippets.

@kennwhite
Last active July 22, 2023 14:09
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kennwhite/90b7d3827ae4b2a05222671e5b52b9b6 to your computer and use it in GitHub Desktop.
Save kennwhite/90b7d3827ae4b2a05222671e5b52b9b6 to your computer and use it in GitHub Desktop.
Powershell 1-liner to generate random n-byte key from Windows command line
// Windows equivalent to Linux/Mac: echo $(head -c 64 /dev/urandom | base64 | tr -d '\n')
// Get-Random in Windows 10/Server 2016 PowerShell uses a CSPRNG seed by default.
// Prior to PS 5.1, seed was system clock.
// For Win 10/2016+
powershell -command "[Convert]::ToBase64String((1..64|%{[byte](Get-Random -Max 256)}))"
// For Win 8.x/2012
powershell -command "$r=[byte[]]::new(64);$g=[System.Security.Cryptography.RandomNumberGenerator]::Create();$g.GetBytes($r);[Convert]::ToBase64String($r)"
@onequbit
Copy link

This will produce a raw hex string, not Base64...
(1..64|%{[byte](Get-Random -Max 256)}|foreach ToString X2) -join ''

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment