Ignore:
Timestamp:
01/18/10 05:32:15 (5 years ago)
Author:
lowjoel
Message:

Combined the functions in AdvApi?, MsCorEEApi and WintrustApi? to Security.cs

File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/eraser6/CodeReview/Eraser.Util/Security.cs

    r1543 r1552  
    2929namespace Eraser.Util 
    3030{ 
    31     public static class AdvApi 
     31    public static class Security 
    3232    { 
    3333        /// <summary> 
     
    9090            } 
    9191        } 
    92     } 
    93  
    94     public sealed class CryptApi : IDisposable 
     92 
     93        /// <summary> 
     94        /// Verifies the Authenticode signature in a file. 
     95        /// </summary> 
     96        /// <param name="pathToFile">The file to verify.</param> 
     97        /// <returns>True if the file contains a valid Authenticode certificate.</returns> 
     98        public static bool VerifyAuthenticode(string pathToFile) 
     99        { 
     100            IntPtr unionPointer = IntPtr.Zero; 
     101 
     102            try 
     103            { 
     104                NativeMethods.WINTRUST_FILE_INFO fileinfo = new NativeMethods.WINTRUST_FILE_INFO(); 
     105                fileinfo.cbStruct = (uint)Marshal.SizeOf(typeof(NativeMethods.WINTRUST_FILE_INFO)); 
     106                fileinfo.pcwszFilePath = pathToFile; 
     107 
     108                NativeMethods.WINTRUST_DATA data = new NativeMethods.WINTRUST_DATA(); 
     109                data.cbStruct = (uint)Marshal.SizeOf(typeof(NativeMethods.WINTRUST_DATA)); 
     110                data.dwUIChoice = NativeMethods.WINTRUST_DATA.UIChoices.WTD_UI_NONE; 
     111                data.fdwRevocationChecks = NativeMethods.WINTRUST_DATA.RevocationChecks.WTD_REVOKE_NONE; 
     112                data.dwUnionChoice = NativeMethods.WINTRUST_DATA.UnionChoices.WTD_CHOICE_FILE; 
     113                unionPointer = data.pUnion = Marshal.AllocHGlobal((int)fileinfo.cbStruct); 
     114                Marshal.StructureToPtr(fileinfo, data.pUnion, false); 
     115 
     116                Guid guid = NativeMethods.WINTRUST_ACTION_GENERIC_VERIFY_V2; 
     117                return NativeMethods.WinVerifyTrust(IntPtr.Zero, ref guid, ref data) == 0; 
     118            } 
     119            finally 
     120            { 
     121                if (unionPointer != IntPtr.Zero) 
     122                    Marshal.FreeHGlobal(unionPointer); 
     123            } 
     124        } 
     125 
     126        /// <summary> 
     127        /// Gets a value indicating whether the assembly manifest at the supplied 
     128        /// path contains a strong name signature.  
     129        /// </summary> 
     130        /// <param name="assemblyPath">The path to the portable executable (.exe or 
     131        /// .dll) file for the assembly to be verified.</param> 
     132        /// <returns>True if the verification was successful; otherwise, false.</returns> 
     133        /// <remarks>VerifyStrongName is a utility function to check the validity 
     134        /// of an assembly, taking into account registry settings.</remarks> 
     135        public static bool VerifyStrongName(string assemblyPath) 
     136        { 
     137            bool wasVerified = false; 
     138            return NativeMethods.StrongNameSignatureVerificationEx(assemblyPath, false, 
     139                out wasVerified) && wasVerified; 
     140        } 
     141 
     142        /// <summary> 
     143        /// Randomises the provided buffer using CryptGenRandom. 
     144        /// </summary> 
     145        /// <param name="cryptGenRandom">The buffer which receives the random 
     146        /// data. The contents of this buffer can also be used as a random 
     147        /// seed.</param> 
     148        /// <returns>True if the operation suceeded.</returns> 
     149        public static bool Randomise(byte[] buffer) 
     150        { 
     151            return CryptApi.CryptGenRandom(buffer); 
     152        } 
     153    } 
     154 
     155    internal sealed class CryptApi : IDisposable 
    95156    { 
    96157        /// <summary> 
Note: See TracChangeset for help on using the changeset viewer.