Skip to content

Commit

Permalink
Resolves #28
Browse files Browse the repository at this point in the history
  • Loading branch information
tiuub committed Jun 22, 2021
1 parent 414dc67 commit a507f6b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
11 changes: 6 additions & 5 deletions KeeOtp2/Forms/OtpInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private void checkBoxCustomSettings_CheckedChanged(object sender, EventArgs e)

private void linkLabelMigrate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (MessageBox.Show(KeeOtp2Statics.MessageBoxMigrationReplacePlaceholder, KeeOtp2Statics.Migration, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
if (MessageBox.Show(String.Format(KeeOtp2Statics.MessageBoxMigrationReplacePlaceholder, KeeOtp2Ext.KeeOtp1PlaceHolder, string.Format("{0}/{1}", KeeOtp2Ext.BuiltInTotpPlaceHolder, KeeOtp2Ext.BuiltInHotpPlaceHolder)), KeeOtp2Statics.Migration, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
if (data.Type == OtpType.Totp)
OtpAuthUtils.replacePlaceholder(this.entry, KeeOtp2Ext.KeeOtp1PlaceHolder, KeeOtp2Ext.BuiltInTotpPlaceHolder);
Expand All @@ -247,6 +247,7 @@ private void comboBoxLength_DropDownClosed(object sender, EventArgs e)

private void comboBoxType_SelectionChangeCommitted(object sender, EventArgs e)
{
bool timerEnabled = timerUpdateTotp.Enabled;
timerUpdateTotp.Stop();
timerUpdateTotp.Dispose();
OtpType type = comboBoxTypeIndexValue[comboBoxType.SelectedIndex];
Expand Down Expand Up @@ -280,7 +281,7 @@ private void comboBoxType_SelectionChangeCommitted(object sender, EventArgs e)
}

loadData();
timerUpdateTotp.Start();
timerUpdateTotp.Enabled = timerEnabled;
}

private void timerUpdateTotp_Tick(object sender, EventArgs e)
Expand All @@ -293,7 +294,7 @@ private void timerUpdateTotp_Tick(object sender, EventArgs e)
OtpBase otp = OtpAuthUtils.getOtp(data);
if (data.Type == OtpType.Totp || data.Type == OtpType.Steam)
{
groupBoxKey.Text = String.Format(KeeOtp2Statics.OtpInformationKeyUriTotpPreview, otp.getTotpString(OtpTime.getTime()), otp.getRemainingSeconds());
groupBoxKey.Text = String.Format(KeeOtp2Statics.OtpInformationKeyUriTotpPreview, otp.getTotpString(OtpTime.getTime()), otp.getRemainingSeconds(OtpTime.getTime()));
}
else if (data.Type == OtpType.Hotp)
{
Expand Down Expand Up @@ -502,11 +503,11 @@ private void loadData()
linkLabelMigrate.Enabled = false;
}

SetCustomSettingsState();

timerUpdateTotp.Enabled = timerEnabled;
comboBoxLength.Enabled = comboBoxLengthEnabled;
comboBoxType.Enabled = comboBoxTypeEnabled;

SetCustomSettingsState();
}


Expand Down
2 changes: 1 addition & 1 deletion KeeOtp2/Forms/ShowOneTimePasswords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private void UpdateDisplay()
{
this.linkLabelIncorrectNext.Text = KeeOtp2Statics.ShowOtpIncorrect;
this.labelOtp.Text = insertSpaceInMiddle(otp.getTotpString(OtpTime.getTime()));
this.groupboxTotp.Text = String.Format(KeeOtp2Statics.ShowOtpNextRemaining, otp.getRemainingSeconds().ToString().PadLeft(2, '0'), insertSpaceInMiddle(otp.getTotpString(OtpTime.getTime().AddSeconds(data.Period))));
this.groupboxTotp.Text = String.Format(KeeOtp2Statics.ShowOtpNextRemaining, otp.getRemainingSeconds(OtpTime.getTime()).ToString().PadLeft(2, '0'), insertSpaceInMiddle(otp.getTotpString(OtpTime.getTime().AddSeconds(data.Period))));
}
else if (data.Type == OtpType.Hotp)
{
Expand Down
35 changes: 20 additions & 15 deletions KeeOtp2/OtpAuthData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace KeeOtp2
/// </summary>
public class OtpAuthData : ICloneable
{
public ProtectedString Key { get; set; }
public ProtectedBinary Key { get; set; }
public OtpType Type { get; set; }
public OtpSecretEncoding Encoding { get; set; }
public OtpHashMode Algorithm { get; set; }
Expand All @@ -26,7 +26,7 @@ public class OtpAuthData : ICloneable

public OtpAuthData()
{
this.Key = new ProtectedString(true, "");
this.Key = new ProtectedBinary(true, StrUtil.Utf8.GetBytes(""));
this.Type = OtpType.Totp;
this.Encoding = OtpSecretEncoding.Base32;
this.Algorithm = OtpHashMode.Sha1;
Expand All @@ -43,16 +43,16 @@ public OtpAuthData()

public string GetPlainSecret()
{
if (!string.IsNullOrEmpty(this.Key.ReadString()))
if (this.Key.ReadData().Length > 0)
{
if (this.Encoding == OtpSecretEncoding.Base32)
return Base32Encoding.ToString(this.Key.ReadUtf8());
return Base32Encoding.ToString(this.Key.ReadData());
else if (this.Encoding == OtpSecretEncoding.Base64)
return Convert.ToBase64String(this.Key.ReadUtf8());
return Convert.ToBase64String(this.Key.ReadData());
else if (this.Encoding == OtpSecretEncoding.Hex)
return MemUtil.ByteArrayToHexString(this.Key.ReadUtf8());
return MemUtil.ByteArrayToHexString(this.Key.ReadData());
else if (this.Encoding == OtpSecretEncoding.UTF8)
return StrUtil.Utf8.GetString(this.Key.ReadUtf8());
return StrUtil.Utf8.GetString(this.Key.ReadData());
else
return null;
}
Expand All @@ -61,14 +61,19 @@ public string GetPlainSecret()

public void SetPlainSecret(string secret)
{
if (this.Encoding == OtpSecretEncoding.Base32)
this.Key = new ProtectedString(true, MemUtil.ParseBase32(secret));
else if (this.Encoding == OtpSecretEncoding.Base64)
this.Key = new ProtectedString(true, Convert.FromBase64String(secret));
else if (this.Encoding == OtpSecretEncoding.Hex)
this.Key = new ProtectedString(true, MemUtil.HexStringToByteArray(secret));
else if (this.Encoding == OtpSecretEncoding.UTF8)
this.Key = new ProtectedString(true, StrUtil.Utf8.GetBytes(secret));
if (secret.Length > 0)
{
if (this.Encoding == OtpSecretEncoding.Base32)
this.Key = new ProtectedBinary(true, MemUtil.ParseBase32(secret));
else if (this.Encoding == OtpSecretEncoding.Base64)
this.Key = new ProtectedBinary(true, Convert.FromBase64String(secret));
else if (this.Encoding == OtpSecretEncoding.Hex)
this.Key = new ProtectedBinary(true, MemUtil.HexStringToByteArray(secret));
else if (this.Encoding == OtpSecretEncoding.UTF8)
this.Key = new ProtectedBinary(true, StrUtil.Utf8.GetBytes(secret));
}
else
this.Key = new ProtectedBinary(true, StrUtil.Utf8.GetBytes(""));
}

public bool isForcedKeeOtp1Mode()
Expand Down
24 changes: 15 additions & 9 deletions KeeOtp2/OtpBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public OtpBase(OtpAuthData data)
public abstract string getTotpString(DateTime timestamp);
public abstract string getHotpString(long counter);
public abstract int getRemainingSeconds();
public abstract int getRemainingSeconds(DateTime timestamp);
}

public class OtpTotp : OtpBase
Expand All @@ -29,7 +30,7 @@ public class OtpTotp : OtpBase

public OtpTotp(OtpAuthData data) : base(data)
{
this.totp = new Totp(data.Key.ReadUtf8(), data.Period, data.Algorithm, data.Digits, null);
this.totp = new Totp(data.Key.ReadData(), data.Period, data.Algorithm, data.Digits, null);
}

public override string getTotpString()
Expand All @@ -44,7 +45,12 @@ public override string getTotpString(DateTime timestamp)

public override int getRemainingSeconds()
{
return totp.RemainingSeconds();
return getRemainingSeconds(DateTime.Now);
}

public override int getRemainingSeconds(DateTime timestamp)
{
return totp.RemainingSeconds(timestamp);
}

public override string getHotpString(long counter)
Expand All @@ -59,7 +65,7 @@ public class OtpHotp : OtpBase

public OtpHotp(OtpAuthData data) : base(data)
{
this.hotp = new Hotp(data.Key.ReadUtf8(), data.Algorithm, data.Digits);
this.hotp = new Hotp(data.Key.ReadData(), data.Algorithm, data.Digits);
}

public override string getHotpString(long counter)
Expand All @@ -81,6 +87,11 @@ public override int getRemainingSeconds()
{
throw new NotImplementedException();
}

public override int getRemainingSeconds(DateTime timestamp)
{
throw new NotImplementedException();
}
}

public class OtpSteam : OtpTotp
Expand All @@ -106,7 +117,7 @@ public override string getTotpString(DateTime timestamp)
if (BitConverter.IsLittleEndian)
Array.Reverse(codeInterval);

InMemoryKey key = new InMemoryKey(data.Key.ReadUtf8());
InMemoryKey key = new InMemoryKey(data.Key.ReadData());
byte[] hash = key.ComputeHmac(OtpHashMode.Sha1, codeInterval);

int start = hash[hash.Length - 1] & 0xf;
Expand All @@ -127,10 +138,5 @@ public override string getTotpString(DateTime timestamp)

return sb.ToString();
}

public override int getRemainingSeconds()
{
return base.getRemainingSeconds();
}
}
}
4 changes: 2 additions & 2 deletions KeeOtp2/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.5.6.0")]
[assembly: AssemblyFileVersion("1.5.6.0")]
[assembly: AssemblyVersion("1.5.6.1")]
[assembly: AssemblyFileVersion("1.5.6.1")]
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
:
KeeOtp2:1.5.5.0
KeeOtp2:1.5.6.1
:

0 comments on commit a507f6b

Please sign in to comment.