Commits

Devin Martin committed 45e73db

Store data as name value paris

Comments (0)

Files changed (7)

Constants.cs

-using System;
-using System.Collections.Generic;
-using System.Text;
-
-namespace KeeOtp
-{
-    internal class Constants
-    {
-        public const string OtpDictionaryKey = "OTPAUTH";
-    }
-}
     <Reference Include="System" />
     <Reference Include="System.Data" />
     <Reference Include="System.Drawing" />
+    <Reference Include="System.Web" />
     <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Constants.cs" />
+    <Compile Include="OtpType.cs" />
     <Compile Include="KeeOtpExt.cs" />
+    <Compile Include="OtpAuthData.cs" />
     <Compile Include="ShowOneTimePasswords.cs">
       <SubType>Form</SubType>
     </Compile>
             this.otpSeperatorToolStripItem = new ToolStripSeparator();
             host.MainWindow.EntryContextMenu.Items.Add(this.otpSeperatorToolStripItem);
 
-            this.otpDialogToolStripItem = host.MainWindow.EntryContextMenu.Items.Add("One Time Passwords");
+            this.otpDialogToolStripItem = host.MainWindow.EntryContextMenu.Items.Add("One Time Password");
             this.otpDialogToolStripItem.Click += new EventHandler(otpDialogToolStripItem_Click);
             return true; // Initialization successful
         }
+using System;
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.Text;
+using System.Web;
+using OtpSharp;
+
+namespace KeeOtp
+{
+    /// <summary>
+    /// Class that serializes and deserializes data into the Strings for the entry
+    /// </summary>
+    class OtpAuthData
+    {
+        public const string StringDictionaryKey = "otp";
+
+        const string keyParameter = "key";
+        const string typeParameter = "type";
+        const string stepParameter = "step";
+        const string sizeParameter = "size";
+        const string counterParameter = "counter";
+
+        public byte[] Key { get; set; }
+        public OtpType Type { get; set; }
+
+        public int Step { get; set; }
+        public int Size { get; set; }
+
+        public int Counter { get; set; }
+
+
+        public static OtpAuthData FromString(string data)
+        {
+            var parameters = HttpUtility.ParseQueryString(data);
+
+            if (parameters[keyParameter] == null)
+                throw new ArgumentException("Must have a key in the data");
+
+            var otpData = new OtpAuthData();
+
+            otpData.Key = Base32.Decode(parameters[keyParameter]);
+            if (parameters[typeParameter] != null)
+                otpData.Type = (OtpType)Enum.Parse(typeof(OtpType), parameters[typeParameter]);
+
+            if (otpData.Type == OtpType.Totp)
+                otpData.Step = GetIntOrDefault(parameters, stepParameter, 30);
+            else if (otpData.Type == OtpType.Hotp)
+                otpData.Counter = GetIntOrDefault(parameters, counterParameter, 0);
+
+            otpData.Size = GetIntOrDefault(parameters, sizeParameter, 6);
+
+            return otpData;
+        }
+
+        private static int GetIntOrDefault(NameValueCollection parameters, string parameterKey, int defaultValue)
+        {
+            if (parameters[stepParameter] != null)
+            {
+                int step;
+                if (int.TryParse(parameters[stepParameter], out step))
+                    return step;
+                else
+                    return defaultValue;
+            }
+            else
+                return defaultValue;
+        }
+
+        public string EncodedString
+        {
+            get
+            {
+                NameValueCollection collection = new NameValueCollection();
+                collection.Add(keyParameter, Base32.Encode(this.Key));
+
+                if (this.Type != KeeOtp.OtpType.Totp)
+                    collection.Add(typeParameter, this.Type.ToString());
+
+                if (this.Type == KeeOtp.OtpType.Hotp)
+                    collection.Add(counterParameter, this.Counter.ToString());
+
+                else if (this.Type == KeeOtp.OtpType.Totp)
+                {
+                    if (this.Step != 30)
+                        collection.Add(stepParameter, this.Step.ToString());
+                }
+
+                if (this.Size != 6)
+                    collection.Add(sizeParameter, this.Size.ToString());
+
+                string data = string.Empty;
+                foreach (var key in collection.AllKeys)
+                {
+                    data += string.Format("{0}={1}&", key, HttpUtility.UrlEncode(collection[key]));
+                }
+
+                return data.TrimEnd('&');
+            }
+        }
+    }
+}
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace KeeOtp
+{
+    enum OtpType
+    {
+        Totp,
+        Hotp
+    }
+}

ShowOneTimePasswords.Designer.cs

             this.labelOtp.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
             this.labelOtp.Location = new System.Drawing.Point(24, 93);
             this.labelOtp.Name = "labelOtp";
-            this.labelOtp.Size = new System.Drawing.Size(248, 73);
+            this.labelOtp.Size = new System.Drawing.Size(224, 73);
             this.labelOtp.TabIndex = 0;
-            this.labelOtp.Text = "012345";
+            this.labelOtp.Text = "xxxxxx";
             // 
             // labelRemaining
             // 
             this.labelRemaining.AutoSize = true;
             this.labelRemaining.Location = new System.Drawing.Point(145, 166);
             this.labelRemaining.Name = "labelRemaining";
-            this.labelRemaining.Size = new System.Drawing.Size(35, 13);
+            this.labelRemaining.Size = new System.Drawing.Size(12, 13);
             this.labelRemaining.TabIndex = 1;
-            this.labelRemaining.Text = "label2";
+            this.labelRemaining.Text = "x";
             // 
             // labelRemainingLabel
             // 

ShowOneTimePasswords.cs

 using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Data;
-using System.Drawing;
-using System.Text;
 using System.Windows.Forms;
-using KeePassLib.Security;
 using OtpSharp;
 
 namespace KeeOtp
 
         private void FormWasShown()
         {
-            if (!entry.Strings.Exists(Constants.OtpDictionaryKey))
+            if (!entry.Strings.Exists(OtpAuthData.StringDictionaryKey))
             {
                 this.Close();
                 MessageBox.Show("Please add a one time password field");
             {
                 try
                 {
-                    var otpEncodedKey = entry.Strings.Get(Constants.OtpDictionaryKey);
-                    var key = Base32.Decode(otpEncodedKey.ReadString());
-                    this.totp = new Totp(key);
+                    var otpAuthData = OtpAuthData.FromString(entry.Strings.Get(OtpAuthData.StringDictionaryKey).ReadString());
+
+                    this.totp = new Totp(otpAuthData.Key, step:otpAuthData.Step);
                     this.timerUpdateTotp.Enabled = true;
                 }
                 catch (Exception e)