상속: Keyczar.Util.StringType
예제 #1
0
        /// <summary>
        /// Adds the key.
        /// </summary>
        /// <param name="status">The status.</param>
        /// <param name="keySize">Size of the key.</param>
        /// <param name="options">The options. dictionary or annoymous type of properties to set</param>
        /// <returns></returns>
        public int AddKey(KeyStatus status, int keySize = 0, object options = null)
        {
            Key  key;
            bool loop;

            do
            {
                loop = false;
                key  = Key.Generate(_metadata.KeyType, keySize);
                if (options != null)
                {
                    var dict = options as IDictionary <string, object>;
                    if (dict == null)
                    {
                        Utility.CopyProperties(options, key);
                    }
                    else
                    {
                        Utility.CopyProperties(dict, key);
                    }
                }
                foreach (var existingkey in _keys)
                {
                    var newhash   = Util.Utility.ToInt32(key.GetKeyHash());
                    var existhash = Utility.ToInt32(existingkey.Value.GetKeyHash());
                    if (newhash == existhash)
                    {
                        loop = true;
                        break;
                    }
                }
            } while (loop);
            return(AddKey(status, key));
        }
예제 #2
0
        /// <summary>
        /// Adds the key.
        /// </summary>
        /// <param name="status">The status.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public int AddKey(KeyStatus status, Key key, string comment = null)
        {
            if (key.KeyType.Kind != Metadata.Kind && Metadata.Kind != null)
            {
                throw new InvalidKeyTypeException($"Keyset only supports {Metadata.Kind} keys");
            }

#pragma warning disable 618
            if (Metadata.KeyType != null)
            {
                //Once We add a key our new format shouldn't track the old one anymore
                Metadata.KeyType = null;
            }
#pragma warning restore 618

            int lastVersion = 0;
            foreach (var version in _metadata.Versions)
            {
                if (status == KeyStatus.Primary && version.Status == KeyStatus.Primary)
                {
                    version.Status = KeyStatus.Active;
                }
                lastVersion = Math.Max(lastVersion, version.VersionNumber);
            }

            _metadata.Versions.Add(new KeyVersion(status, ++lastVersion, key, comment));
            _keys.Add(lastVersion, key);
            onlyMetaChanged = false;
            return(lastVersion);
        }
예제 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="KeyVersion"/> class.
 /// </summary>
 public KeyVersion(KeyStatus status, int versionNumber, Key key, string comment = null)
 {
     Status        = status;
     VersionNumber = versionNumber;
     KeyType       = key.KeyType;
     KeyId         = key.GetKeyHash();
     Exportable    = false;
     Comment       = comment;
 }
예제 #4
0
        /// <summary>
        /// Adds the key.
        /// </summary>
        /// <param name="status">The status.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public int AddKey(KeyStatus status, Key key)
        {
            int lastVersion = 0;

            foreach (var version in _metadata.Versions)
            {
                if (status == KeyStatus.Primary && version.Status == KeyStatus.Primary)
                {
                    version.Status = KeyStatus.Active;
                }
                lastVersion = Math.Max(lastVersion, version.VersionNumber);
            }
            _metadata.Versions.Add(new KeyVersion()
            {
                Status = status, VersionNumber = ++lastVersion
            });

            _keys.Add(lastVersion, key);
            onlyMetaChanged = false;
            return(lastVersion);
        }
예제 #5
0
        /// <summary>
        /// Adds the key.
        /// </summary>
        /// <param name="status">The status.</param>
        /// <param name="keySize">Size of the key.</param>
        /// <param name="type">The Key type.</param>
        /// <param name="options">The options. dictionary or annoymous type of properties to set</param>
        /// <param name="comment"></param>
        /// <returns></returns>
        public int AddKey(KeyStatus status, int keySize = 0, KeyType type = null, object options = null, string comment = null)
        {
            if (type != null && Metadata.Kind != null && type.Kind != Metadata.Kind)
            {
                throw new InvalidKeyTypeException($"Keyset only supports {Metadata.Kind} keys");
            }

            Key  key;
            bool loop;

            do
            {
                loop = false;
                key  = Key.Generate(type ?? _metadata.DefaultKeyType, keySize);
                if (options != null)
                {
                    var dict = options as IDictionary <string, object>;
                    if (dict == null)
                    {
                        Utility.CopyProperties(options, key);
                    }

                    else
                    {
                        Utility.CopyProperties(dict, key);
                    }
                }
                foreach (var existingkey in _keys)
                {
                    var newhash   = Util.Utility.ToInt32(key.GetKeyHash());
                    var existhash = Utility.ToInt32(existingkey.Value.GetKeyHash());
                    if (newhash == existhash)
                    {
                        loop = true;
                        break;
                    }
                }
            } while (loop);
            return(AddKey(status, key, comment));
        }
예제 #6
0
        /// <summary>
        /// Adds the key.
        /// </summary>
        /// <param name="status">The status.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public int AddKey(KeyStatus status, Key key)
        {
            int lastVersion = 0;
            foreach (var version in _metadata.Versions)
            {
                if (status == KeyStatus.Primary && version.Status == KeyStatus.Primary)
                    version.Status = KeyStatus.Active;
                lastVersion = Math.Max(lastVersion, version.VersionNumber);
            }
            _metadata.Versions.Add(new KeyVersion() {Status = status, VersionNumber = ++lastVersion});

            _keys.Add(lastVersion, key);
            onlyMetaChanged = false;
            return lastVersion;
        }
예제 #7
0
 /// <summary>
 /// Adds the key.
 /// </summary>
 /// <param name="status">The status.</param>
 /// <param name="keySize">Size of the key.</param>
 /// <param name="options">The options. dictionary or annoymous type of properties to set</param>
 /// <returns></returns>
 public int AddKey(KeyStatus status, int keySize = 0, object options = null)
 {
     Key key;
     bool loop;
     do
     {
         loop = false;
         key = Key.Generate(_metadata.KeyType, keySize);
         if (options != null)
         {
             var dict = options as IDictionary<string, object>;
             if (dict == null)
                 Utility.CopyProperties(options, key);
             else
                 Utility.CopyProperties(dict, key);
         }
         foreach (var existingkey in _keys)
         {
             var newhash = Util.Utility.ToInt32(key.GetKeyHash());
             var existhash = Utility.ToInt32(existingkey.Value.GetKeyHash());
             if (newhash == existhash)
             {
                 loop = true;
                 break;
             }
         }
     } while (loop);
     return AddKey(status, key);
 }