<commit_before>﻿// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNet.Security.DataProtection.AuthenticatedEncryption;
using Microsoft.AspNet.Security.DataProtection.Cng;
using Microsoft.AspNet.Security.DataProtection.KeyManagement;
using Microsoft.Framework.OptionsModel;

namespace Microsoft.AspNet.Security.DataProtection
{
    /// <summary>
    /// An IDataProtectionProvider that is transient.
    /// </summary>
    /// <remarks>
    /// Payloads generated by a given EphemeralDataProtectionProvider instance can only
    /// be deciphered by that same instance. Once the instance is lost, all ciphertexts
    /// generated by that instance are permanently undecipherable.
    /// </remarks>
    public sealed class EphemeralDataProtectionProvider : IDataProtectionProvider
    {
        private readonly KeyRingBasedDataProtectionProvider _dataProtectionProvider;

        public EphemeralDataProtectionProvider()
        {
            IKeyRingProvider keyringProvider;

            if (OSVersionUtil.IsBCryptOnWin7OrLaterAvailable())
            {
                // Fastest implementation: AES-GCM
                keyringProvider = new EphemeralKeyRing<CngGcmAuthenticatedEncryptorConfigurationOptions>();
            }
            else
            {
                // Slowest implementation: managed CBC + HMAC
                keyringProvider = new EphemeralKeyRing<ManagedAuthenticatedEncryptorConfigurationOptions>();
            }

            _dataProtectionProvider = new KeyRingBasedDataProtectionProvider(keyringProvider);
        }

        public IDataProtector CreateProtector([NotNull] string purpose)
        {
            // just forward to the underlying provider
            return _dataProtectionProvider.CreateProtector(purpose);
        }

        private sealed class DefaultOptionsAccessor<T> : IOptionsAccessor<T> where T : class, new()
        {
            public T Options { get; } = new T();

            public T GetNamedOptions(string name)
            {
                return Options;
            }
        }

        private sealed class EphemeralKeyRing<T> : IKeyRing, IKeyRingProvider
            where T : IInternalConfigurationOptions, new()
        {
            // Currently hardcoded to a 512-bit KDK.
            private const int NUM_BYTES_IN_KDK = 512 / 8;

            public IAuthenticatedEncryptor DefaultAuthenticatedEncryptor { get; } = new T().CreateAuthenticatedEncryptor(ProtectedMemoryBlob.Random(NUM_BYTES_IN_KDK));

            public Guid DefaultKeyId { get; } = default(Guid);

            public IAuthenticatedEncryptor GetAuthenticatedEncryptorByKeyId(Guid keyId, out bool isRevoked)
            {
                isRevoked = false;
                return (keyId == default(Guid)) ? DefaultAuthenticatedEncryptor : null;
            }

            public IKeyRing GetCurrentKeyRing()<commit_msg>Remove dead code from EphemeralDataProtectionProvider.cs.<commit_after>﻿// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNet.Security.DataProtection.AuthenticatedEncryption;
using Microsoft.AspNet.Security.DataProtection.Cng;
using Microsoft.AspNet.Security.DataProtection.KeyManagement;

namespace Microsoft.AspNet.Security.DataProtection
{
    /// <summary>
    /// An IDataProtectionProvider that is transient.
    /// </summary>
    /// <remarks>
    /// Payloads generated by a given EphemeralDataProtectionProvider instance can only
    /// be deciphered by that same instance. Once the instance is lost, all ciphertexts
    /// generated by that instance are permanently undecipherable.
    /// </remarks>
    public sealed class EphemeralDataProtectionProvider : IDataProtectionProvider
    {
        private readonly KeyRingBasedDataProtectionProvider _dataProtectionProvider;

        public EphemeralDataProtectionProvider()
        {
            IKeyRingProvider keyringProvider;

            if (OSVersionUtil.IsBCryptOnWin7OrLaterAvailable())
            {
                // Fastest implementation: AES-GCM
                keyringProvider = new EphemeralKeyRing<CngGcmAuthenticatedEncryptorConfigurationOptions>();
            }
            else
            {
                // Slowest implementation: managed CBC + HMAC
                keyringProvider = new EphemeralKeyRing<ManagedAuthenticatedEncryptorConfigurationOptions>();
            }

            _dataProtectionProvider = new KeyRingBasedDataProtectionProvider(keyringProvider);
        }

        public IDataProtector CreateProtector([NotNull] string purpose)
        {
            // just forward to the underlying provider
            return _dataProtectionProvider.CreateProtector(purpose);
        }

        private sealed class EphemeralKeyRing<T> : IKeyRing, IKeyRingProvider
            where T : IInternalConfigurationOptions, new()
        {
            // Currently hardcoded to a 512-bit KDK.
            private const int NUM_BYTES_IN_KDK = 512 / 8;

            public IAuthenticatedEncryptor DefaultAuthenticatedEncryptor { get; } = new T().CreateAuthenticatedEncryptor(ProtectedMemoryBlob.Random(NUM_BYTES_IN_KDK));

            public Guid DefaultKeyId { get; } = default(Guid);

            public IAuthenticatedEncryptor GetAuthenticatedEncryptorByKeyId(Guid keyId, out bool isRevoked)
            {
                isRevoked = false;
                return (keyId == default(Guid)) ? DefaultAuthenticatedEncryptor : null;
            }

            public IKeyRing GetCurrentKeyRing()
{'fraction_non_alphanumeric': 0.06815429856426224, 'fraction_numerical': 0.0034596090641757483, 'mean_word_length': 2.6296296296296298, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3143817', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1419, 'n_words': 366}