Advertisement
Old-Lost

UnCredential Class

Jan 11th, 2017
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class UnSecureString {
  2.     [string]$EncryptedString
  3.     [byte[]]$Key
  4.     hidden static [void]CheckKeyLength([byte[]]$Key) {
  5.         if ($Key.Length -notin @(16, 24, 32)) {
  6.             throw "Invalid Key length. Must be 16, 24, or 32 bytes"
  7.         }
  8.     }
  9.     static [byte[]]RandomKey() {
  10.         [byte[]]$vKey = New-Object Byte[](32)
  11.         $Rng = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
  12.         $Rng.GetBytes($vKey)
  13.         return $vKey
  14.     }
  15.     hidden static [void]CheckProperties([PSCustomObject]$Data) {
  16.         'Password', 'Key' |
  17.         ? { $_ -notin $Data.PSObject.Properties.Name } |
  18.         Select-Object -First 1 |
  19.         % { throw "[UnSecureString]: [PSCustomObject] missing property $_" }
  20.     }
  21.     static [UnSecureString]Load([string]$Path) {
  22.         $Data = Import-Clixml -Path $Path
  23.         [UnSecureString]::CheckProperties($Data)
  24.         return ([UnSecureString]::new($Data.Password, $Data.Key))
  25.     }
  26.     UnSecureString() { }
  27.     UnSecureString([System.Security.SecureString]$SecureString) {
  28.         $vKey = [UnSecureString]::RandomKey()
  29.         $this.Key = $vKey
  30.         $this.EncryptedString = ConvertFrom-SecureString -SecureString $SecureString -Key $vKey
  31.     }
  32.     UnSecureString([System.Security.SecureString]$SecureString, [byte[]]$Key) {
  33.         [UnSecureString]::CheckKeyLength($Key)
  34.         $this.Key = $Key
  35.         $this.EncryptedString = ConvertFrom-SecureString -SecureString $SecureString -Key $Key
  36.     }
  37.     UnSecureString([string]$String) {
  38.         $SecureString = ConvertTo-SecureString -String $String -AsPlainText -Force
  39.         $vKey = [UnSecureString]::RandomKey()
  40.         $this.Key = $vKey
  41.         $this.EncryptedString = ConvertFrom-SecureString -SecureString $SecureString -Key $vKey
  42.     }
  43.     UnSecureString([string]$EncryptedString, [byte[]]$Key) {
  44.         [UnSecureString]::CheckKeyLength($Key)
  45.         $this.Key = $Key
  46.         $this.EncryptedString = $EncryptedString
  47.     }
  48.     UnSecureString([PSCustomObject]$Data) {
  49.         [UnSecureString]::CheckProperties($Data)
  50.         [UnSecureString]::CheckKeyLength($Data.Key)
  51.         $this.Key = $Data.Key
  52.         $this.EncryptedString = $Data.Password
  53.     }
  54.     [System.Security.SecureString]ToSecureString() {
  55.         [System.Security.SecureString]$SecureString = ConvertTo-SecureString -String $this.EncryptedString -Key $this.Key
  56.         if ($SecureString -isnot [System.Security.SecureString]) { throw "Could not create SecureString object from UnSecureString object" }
  57.         return $SecureString
  58.     }
  59.     [boolean]IsValid() {
  60.         try {
  61.             ConvertTo-SecureString -String $this.EncryptedString -Key $this.Key | Out-Null
  62.             return $true
  63.         } catch {
  64.             return $false
  65.         }
  66.     }
  67.     [boolean]IsNotValid() { return (-not $this.IsValid()) }
  68.     [string]ToString() { return $this.EncryptedString }
  69.     [string]ToString([byte[]]$Key) {
  70.         [UnSecureString]::CheckKeyLength($Key)
  71.         return ($this.ToSecureString() | ConvertFrom-SecureString -Key $Key)
  72.     }
  73.     [string]ToPassword() {
  74.         [System.Security.SecureString]$SecureString = $this.ToSecureString()
  75.         $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($SecureString)
  76.         $result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
  77.         [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
  78.         return $result
  79.     }
  80.     [PSCustomObject]ToPSObject() {
  81.         $f = @{
  82.             Password = $this.EncryptedString
  83.             Key      = $this.Key
  84.         }
  85.         return (New-Object -TypeName PSCustomObject -Property $f)
  86.     }
  87.     [PSCustomObject]ToPSObject([byte[]]$Key) {
  88.         $f = @{
  89.             Password = $this.ToString($Key)
  90.             Key      = $Key
  91.         }
  92.         return (New-Object -TypeName PSCustomObject -Property $f)
  93.     }
  94.     [void]Save([string]$Path) {
  95.         $this.ToPSObject() | Export-Clixml $Path
  96.     }
  97.     [void]Save([string]$Path, [byte[]]$Key) {
  98.         $this.ToPSObject($Key) | Export-Clixml $Path
  99.     }
  100. }
  101.  
  102. class UnCredential {
  103.     [string]$Username
  104.     [UnSecureString]$Password
  105.     hidden static [void]CheckProperties([PSCustomObject]$Data) {
  106.         'Username', 'Password', 'Key' |
  107.         ? { $_ -notin $Data.PSObject.Properties.Name } |
  108.         Select-Object -First 1 |
  109.         % { throw "[UnCredential]: [PSCustomObject] missing property $_" }
  110.     }
  111.     UnCredential([string]$Username, [System.Security.SecureString]$Password) {
  112.         $this.Username = $Username
  113.         $this.Password = [UnsecureString]::new($Password)
  114.     }
  115.     UnCredential([string]$Username, [string]$Password) {
  116.         $this.Username = $Username
  117.         $this.Password = [UnsecureString]::new($Password)
  118.     }
  119.     UnCredential([string]$Path) {
  120.         $Data = Import-Clixml -Path $Path
  121.         [UnCredential]::CheckProperties($Data)
  122.         $this.Username = $Data.Username
  123.         $this.Password = [UnSecureString]::new($Data.Password, $Data.Key)
  124.     }
  125.     UnCredential([string]$Username, [string]$Password, [byte[]]$Key) {
  126.         $this.Username = $Username
  127.         $this.Password = [UnSecureString]::new($Password, $Key)
  128.     }
  129.     UnCredential([PSCustomObject]$Data) {
  130.         [UnCredential]::CheckProperties($Data)
  131.         $this.Username = $Data.Username
  132.         $this.Password = [UnSecureString]::new($Data.Password, $Data.Key)
  133.     }
  134.     UnCredential([System.Management.Automation.PSCredential]$PSCredential) {
  135.         $this.Username = $PSCredential.Username
  136.         $this.Password = [UnSecureString]::new($PSCredential.Password)
  137.     }
  138.     [PSCustomObject]ToPSObject() {
  139.         $f = @{
  140.             Username = $this.Username
  141.             Password = $this.Password.EncryptedString
  142.             Key      = $this.Password.Key
  143.         }
  144.         return (New-Object -TypeName PSCustomObject -Property $f)
  145.     }
  146.     [PSCustomObject]ToPSObject([byte[]]$Key) {
  147.         $f = @{
  148.             Username = $this.Username
  149.             Password = $this.Password.ToString($Key)
  150.             Key      = $Key
  151.         }
  152.         return (New-Object -TypeName PSCustomObject -Property $f)
  153.     }
  154.     [void]Save([string]$Path) {
  155.         $this.ToPSObject() | Export-Clixml $Path
  156.     }
  157.     [void]Save([string]$Path, [byte[]]$Key) {
  158.         $this.ToPSObject($Key) | Export-Clixml $Path
  159.     }
  160.     [System.Management.Automation.PSCredential]ToPSCredential() {
  161.         [System.Security.SecureString]$Pword = $this.Password.ToSecureString()
  162.         [System.Management.Automation.PSCredential]$Cred = New-Object System.Management.Automation.PSCredential($this.Username, $Pword)
  163.         if ($Cred -isnot [System.Management.Automation.PSCredential]) { throw "Could not create PSCredential object from UnCredential object" }
  164.         return $Cred
  165.     }
  166.     [string]ToString() { return $this.Username }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement