|
| 1 | +<# |
| 2 | +Copyright 2014 Cloudbase Solutions Srl |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +#> |
| 16 | + |
| 17 | +$Source = @" |
| 18 | +using System; |
| 19 | +using System.Text; |
| 20 | +using System.Runtime.InteropServices; |
| 21 | +
|
| 22 | +namespace PSCloudbase |
| 23 | +{ |
| 24 | + public sealed class Win32CryptApi |
| 25 | + { |
| 26 | + public static long CRYPT_SILENT = 0x00000040; |
| 27 | + public static long CRYPT_VERIFYCONTEXT = 0xF0000000; |
| 28 | + public static int PROV_RSA_FULL = 1; |
| 29 | +
|
| 30 | + [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)] |
| 31 | + [return : MarshalAs(UnmanagedType.Bool)] |
| 32 | + public static extern bool CryptAcquireContext(ref IntPtr hProv, |
| 33 | + StringBuilder pszContainer, // Don't use string, as Powershell replaces $null with an empty string |
| 34 | + StringBuilder pszProvider, // Don't use string, as Powershell replaces $null with an empty string |
| 35 | + uint dwProvType, |
| 36 | + uint dwFlags); |
| 37 | +
|
| 38 | + [DllImport("Advapi32.dll", EntryPoint = "CryptReleaseContext", CharSet = CharSet.Unicode, SetLastError = true)] |
| 39 | + public static extern bool CryptReleaseContext(IntPtr hProv, Int32 dwFlags); |
| 40 | +
|
| 41 | + [DllImport("advapi32.dll", SetLastError=true)] |
| 42 | + public static extern bool CryptGenRandom(IntPtr hProv, uint dwLen, byte[] pbBuffer); |
| 43 | +
|
| 44 | + [DllImport("Kernel32.dll")] |
| 45 | + public static extern uint GetLastError(); |
| 46 | + } |
| 47 | +} |
| 48 | +"@ |
| 49 | + |
| 50 | +Add-Type -TypeDefinition $Source -Language CSharp |
| 51 | + |
| 52 | +function Get-RandomPassword |
| 53 | +{ |
| 54 | + [CmdletBinding()] |
| 55 | + param |
| 56 | + ( |
| 57 | + [parameter(Mandatory=$true)] |
| 58 | + [int]$Length |
| 59 | + ) |
| 60 | + process |
| 61 | + { |
| 62 | + $hProvider = 0 |
| 63 | + try |
| 64 | + { |
| 65 | + if(![PSCloudbase.Win32CryptApi]::CryptAcquireContext([ref]$hProvider, $null, $null, |
| 66 | + [PSCloudbase.Win32CryptApi]::PROV_RSA_FULL, |
| 67 | + ([PSCloudbase.Win32CryptApi]::CRYPT_VERIFYCONTEXT -bor |
| 68 | + [PSCloudbase.Win32CryptApi]::CRYPT_SILENT))) |
| 69 | + { |
| 70 | + throw "CryptAcquireContext failed with error: 0x" + "{0:X0}" -f [PSCloudbase.Win32CryptApi]::GetLastError() |
| 71 | + } |
| 72 | + |
| 73 | + $buffer = New-Object byte[] $Length |
| 74 | + if(![PSCloudbase.Win32CryptApi]::CryptGenRandom($hProvider, $Length, $buffer)) |
| 75 | + { |
| 76 | + throw "CryptGenRandom failed with error: 0x" + "{0:X0}" -f [PSCloudbase.Win32CryptApi]::GetLastError() |
| 77 | + } |
| 78 | + |
| 79 | + $buffer | ForEach-Object { $password += "{0:X0}" -f $_ } |
| 80 | + return $password |
| 81 | + } |
| 82 | + finally |
| 83 | + { |
| 84 | + if($hProvider) |
| 85 | + { |
| 86 | + $retVal = [PSCloudbase.Win32CryptApi]::CryptReleaseContext($hProvider, 0) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments