Пример #1
0
        /// <inheritdoc />
        public byte[] Export(CryptographicPrivateKeyBlobType blobType)
        {
            try
            {
                if (blobType == CryptographicPrivateKeyBlobType.BCryptPrivateKey && this.eccPrivateKeyBlob != null)
                {
                    // Imported keys are always ephemeral and cannot be exported.
                    // But we can make the API work if we have the private key data.
                    // Copy the key data before returning it to avoid sharing an array
                    // with the caller that would allow the caller to change our key data.
                    return(this.eccPrivateKeyBlob.CloneArray());
                }

                return(this.key.Export(CngAsymmetricKeyAlgorithmProvider.GetPlatformKeyBlobType(blobType)));
            }
            catch (CryptographicException ex)
            {
                if (ex.IsNotSupportedException())
                {
                    throw new NotSupportedException(ex.Message, ex);
                }

                throw;
            }
        }
Пример #2
0
        private ECDsaCng CreateCng()
        {
            var cng = new ECDsaCng(this.key);

            // .NET Core 2.1 / UAP has a bug where it throws NullReferenceException from SignHash because it didn't set this property.
            if (cng.HashAlgorithm is null)
            {
                if (CngAsymmetricKeyAlgorithmProvider.GetHashCngAlgorithm(this.algorithm) is { } algorithm)
                {
                    cng.HashAlgorithm = algorithm;
                }
                else
                {
                    throw new NotSupportedException("Hash algorithm " + this.algorithm + " could not be obtained.");
                }
            }
Пример #3
0
 /// <inheritdoc />
 public byte[] ExportPublicKey(CryptographicPublicKeyBlobType blobType)
 {
     return(this.key.Export(CngAsymmetricKeyAlgorithmProvider.GetPlatformKeyBlobType(blobType)));
 }