Skip to content

Commit

Permalink
Improve QR code handling
Browse files Browse the repository at this point in the history
Switch to ZXing.net to read / write QR images
Support data-uri containing a string represenation of QR images (data:image/...)
  • Loading branch information
Rookiestyle committed Nov 1, 2020
1 parent bcb2f72 commit d176066
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 5,152 deletions.
16 changes: 9 additions & 7 deletions src/KeePassOTP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="QRCode\QRCode.cs" />
<Compile Include="QRCode\QRCodeData.cs" />
<Compile Include="QRCode\QRCodeDecoder.cs" />
<Compile Include="QRCode\QRCodeGenerator.cs" />
<Compile Include="Util.cs" />
<Compile Include="Util.cs" />
<Compile Include="KeePassOTP.cs" />
<Compile Include="KeePassOTPColumnProvider.cs" />
<Compile Include="KeePassOTPExt.cs" />
Expand Down Expand Up @@ -129,15 +125,17 @@
<Project>{10938016-dee2-4a25-9a5a-8fd3444379ca}</Project>
<Name>KeePass</Name>
<!--
We do use the nuget package protobuf-net
We do use the nuget packages protobuf-net and ZXing.net
Unfortunately, the dll files created during compile time won't be added to the plgx file
We need to use plgxtool to add them
plgxtools does this using the PlgxReference tag which needs to be inside a ProjectReference
Using the reference to KeePass is a workaround and saves us from doing ExcludeFromPlgx for KeePass
-->
<PlgxReference>bin\Release\protobuf-net.dll</PlgxReference>
</ProjectReference>
<PlgxReference>bin\Release\zxing.dll</PlgxReference>
<PlgxReference>bin\Release\zxing.presentation.dll</PlgxReference>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="Resources\qr-code.png" />
Expand All @@ -151,6 +149,10 @@
<Version>2.4.6</Version>
<ExcludeFromPlgx />
</PackageReference>
<PackageReference Include="ZXing.Net">
<Version>0.16.6</Version>
<ExcludeFromPlgx />
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="BeforeBuild" Condition=" '$(Configuration)' == 'Debug' ">
Expand Down
12 changes: 5 additions & 7 deletions src/KeePassOTPExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ private void OnOTPQRCode(object sender, EventArgs e)
if (!otp.Valid) return;
try
{
byte[] bOTP = otp.OTPAuthString.ReadUtf8();
QRCoder.QRCodeData qrd = QRCoder.QRCodeGenerator.GenerateQrCode(bOTP, QRCoder.QRCodeGenerator.ECCLevel.Q);
MemUtil.ZeroByteArray(bOTP);
QRCoder.QRCode qrc = new QRCoder.QRCode(qrd);
Bitmap bmp = qrc.GetGraphic(8);
ZXing.BarcodeWriter zBW = new ZXing.BarcodeWriter();
zBW.Options.Height = 320;
zBW.Options.Width = 320;
zBW.Format = ZXing.BarcodeFormat.QR_CODE;
Bitmap bmp = zBW.Write(otp.OTPAuthString.ReadString());
QRForm f = new QRForm();
f.FormBorderStyle = FormBorderStyle.FixedDialog;
f.StartPosition = FormStartPosition.CenterParent;
Expand Down Expand Up @@ -281,8 +281,6 @@ private void OnOTPQRCode(object sender, EventArgs e)
f.ShowDialog(KeePass.UI.GlobalWindowManager.TopWindow);
pb.Image.Dispose();
f.Dispose();
qrc.Dispose();
qrd.Dispose();
}
catch { }
}
Expand Down
19 changes: 13 additions & 6 deletions src/KeePassOTPSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ private bool SettingsChanged()
private void UpdatePreview()
{
if (m_NoUpdate) return;
if (StrUtil.IsDataUri(tbOTPSeed.Text))
{
try
{
OTP.OTPAuthString = ParseFromImageByteArray(StrUtil.DataUriToData(tbOTPSeed.Text));
InitSettings(true);
}
catch { tbOTPSeed.Text = string.Empty; }
}
if (tbOTPSeed.Text.ToLowerInvariant().StartsWith("otpauth://"))
{
OTP.OTPAuthString = new ProtectedString(true, tbOTPSeed.Text);
Expand Down Expand Up @@ -377,12 +386,10 @@ private ProtectedString ParseFromImageByteArray(byte[] buffer)
private ProtectedString ParseFromImage(System.Drawing.Bitmap bitmap)
{
if (bitmap == null) return ProtectedString.EmptyEx;
QRDecoder.QRDecoder decoder = new QRDecoder.QRDecoder();
byte[][] DataByteArray = decoder.ImageDecoder(bitmap);
if ((DataByteArray == null) || (DataByteArray.Length < 1)) return ProtectedString.EmptyEx;
ProtectedString psResult = new ProtectedString(true, DataByteArray[0]);
MemUtil.ZeroByteArray(DataByteArray[0]);
return psResult;
ZXing.BarcodeReader r = new ZXing.BarcodeReader();
ZXing.Result result = r.Decode(bitmap);
if (result != null) return new ProtectedString(true, result.Text);
return ProtectedString.EmptyEx;
}

private void pbQR_DragEnter(object sender, DragEventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,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("0.18.2")]
[assembly: AssemblyFileVersion("0.18.2")]
[assembly: AssemblyVersion("0.19")]
[assembly: AssemblyFileVersion("0.19")]
224 changes: 0 additions & 224 deletions src/QRCode/QRCode.cs

This file was deleted.

Loading

0 comments on commit d176066

Please sign in to comment.