<commit_before>/*  Copyright 2012 <NAME> (<EMAIL>)
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Keyczar.Crypto;

namespace Keyczar
{
    /// <summary>
    /// Metadata for Key Types
    /// </summary>
    public class KeyType : Util.StringType
    {

        /// <summary>
        /// Aes key
        /// </summary>
        public static readonly KeyType AES = "AES";
        /// <summary>
        /// Hmac Sha1 key
        /// </summary>
        public static readonly KeyType HMAC_SHA1 = "HMAC_SHA1";
        /// <summary>
        /// DSA Private Key
        /// </summary>
        public static readonly KeyType DSA_PRIV = "DSA_PRIV";
        /// <summary>
        /// Dsa Public key
        /// </summary>
        public static readonly KeyType DSA_PUB = "DSA_PUB";
        /// <summary>
        /// RSA private key
        /// </summary>
        public static readonly KeyType RSA_PRIV = "RSA_PRIV";
        /// <summary>
        /// Rsa public key type
        /// </summary>
        public static readonly KeyType RSA_PUB = "RSA_PUB";

        //Unofficial
        /// <summary>
        /// Unofficial type AES Authenticated Encryption with Associated Data
        /// </summary>
        public static readonly KeyType AES_AEAD = "C#_AES_AEAD";


        /// <summary>
        /// Get KeyType for the clr type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static KeyType ForType(Type type)
        {
            return _specs.Where(it => it.Value.Type == type).Select(it=>it.Key).FirstOrDefault();
        }

		protected static bool DefineSpec(KeyTypeSpec spec){
			if(_specs.ContainsKey(spec.Name.Identifier))
				return false;
			_specs.Add(spec.Name.Identifier, spec);
			return true;
		}

		static KeyType(){
			AES.KeySizes<AesKey>(128,192,256).DefineSpec();
			HMAC_SHA1.KeySizes<HmacSha1Key>(256).WithDigestSizes(20).DefineSpec();
			DSA_PRIV.KeySizes<DsaPrivateKey>(1024).WithDigestSizes(48).IsAsymmetric().DefineSpec();
			DSA_PUB.KeySizes<DsaPublicKey>(1024).WithDigestSizes(48).IsAsymmetric().DefineSpec();
			RSA_PRIV.KeySizes<RsaPrivateKey>(2048, 1024, 4096).WithDigestSizes(256, 128, 512).IsAsymmetric().DefineSpec();
			RSA_PUB.KeySizes<RsaPublicKey>(2048, 1024, 4096 ).WithDigestSizes(256, 128, 512).IsAsymmetric().DefineSpec();
			//Unofficial
			AES_AEAD.KeySizes<Unofficial.AesAeadKey>(256,192,128).IsUnofficial().DefineSpec();
		}

		private static readonly IDictionary<string, KeyTypeSpec> _specs = new Dictionary<string, KeyTypeSpec>();

        public KeyTypeSpec KeySizes<T>(params int[] keySizes) where T: Key
        {

            return new KeyTypeSpec
            {
                Name =  Identifier,
                Type = typeof(T),
                KeySizes = keySizes,
            };
        }

        public class KeyTypeSpec
        {
			internal KeyTypeSpec(){
			}

            public KeyType Name;
            public Type Type;
            public int[] KeySizes;
            public int[] DigestSizes = new int[]{0};
            public bool Unofficial;
            public bool Asymmetric;
            public KeyTypeSpec WithDigestSizes(params int[] sigSizes)
            {
                DigestSizes = sigSizes;
                return this;
            }
            public KeyTypeSpec IsUnofficial()
            {
                Unofficial = true;
                return this;
            }

            public KeyTypeSpec IsAsymmetric()
            {
                Asymmetric = true;
                return this;
            }

			public KeyType DefineSpec(){
				if(KeyType.DefineSpec(this))
					return Name;
				return null;
			}
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="KeyType"/>.
        /// </summary>
        /// <param name="identifer">The identifer.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator KeyType(string identifer)
        {
			if(String.IsNullOrWhiteSpace(identifer))
			   return null;
            return new KeyType(identifer);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="KeyType"/> class.
        /// </summary>
        /// <param name="identifer">The identifer.</param>
        public KeyType(string identifer)
            : base(identifer)
        {


        }

		public static KeyType Name(string identifier){
			return identifier;
		}

        private Type _type;
        private int[] _keySizeOptions;
        private bool? _unofficial;
        private bool? _asymmetric;
        /// <summary>
        /// Gets the key size options.
        /// </summary>
        /// <value>The key size options.</value>
        public int[] KeySizeOptions
        {
            get<commit_msg>Small changes a documentation.<commit_after>/*  Copyright 2012 <NAME> (<EMAIL>)
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Keyczar.Crypto;

namespace Keyczar
{
    /// <summary>
    /// Metadata for Key Types
    /// </summary>
    public class KeyType : Util.StringType
    {

        /// <summary>
        /// Aes key
        /// </summary>
        public static readonly KeyType AES = "AES";
        /// <summary>
        /// Hmac Sha1 key
        /// </summary>
        public static readonly KeyType HMAC_SHA1 = "HMAC_SHA1";
        /// <summary>
        /// DSA Private Key
        /// </summary>
        public static readonly KeyType DSA_PRIV = "DSA_PRIV";
        /// <summary>
        /// Dsa Public key
        /// </summary>
        public static readonly KeyType DSA_PUB = "DSA_PUB";
        /// <summary>
        /// RSA private key
        /// </summary>
        public static readonly KeyType RSA_PRIV = "RSA_PRIV";
        /// <summary>
        /// Rsa public key type
        /// </summary>
        public static readonly KeyType RSA_PUB = "RSA_PUB";

        //Unofficial
        /// <summary>
        /// Unofficial type AES Authenticated Encryption with Associated Data
        /// </summary>
        public static readonly KeyType AES_AEAD = "C#_AES_AEAD";


        /// <summary>
        /// Get KeyType for the clr type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static KeyType ForType(Type type)
        {
            return _specs.Where(it => it.Value.Type == type).Select(it=>it.Key).FirstOrDefault();
        }

        /// <summary>
        /// Defines the spec.
        /// </summary>
        /// <param name="spec">The spec.</param>
        /// <returns></returns>
		protected static bool DefineSpec(KeyTypeSpec spec){
			if(_specs.ContainsKey(spec.Name.Identifier))
				return false;
			_specs.Add(spec.Name.Identifier, spec);
			return true;
		}

		static KeyType(){
			AES.KeySizes<AesKey>(128,192,256).DefineSpec();
			HMAC_SHA1.KeySizes<HmacSha1Key>(256).WithDigestSizes(20).DefineSpec();
			DSA_PRIV.KeySizes<DsaPrivateKey>(1024).WithDigestSizes(48).IsAsymmetric().DefineSpec();
			DSA_PUB.KeySizes<DsaPublicKey>(1024).WithDigestSizes(48).IsAsymmetric().DefineSpec();
			RSA_PRIV.KeySizes<RsaPrivateKey>(2048, 1024, 4096).WithDigestSizes(256, 128, 512).IsAsymmetric().DefineSpec();
			RSA_PUB.KeySizes<RsaPublicKey>(2048, 1024, 4096 ).WithDigestSizes(256, 128, 512).IsAsymmetric().DefineSpec();
			//Unofficial
			AES_AEAD.KeySizes<Unofficial.AesAeadKey>(256,192,128).IsUnofficial().DefineSpec();
		}

		private static readonly IDictionary<string, KeyTypeSpec> _specs = new Dictionary<string, KeyTypeSpec>();

        /// <summary>
        /// Describes the sizes and algorithms.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keySizes">The key sizes.</param>
        /// <returns></returns>
        public KeyTypeSpec KeySizes<T>(params int[] keySizes) where T: Key
        {

            return new KeyTypeSpec
            {
                Name =  Identifier,
                Type = typeof(T),
                KeySizes = keySizes,
            };
        }

        /// <summary>
        /// Describes meta data about keytypes
        /// </summary>
        public class KeyTypeSpec
        {
			internal KeyTypeSpec(){
                DigestSizes = new int[]{0};
			    
			}

            
            /// <summary>
            /// Gets or sets the name.
            /// </summary>
            /// <value>The name.</value>
            public KeyType Name { get; internal set; }
          
            /// <summary>
            /// Gets or sets the type.
            /// </summary>
            /// <value>The type.</value>
            public Type Type{ get; internal set; }

            /// <summary>
            /// Gets or sets the key sizes.
            /// </summary>
            /// <value>The key sizes.</value>
            public int[] KeySizes{ get; internal set; }
            /// <summary>
            /// Gets or sets the digest sizes.
            /// </summary>
            /// <value>The digest sizes.</value>
            public int[] DigestSizes{ get; internal set; }
            /// <summary>
            /// Gets or sets a value indicating whether this <see cref="KeyTypeSpec"/> is unofficial.
            /// </summary>
            /// <value><c>true</c> if unofficial; otherwise, <c>false</c>.</value>
            public bool Unofficial{ get; internal set; }
            /// <summary>
            /// Gets or sets a value indicating whether this <see cref="KeyTypeSpec"/> is asymmetric.
            /// </summary>
            /// <value><c>true</c> if asymmetric; otherwise, <c>false</c>.</value>
            public bool Asymmetric{ get; internal set; }

            /// <summary>
            /// Describes the digest sizes.
            /// </summary>
            /// <param name="sigSizes">The sig sizes.</param>
            /// <returns></returns>
            public KeyTypeSpec WithDigestSizes(params int[] sigSizes)
            {
                DigestSizes = sigSizes;
                return this;
            }
            /// <summary>
            /// Specifies this  instance is unofficial.
            /// </summary>
            /// <returns></returns>
            public KeyTypeSpec IsUnofficial()
            {
                Unofficial = true;
                return this;
            }

            /// <summary>
            /// Specifies this instance is asymmetric.
            /// </summary>
            /// <returns></returns>
            public KeyTypeSpec IsAsymmetric()
            {
                Asymmetric = true;
                return this;
            }

            /// <summary>
            /// Defines the spec.
            /// </summary>
            /// <returns></returns>
			public KeyType DefineSpec(){
				if(KeyType.DefineSpec(this))
					return Name;
				return null;
			}
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="KeyType"/>.
        /// </summary>
        /// <param name="identifer">The identifer.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator KeyType(string identifer)
        {
			if(String.IsNullOrWhiteSpace(identifer))
			   return null;
            return new KeyType(identifer);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="KeyType"/> class.
        /// </summary>
        /// <param name="identifer">The identifer.</param>
        public KeyType(string identifer)
            : base(identifer)
        {


        }

        /// <summary>
        /// Returns or creates a keytype
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <returns></returns>
		public static KeyType Name(string identifier){
			return identifier;
		}

        private Type _type;
        private int[] _keySizeOptions;
        private bool? _unofficial;
        private bool? _asymmetric;
        /// <summary>
        /// Gets the key size options.
        /// </summary>
        /// <value>The key size options.</value>
        public int[] KeySizeOptions
        {
            get
{'fraction_non_alphanumeric': 0.12543997603534787, 'fraction_numerical': 0.013629895903542275, 'mean_word_length': 1.7556747833264548, 'pattern_counts': {'":': 0, '<': 179, '<?xml version=': 0, '>': 183, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5096127', 'n_tokens_mistral': 3960, 'n_tokens_neox': 3643, 'n_words': 981}