Skip to content

Instantly share code, notes, and snippets.

@b4tman
Last active November 2, 2023 12:58
Show Gist options
  • Save b4tman/757b95eeb2cf13dad3b2961397240f9f to your computer and use it in GitHub Desktop.
Save b4tman/757b95eeb2cf13dad3b2961397240f9f to your computer and use it in GitHub Desktop.
function encode($s){
return $s | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
}
function decode($s){
$SecureString = $s | ConvertTo-SecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString)
$PlainString = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
return $PlainString
}
function zstr($s) {
$data = [System.Text.Encoding]::UTF8.GetBytes($s)
$ms = New-Object IO.MemoryStream
$msd = New-Object IO.MemoryStream
$ms.Write($data, 0, $data.Length)
$ms.Seek(0,0) | Out-Null
$cs = New-Object IO.Compression.GZipStream($msd, [IO.Compression.CompressionMode]::Compress)
$ms.CopyTo($cs)
$ms.Close()
$cs.Close()
return [System.Convert]::ToBase64String($msd.ToArray())
}
function unzstr($s) {
$data = [System.Convert]::FromBase64String($s)
$ms = New-Object IO.MemoryStream
$msd = New-Object IO.MemoryStream
$ms.Write($data, 0, $data.Length)
$ms.Seek(0,0) | Out-Null
$cs = New-Object IO.Compression.GZipStream($ms, [IO.Compression.CompressionMode]::Decompress)
$cs.CopyTo($msd)
$ms.Close()
$cs.Close()
$msd.Close()
return [System.Text.Encoding]::UTF8.GetString($msd.ToArray())
}
function Compress-String {
param (
[Parameter(Mandatory=$true)]
[string]$inputString
)
$compressedString = ""
$charCount = 1
$prevChar = $inputString[0]
for ($i = 1; $i -lt $inputString.Length; $i++) {
$currentChar = $inputString[$i]
if ($currentChar -eq $prevChar) {
$charCount++
}
else {
if ($charCount -ge 3) {
$compressedString += "$prevChar{$charCount}"
}
else {
$compressedString += [string]$prevChar * $charCount
}
$charCount = 1
$prevChar = $currentChar
}
}
if ($charCount -ge 3) {
$compressedString += "$prevChar{$charCount}"
}
else {
$compressedString += [string]$prevChar * $charCount
}
return $compressedString
}
function Decompress-String {
param (
[Parameter(Mandatory=$true)]
[string]$compressedString
)
$decompressedString = ""
$i = 0
while ($i -lt $compressedString.Length) {
$currentChar = $compressedString[$i]
if ($currentChar -ne "{" -and $currentChar -ne "}") {
$decompressedString += $currentChar
$i++
}
elseif ($currentChar -eq "{") {
$endIndex = $compressedString.IndexOf("}", $i)
$substring = $compressedString.Substring($i + 1, $endIndex - $i - 1)
$repeatCount = [int]$substring - 1
$decompressedString += [string]$compressedString[$i - 1] * $repeatCount
$i = $endIndex + 1
} else {
$i++
}
}
return $decompressedString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment