Beispiel #1
0
        // internal for testing
        internal static ImmutableArray <byte> GetPublicKey(string keyContainer)
        {
            ICLRStrongName strongName = GetStrongNameInterface();

            IntPtr keyBlob;
            int    keyBlobByteCount;

            strongName.StrongNameGetPublicKey(keyContainer, default(IntPtr), 0, out keyBlob, out keyBlobByteCount);

            byte[] pubKey = new byte[keyBlobByteCount];
            Marshal.Copy(keyBlob, pubKey, 0, keyBlobByteCount);
            strongName.StrongNameFreeBuffer(keyBlob);

            return(pubKey.AsImmutableOrNull());
        }
Beispiel #2
0
        // internal for testing
        /// <exception cref="IOException"/>
        internal static ImmutableArray <byte> GetPublicKey(byte[] keyFileContents)
        {
            try
            {
                var lastSeen = lastSeenKeyPair;
                if (lastSeen != null && ByteSequenceComparer.ValueEquals(lastSeen.Item1, keyFileContents))
                {
                    return(lastSeen.Item2);
                }

                ICLRStrongName strongName = GetStrongNameInterface();

                IntPtr keyBlob;
                int    keyBlobByteCount;

                //EDMAURER use marshal to be safe?
                unsafe
                {
                    fixed(byte *p = keyFileContents)
                    {
                        try
                        {
                            strongName.StrongNameGetPublicKey(null, (IntPtr)p, keyFileContents.Length, out keyBlob, out keyBlobByteCount);
                        }
                        catch (ArgumentException ex)
                        {
                            throw new IOException(ex.Message);
                        }
                    }
                }

                byte[] pubKey = new byte[keyBlobByteCount];
                Marshal.Copy(keyBlob, pubKey, 0, keyBlobByteCount);
                strongName.StrongNameFreeBuffer(keyBlob);

                var result = pubKey.AsImmutableOrNull();
                lastSeenKeyPair = Tuple.Create(keyFileContents, result);

                return(result);
            }
            catch (COMException ex)
            {
                throw new IOException(ex.Message);
            }
        }