Skip to content

Instantly share code, notes, and snippets.

@justincjahn
Last active September 12, 2023 01:01
Show Gist options
  • Save justincjahn/5011ee5cb664819ffff95cf60179c940 to your computer and use it in GitHub Desktop.
Save justincjahn/5011ee5cb664819ffff95cf60179c940 to your computer and use it in GitHub Desktop.
Create a new Duo-compatible HOTP secret for Yubikeys
<#
.SYNOPSIS
Generate an HOTP secret compatible with Yubikey and Duo.
.DESCRIPTION
Thanks to Yubico for the example:
https://support.yubico.com/hc/en-us/articles/360015668699-Generating-Base32-string-examples
#>
Function New-HOTPSecret {
[CmdletBinding()]
param(
# The length to generate.
[Parameter(Position = 0, Mandatory = $false, ValueFromPipeline = $false)]
[ValidateScript({ $_ % 2 -eq 0 }, ErrorMessage = "The HOTP secret's length must be an even number.")]
[int] $Length = 32
)
$rng = [Security.Cryptography.RNGCryptoServiceProvider]::Create()
[Byte[]] $buffer = 1
for ($secret = ""; $secret.length -lt $Length) {
$rng.GetBytes($buffer)
# DUO Only supports A-F
if ([char]$buffer[0] -clike "[2-7A-F]") {
$secret += [char]$buffer[0]
}
}
$secret
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment