Skip to content

Commit 60370c6

Browse files
authored
Update Set-YubiKey-HOTP.ps1 (#163)
Added optional secret format param to support writing hex secret to csv instead of base32. Added check for OTP capability.
1 parent fe252b5 commit 60370c6

1 file changed

Lines changed: 44 additions & 16 deletions

File tree

Docs/Cookbook/Set-YubiKey-HOTP.ps1

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<#
22
.SYNOPSIS
3-
Programs HOTP to a slot on a YubiKey and outputs to seed file.
3+
Programs HOTP to a slot on a YubiKey and outputs to seed file in Hex or Base32 format.
44
55
.DESCRIPTION
66
This Cmdlet programs HOTP (HMAC-based One-Time Password) to a selected slot on a YubiKey.
@@ -23,46 +23,68 @@ Appends a carriage return (Enter) after the passcode
2323
.PARAMETER Use8Digits
2424
Use 8 digits instead of 6 for the passcode
2525
26+
.PARAMETER SecretFormat
27+
Secret format to use (Base32 or Hex)
28+
2629
.EXAMPLE
2730
.\Set-YubiKey-HOTP.ps1 -ShortPress
28-
Programs HOTP to slot 1 (short press) on the YubiKey.
31+
Programs HOTP to slot 1 (short press) on the YubiKey using Base32 format (default).
2932
3033
.EXAMPLE
3134
.\Set-YubiKey-HOTP.ps1 -LongPress -SendTabFirst -AppendCarriageReturn
32-
Programs HOTP to slot 2 (long press) with TAB before the code and Enter after.
35+
Programs HOTP to slot 2 (long press) with TAB before the code and Enter after, using Base32 format.
3336
3437
.EXAMPLE
3538
.\Set-YubiKey-HOTP.ps1 -ShortPress -Use8Digits
36-
Programs HOTP to slot 1 with 8-digit passcodes.
39+
Programs HOTP to slot 1 with 8-digit passcodes using Base32 format.
40+
41+
.EXAMPLE
42+
.\Set-YubiKey-HOTP.ps1 -ShortPress -SecretFormat Hex
43+
Programs HOTP to slot 1 using hex format (recommended for Cisco Duo and Ping Identity).
44+
45+
.EXAMPLE
46+
.\Set-YubiKey-HOTP.ps1 -LongPress -SecretFormat Hex -SendTabFirst -AppendCarriageReturn
47+
Programs HOTP to slot 2 using hex format with TAB and Enter, suitable for Cisco Duo.
3748
3849
.NOTES
3950
- Requires the powerShellYK module
4051
- Creates/updates a CSV file named 'hotp-secrets.csv' in the current directory
4152
- Each configuration generates a new secret key
4253
- If a YubiKey with the same serial number is programmed again, its entry will be updated
4354
- After programming, prompts to program another YubiKey (Y/n)
55+
- For Cisco Duo and Ping Identity, use hex format with "-SecretFormat Hex"
4456
4557
.LINK
4658
https://github.com/virot/powershellYK/
4759
#>
4860

4961
# Function to program a single YubiKey
50-
function Program-YubiKey {
62+
function Set-YubiKeyHOTPConfig {
5163
param (
5264
[Yubico.YubiKey.Otp.Slot]$Slot,
5365
[switch]$SendTabFirst,
5466
[switch]$AppendCarriageReturn,
5567
[switch]$Use8Digits,
56-
[string]$CsvFilePath
68+
[string]$CsvFilePath,
69+
[ValidateSet('Base32', 'Hex')]
70+
[string]$SecretFormat = 'Base32'
5771
)
5872

5973
# Connect to YubiKey
6074
try {
6175
Connect-Yubikey
62-
$yubiKey = Get-YubiKey
63-
if ($null -eq $yubiKey) {
76+
$yubiKeyInfo = Get-YubiKey
77+
if ($null -eq $yubiKeyInfo) {
6478
throw "No YubiKey found."
6579
}
80+
81+
# Check if the insertedYubiKey supports OTP
82+
if (-not $yubiKeyInfo.AvailableUsbCapabilities.HasFlag([Yubico.YubiKey.YubiKeyCapabilities]::Otp)) {
83+
Clear-Host
84+
Write-Host "This YubiKey does not support OTP functionality." -ForegroundColor Red
85+
Write-Host ""
86+
return $false
87+
}
6688
}
6789
catch {
6890
Clear-Host
@@ -76,8 +98,8 @@ function Program-YubiKey {
7698

7799
# Create new configuration object
78100
$newConfig = [PSCustomObject]@{
79-
'Serial' = $yubiKey.SerialNumber
80-
'Secret' = $result.Base32Secret
101+
'Serial' = $yubiKeyInfo.SerialNumber
102+
'Secret' = if ($SecretFormat -eq 'Hex') { $result.HexSecret } else { $result.Base32Secret }
81103
'Counter' = 0
82104
'Length' = if ($Use8Digits) { 8 } else { 6 }
83105
}
@@ -88,7 +110,7 @@ function Program-YubiKey {
88110

89111
# Check if serial exists and update if found
90112
$updatedData = @($existingData | ForEach-Object {
91-
if ($_.Serial -eq $yubiKey.SerialNumber) {
113+
if ($_.Serial -eq $yubiKeyInfo.SerialNumber) {
92114
$serialExists = $true
93115
$newConfig
94116
} else {
@@ -113,9 +135,9 @@ function Program-YubiKey {
113135
Write-Host "YUBIKEY SUCCESSFULLY PROGRAMMED WITH HOTP TO SLOT!" -ForegroundColor Yellow
114136
Write-Host "*****************************************************************" -ForegroundColor Yellow
115137
if ($serialExists) {
116-
Write-Host "ℹ️ Updated existing entry for YubiKey with serial: $($yubiKey.SerialNumber)" -ForegroundColor Yellow
138+
Write-Host "ℹ️ Updated existing entry for YubiKey with serial: $($yubiKeyInfo.SerialNumber)" -ForegroundColor Yellow
117139
} else {
118-
Write-Host "ℹ️ Added new entry for YubiKey with serial: $($yubiKey.SerialNumber)" -ForegroundColor Yellow
140+
Write-Host "ℹ️ Added new entry for YubiKey with serial: $($yubiKeyInfo.SerialNumber)" -ForegroundColor Yellow
119141
}
120142
Write-Host "📝 Information saved to: $CsvFilePath" -ForegroundColor Yellow
121143
Write-Host ""
@@ -153,7 +175,13 @@ function Set-YubiKeyHOTP {
153175
[Parameter(Mandatory=$False,
154176
HelpMessage = "Use 8 digits instead of 6 for the passcode")]
155177
[switch]
156-
$Use8Digits
178+
$Use8Digits,
179+
180+
[Parameter(Mandatory=$False,
181+
HelpMessage = "Secret format to use (Base32 or Hex)")]
182+
[ValidateSet('Base32', 'Hex')]
183+
[string]
184+
$SecretFormat = 'Base32'
157185
)
158186

159187
begin {
@@ -178,7 +206,7 @@ function Set-YubiKeyHOTP {
178206
[System.Console]::ReadKey() > $null
179207
Clear-Host
180208

181-
if (-not (Program-YubiKey -Slot $slot -SendTabFirst:$SendTabFirst -AppendCarriageReturn:$AppendCarriageReturn -Use8Digits:$Use8Digits -CsvFilePath $csvFilePath)) {
209+
if (-not (Set-YubiKeyHOTPConfig -Slot $slot -SendTabFirst:$SendTabFirst -AppendCarriageReturn:$AppendCarriageReturn -Use8Digits:$Use8Digits -CsvFilePath $csvFilePath -SecretFormat $SecretFormat)) {
182210
return
183211
}
184212

@@ -205,7 +233,7 @@ function Set-YubiKeyHOTP {
205233
[System.Console]::ReadKey() > $null
206234
Clear-Host
207235

208-
if (-not (Program-YubiKey -Slot $slot -SendTabFirst:$SendTabFirst -AppendCarriageReturn:$AppendCarriageReturn -Use8Digits:$Use8Digits -CsvFilePath $csvFilePath)) {
236+
if (-not (Set-YubiKeyHOTPConfig -Slot $slot -SendTabFirst:$SendTabFirst -AppendCarriageReturn:$AppendCarriageReturn -Use8Digits:$Use8Digits -CsvFilePath $csvFilePath -SecretFormat $SecretFormat)) {
209237
continue
210238
}
211239
}

0 commit comments

Comments
 (0)