示例#1
0
        public virtual void TestOptions()
        {
            Configuration conf = new Configuration();

            conf.Set(KeyProvider.DefaultCipherName, "myCipher");
            conf.SetInt(KeyProvider.DefaultBitlengthName, 512);
            IDictionary <string, string> attributes = new Dictionary <string, string>();

            attributes["a"] = "A";
            KeyProvider.Options options = KeyProvider.Options(conf);
            Assert.Equal("myCipher", options.GetCipher());
            Assert.Equal(512, options.GetBitLength());
            options.SetCipher("yourCipher");
            options.SetDescription("description");
            options.SetAttributes(attributes);
            options.SetBitLength(128);
            Assert.Equal("yourCipher", options.GetCipher());
            Assert.Equal(128, options.GetBitLength());
            Assert.Equal("description", options.GetDescription());
            Assert.Equal(attributes, options.GetAttributes());
            options = KeyProvider.Options(new Configuration());
            Assert.Equal(KeyProvider.DefaultCipher, options.GetCipher());
            Assert.Equal(KeyProvider.DefaultBitlength, options.GetBitLength
                             ());
        }
示例#2
0
 /// <exception cref="System.IO.IOException"/>
 public override KeyProvider.KeyVersion CreateKey(string name, byte[] material, KeyProvider.Options
                                                  options)
 {
     lock (this)
     {
         Text nameT = new Text(name);
         if (credentials.GetSecretKey(nameT) != null)
         {
             throw new IOException("Key " + name + " already exists in " + this);
         }
         if (options.GetBitLength() != 8 * material.Length)
         {
             throw new IOException("Wrong key length. Required " + options.GetBitLength() + ", but got "
                                   + (8 * material.Length));
         }
         KeyProvider.Metadata meta = new KeyProvider.Metadata(options.GetCipher(), options
                                                              .GetBitLength(), options.GetDescription(), options.GetAttributes(), new DateTime
                                                                  (), 1);
         cache[name] = meta;
         string versionName = BuildVersionName(name, 0);
         credentials.AddSecretKey(nameT, meta.Serialize());
         credentials.AddSecretKey(new Text(versionName), material);
         return(new KeyProvider.KeyVersion(name, versionName, material));
     }
 }
 /// <exception cref="System.IO.IOException"/>
 public override KeyProvider.KeyVersion CreateKey(string name, byte[] material, KeyProvider.Options
                                                  options)
 {
     Preconditions.CheckArgument(name.Equals(StringUtils.ToLowerCase(name)), "Uppercase key names are unsupported: %s"
                                 , name);
     writeLock.Lock();
     try
     {
         try
         {
             if (keyStore.ContainsAlias(name) || cache.Contains(name))
             {
                 throw new IOException("Key " + name + " already exists in " + this);
             }
         }
         catch (KeyStoreException e)
         {
             throw new IOException("Problem looking up key " + name + " in " + this, e);
         }
         KeyProvider.Metadata meta = new KeyProvider.Metadata(options.GetCipher(), options
                                                              .GetBitLength(), options.GetDescription(), options.GetAttributes(), new DateTime
                                                                  (), 1);
         if (options.GetBitLength() != 8 * material.Length)
         {
             throw new IOException("Wrong key length. Required " + options.GetBitLength() + ", but got "
                                   + (8 * material.Length));
         }
         cache[name] = meta;
         string versionName = BuildVersionName(name, 0);
         return(InnerSetKeyVersion(name, versionName, material, meta.GetCipher()));
     }
     finally
     {
         writeLock.Unlock();
     }
 }
示例#4
0
 /// <summary>Create a new key generating the material for it.</summary>
 /// <remarks>
 /// Create a new key generating the material for it.
 /// The given key must not already exist.
 /// <p/>
 /// This implementation generates the key material and calls the
 /// <see cref="CreateKey(string, byte[], Options)"/>
 /// method.
 /// </remarks>
 /// <param name="name">the base name of the key</param>
 /// <param name="options">the options for the new key.</param>
 /// <returns>the version name of the first version of the key.</returns>
 /// <exception cref="System.IO.IOException"/>
 /// <exception cref="NoSuchAlgorithmException"/>
 public virtual KeyProvider.KeyVersion CreateKey(string name, KeyProvider.Options
                                                 options)
 {
     byte[] material = GenerateKey(options.GetBitLength(), options.GetCipher());
     return(CreateKey(name, material, options));
 }