Changeset 1832 for trunk/eraser6/Eraser.Util/Security.cs
- Timestamp:
- 02/12/10 08:39:59 (3 years ago)
- File:
-
- 1 edited
-
trunk/eraser6/Eraser.Util/Security.cs (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/eraser6/Eraser.Util/Security.cs
r1802 r1832 92 92 out wasVerified) && wasVerified; 93 93 } 94 95 /// <summary>96 /// Randomises the provided buffer using CryptGenRandom.97 /// </summary>98 /// <param name="cryptGenRandom">The buffer which receives the random99 /// data. The contents of this buffer can also be used as a random100 /// seed.</param>101 /// <returns>True if the operation suceeded.</returns>102 public static bool Randomise(byte[] buffer)103 {104 return CryptApi.CryptGenRandom(buffer);105 }106 }107 108 internal sealed class CryptApi : IDisposable109 {110 /// <summary>111 /// Constructor.112 /// </summary>113 private CryptApi()114 {115 /* Intel i8xx (82802 Firmware Hub Device) hardware random number generator */116 const string IntelDefaultProvider = "Intel Hardware Cryptographic Service Provider";117 118 handle = new SafeCryptHandle();119 if (NativeMethods.CryptAcquireContext(out handle, null,120 IntelDefaultProvider, NativeMethods.PROV_INTEL_SEC, 0))121 {122 return;123 }124 else if (NativeMethods.CryptAcquireContext(out handle, null,125 null, NativeMethods.PROV_RSA_FULL, 0))126 {127 return;128 }129 else if (Marshal.GetLastWin32Error() == NativeMethods.NTE_BAD_KEYSET)130 {131 //Default keyset doesn't exist, attempt to create a new one132 if (NativeMethods.CryptAcquireContext(out handle, null, null,133 NativeMethods.PROV_RSA_FULL, NativeMethods.CRYPT_NEWKEYSET))134 {135 return;136 }137 }138 139 throw new NotSupportedException("Unable to acquire a cryptographic service provider.");140 }141 142 #region IDisposable Members143 ~CryptApi()144 {145 Dispose(false);146 }147 148 private void Dispose(bool disposing)149 {150 //If we already have run Dispose, then handle will be null.151 if (handle == null)152 return;153 154 if (disposing)155 handle.Close();156 157 //Don't run Dispose again.158 handle = null;159 }160 161 public void Dispose()162 {163 Dispose(true);164 GC.SuppressFinalize(this);165 }166 #endregion167 168 /// <summary>169 /// The GenRandom function fills a buffer with cryptographically random bytes.170 /// </summary>171 /// <param name="buffer">Buffer to receive the returned data. This buffer172 /// must be at least dwLen bytes in length.173 ///174 /// Optionally, the application can fill this buffer with data to use as175 /// an auxiliary random seed.</param>176 public static bool CryptGenRandom(byte[] buffer)177 {178 return NativeMethods.CryptGenRandom(instance.handle, (uint)buffer.Length, buffer);179 }180 181 /// <summary>182 /// The HCRYPTPROV handle.183 /// </summary>184 private SafeCryptHandle handle;185 186 /// <summary>187 /// The global CryptAPI instance.188 /// </summary>189 private static CryptApi instance = new CryptApi();190 }191 192 internal class SafeCryptHandle : SafeHandleZeroOrMinusOneIsInvalid193 {194 public SafeCryptHandle()195 : base(true)196 {197 }198 199 protected override bool ReleaseHandle()200 {201 NativeMethods.CryptReleaseContext(handle, 0u);202 handle = IntPtr.Zero;203 return true;204 }205 94 } 206 95 }
Note: See TracChangeset
for help on using the changeset viewer.
