Get-RandomHex.ps1
is a PowerShell script designed to generate cryptographically secure random hexadecimal strings. This is useful for creating keys, tokens, or any scenario where high-entropy random data is required. A common use case is generating a 16-byte (128-bit) key, which aligns with the recommended minimum entropy for session IDs as per OWASP guidelines.
- Generates random hexadecimal strings based on a specified bit length.
- Utilizes cryptographically secure random number generation methods.
- Compatible with multiple .NET versions due to the use of
RandomNumberGenerator.Create()
. - Accepts the
-BitLength
parameter at both the script and function levels. - Handles bit lengths not divisible by 8 by appropriately trimming the output.
To generate a hex string without specifying a bit length (defaults to 256 bits):
.\Get-RandomHex.ps1
To generate a hex string in PowerShell based on a desired bit length:
.\Get-RandomHex.ps1 -BitLength 128
You can import the function and use it directly in your PowerShell session:
- Import the function
. .\Get-RandomHex.ps1
- Generate a 256-bit random hexadecimal string
Get-RandomHexString
- Generate a 128-bit random hexadecimal string
Get-RandomHexString -BitLength 128
- Clone or download this repository to your local machine.
- Run the script directly or import the function into your PowerShell session.
- Running the script directly:
.\Get-RandomHex.ps1
- Importing the function:
. .\Get-RandomHex.ps1 # Note the dot and space before the script path
Contributions are welcome! Please open an issue or submit a pull request for any improvements or fixes. Before submitting a PR, ensure that you've tested your changes and updated any necessary documentation.