Last active
July 22, 2023 14:09
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will produce a raw hex string, not Base64...
(1..64|%{[byte](Get-Random -Max 256)}|foreach ToString X2) -join ''