示例#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
                             ());
        }
 public static void Setup()
 {
     conf    = new Configuration();
     kp      = new UserProvider.Factory().CreateProvider(new URI("user:///"), conf);
     kpExt   = KeyProviderCryptoExtension.CreateKeyProviderCryptoExtension(kp);
     options = new KeyProvider.Options(conf);
     options.SetCipher(Cipher);
     options.SetBitLength(128);
     encryptionKey = kp.CreateKey(EncryptionKeyName, SecureRandom.GetSeed(16), options
                                  );
 }
示例#3
0
 public virtual void TestMaterialGeneration()
 {
     TestKeyProvider.MyKeyProvider kp = new TestKeyProvider.MyKeyProvider(new Configuration
                                                                              ());
     KeyProvider.Options options = new KeyProvider.Options(new Configuration());
     options.SetCipher(Cipher);
     options.SetBitLength(128);
     kp.CreateKey("hello", options);
     Assert.Equal(128, kp.size);
     Assert.Equal(Cipher, kp.algorithm);
     NUnit.Framework.Assert.IsNotNull(kp.material);
     kp = new TestKeyProvider.MyKeyProvider(new Configuration());
     kp.RollNewVersion("hello");
     Assert.Equal(128, kp.size);
     Assert.Equal(Cipher, kp.algorithm);
     NUnit.Framework.Assert.IsNotNull(kp.material);
 }
示例#4
0
        /// <summary>
        /// Parse the command line arguments and initialize the data
        /// <pre>
        /// % hadoop key create keyName [-size size] [-cipher algorithm]
        /// [-provider providerPath]
        /// % hadoop key roll keyName [-provider providerPath]
        /// % hadoop key list [-provider providerPath]
        /// % hadoop key delete keyName [-provider providerPath] [-i]
        /// </pre>
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        /// <returns>0 on success, 1 on failure.</returns>
        /// <exception cref="System.IO.IOException"/>
        private int Init(string[] args)
        {
            KeyProvider.Options          options    = KeyProvider.Options(GetConf());
            IDictionary <string, string> attributes = new Dictionary <string, string>();

            for (int i = 0; i < args.Length; i++)
            {
                // parse command line
                bool moreTokens = (i < args.Length - 1);
                if (args[i].Equals("create"))
                {
                    string keyName = "-help";
                    if (moreTokens)
                    {
                        keyName = args[++i];
                    }
                    command = new KeyShell.CreateCommand(this, keyName, options);
                    if ("-help".Equals(keyName))
                    {
                        PrintKeyShellUsage();
                        return(1);
                    }
                }
                else
                {
                    if (args[i].Equals("delete"))
                    {
                        string keyName = "-help";
                        if (moreTokens)
                        {
                            keyName = args[++i];
                        }
                        command = new KeyShell.DeleteCommand(this, keyName);
                        if ("-help".Equals(keyName))
                        {
                            PrintKeyShellUsage();
                            return(1);
                        }
                    }
                    else
                    {
                        if (args[i].Equals("roll"))
                        {
                            string keyName = "-help";
                            if (moreTokens)
                            {
                                keyName = args[++i];
                            }
                            command = new KeyShell.RollCommand(this, keyName);
                            if ("-help".Equals(keyName))
                            {
                                PrintKeyShellUsage();
                                return(1);
                            }
                        }
                        else
                        {
                            if ("list".Equals(args[i]))
                            {
                                command = new KeyShell.ListCommand(this);
                            }
                            else
                            {
                                if ("-size".Equals(args[i]) && moreTokens)
                                {
                                    options.SetBitLength(System.Convert.ToInt32(args[++i]));
                                }
                                else
                                {
                                    if ("-cipher".Equals(args[i]) && moreTokens)
                                    {
                                        options.SetCipher(args[++i]);
                                    }
                                    else
                                    {
                                        if ("-description".Equals(args[i]) && moreTokens)
                                        {
                                            options.SetDescription(args[++i]);
                                        }
                                        else
                                        {
                                            if ("-attr".Equals(args[i]) && moreTokens)
                                            {
                                                string[] attrval = args[++i].Split("=", 2);
                                                string   attr    = attrval[0].Trim();
                                                string   val     = attrval[1].Trim();
                                                if (attr.IsEmpty() || val.IsEmpty())
                                                {
                                                    @out.WriteLine("\nAttributes must be in attribute=value form, " + "or quoted\nlike \"attribute = value\"\n"
                                                                   );
                                                    PrintKeyShellUsage();
                                                    return(1);
                                                }
                                                if (attributes.Contains(attr))
                                                {
                                                    @out.WriteLine("\nEach attribute must correspond to only one value:\n" + "atttribute \""
                                                                   + attr + "\" was repeated\n");
                                                    PrintKeyShellUsage();
                                                    return(1);
                                                }
                                                attributes[attr] = val;
                                            }
                                            else
                                            {
                                                if ("-provider".Equals(args[i]) && moreTokens)
                                                {
                                                    userSuppliedProvider = true;
                                                    GetConf().Set(KeyProviderFactory.KeyProviderPath, args[++i]);
                                                }
                                                else
                                                {
                                                    if ("-metadata".Equals(args[i]))
                                                    {
                                                        GetConf().SetBoolean(ListMetadata, true);
                                                    }
                                                    else
                                                    {
                                                        if ("-f".Equals(args[i]) || ("-force".Equals(args[i])))
                                                        {
                                                            interactive = false;
                                                        }
                                                        else
                                                        {
                                                            if ("-help".Equals(args[i]))
                                                            {
                                                                PrintKeyShellUsage();
                                                                return(1);
                                                            }
                                                            else
                                                            {
                                                                PrintKeyShellUsage();
                                                                ToolRunner.PrintGenericCommandUsage(System.Console.Error);
                                                                return(1);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (command == null)
            {
                PrintKeyShellUsage();
                return(1);
            }
            if (!attributes.IsEmpty())
            {
                options.SetAttributes(attributes);
            }
            return(0);
        }