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 ()); }
/// <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(); } }
/// <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)); } }