Skip to content

Instantly share code, notes, and snippets.

@Joaquim3
Created July 28, 2024 11:17
Show Gist options
  • Select an option

  • Save Joaquim3/c12b73b04cef627be0b5f98d0f043899 to your computer and use it in GitHub Desktop.

Select an option

Save Joaquim3/c12b73b04cef627be0b5f98d0f043899 to your computer and use it in GitHub Desktop.
POWERSHELL ⇢ generate random password ⇢ generate salt ⇢ Encrypt password with salt
#--------------------------------------------------------------------
# purpose : 1. generate random password
# 2. generate salt
# 3. Encrypt password with salt
#--------------------------------------------------------------------
#--------------------------------------------------------------------
# purpose : generate a ComputeHash
# params : anystring
# http://jongurgul.com/blog/get-stringhash-get-filehash/
# https://gallery.technet.microsoft.com/scriptcenter/Get-StringHash-aa843f71
#--------------------------------------------------------------------
Function fncGetSha512([String] $String, $HashName = "SHA512")
{
$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()
}
#--------------------------------------------------------------------
# purpose : return alphanumeric string
#--------------------------------------------------------------------
Function Get-RandomAlphanumericString {
[CmdletBinding()]
Param ([int] $length = 8)
Begin{
}
Process{
Write-Output ( -join ((0x30..0x39) + ( 0x41..0x5A) + ( 0x61..0x7A) | Get-Random -Count $length | % {[char]$_}) )
}
}
cls
#--------------------------------------------------------------------
# For Powershell : just clic YES to ALL when prompted.
#--------------------------------------------------------------------
# Disallow script restrictions policies
#--------------------------------------------------------------------
#Set-Executionpolicy RemoteSigned
#Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted"
#--------------------------------------------------------------------
# Initialize Password Module
Add-Type -AssemblyName System.Web #System.Web.Security.Membership.GeneratePassword()
#--------------------------------------------------------------------
# generate random password
$Password = (Get-RandomAlphanumericString -length 11)
# generate salt
$salt = [System.Web.Security.Membership]::GeneratePassword(64,16)
# Encrypt password with salt
$StringToProcess = $Password + $salt
$PwdSha = fncGetSha512 ($StringToProcess)
#--------------------------------------------------------------------
Write-Output "Password : $Password`n"
Write-Output "Salt : $salt`n"
Write-Output "PwdSha : $PwdSha`n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment