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

Ask your Windows 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.
Locked
User avatar
kerbob
Posts: 25
Joined: Sun Mar 03, 2013 12:39 pm

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

Post by kerbob » Thu Jun 28, 2018 7:54 am

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
kerbob
Posts: 25
Joined: Sun Mar 03, 2013 12:39 pm

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

Post by kerbob » Thu Jun 28, 2018 8:32 am

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
kerbob
Posts: 25
Joined: Sun Mar 03, 2013 12:39 pm

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

Post by kerbob » Thu Jun 28, 2018 12:40 pm

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!"
}

Locked