Generate ODP (one day password) generated from Username and Date

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jpbobrek
Posts: 25
Last visit: Sat Dec 23, 2023 1:52 pm

Generate ODP (one day password) generated from Username and Date

Post by jpbobrek »

I have a script that makes some registry changes to allow users to do things they normally shouldn't (i.e. use removable media). I would like a function that turns username + date into a One Day Password.

Example : jsmith + 6/28/2018 = JFT6QD

If the user enters the correct code, the script will continue to execute. If incorrect, display wah-wah-wah message.

Has anyone seen such a piece of code or can any expert provide some guidance or suggestions?
User avatar
jpbobrek
Posts: 25
Last visit: Sat Dec 23, 2023 1:52 pm

Re: Generate ODP (one day password) generated from Username and Date

Post by jpbobrek »

I think I figured it out using the Get-StringHash script found here:
https://gallery.technet.microsoft.com/s ... h-aa843f71

$String = Get-StringHash "jsmith 6/28/2018" "SHA1"
$String = $String.substring(0,7)
Write-Host $String
ac1e7c1
User avatar
jpbobrek
Posts: 25
Last visit: Sat Dec 23, 2023 1:52 pm

Re: Generate ODP (one day password) generated from Username and Date

Post by jpbobrek »

Near final code..

Code Generator for Helpdesk:

Code: Select all

Function Get-StringHash([String]$String, $HashName = "MD5")
{
	$StringBuilder = New-Object System.Text.StringBuilder
	[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String)) | %{
		[Void]$StringBuilder.Append($_.ToString("x2"))
	}
	$StringBuilder.ToString()
}
([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
$Username = Read-Host -Prompt "Please enter username in DOMAIN\username format:"
$Username = $Username.ToLower()
$String = ($Username) + (Get-Date -format "dd-MMM-yyyy")
$Hash = Get-StringHash $String
$Hash = $Hash.substring(5, 5)
$Hash
User Script

Code: Select all

Function Get-StringHash([String]$String, $HashName = "MD5")
{
	$StringBuilder = New-Object System.Text.StringBuilder
	[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String)) | %{
		[Void]$StringBuilder.Append($_.ToString("x2"))
	}
	$StringBuilder.ToString()
}
$Username = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$Username = $Username.ToLower()
$String = ($Username) + (Get-Date -format "dd-MMM-yyyy")
$Hash = Get-StringHash $String
$Hash = $Hash.substring(5, 5)
$Code = Read-Host -Prompt "Please enter access code:"
$Code = $code.ToLower()
if ($Hash -Match $Code) {
	Write-Host "True!"
	#Continue doing shtuff
}
else
{
	Write-Host "Womp Womp Womp.. False!"
}
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked