Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a nullreference in QRCodeGenerator #121

Merged
merged 3 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions KeeTrayTOTP.Tests/QRCodeGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace KeeTrayTOTP.Tests
{
[TestClass]
public class QRCodeGeneratorTests
{
[TestMethod]
public void QRCodeGenerator_ShouldGenerateCodeWithValidKeyUrl()
{
var code = "otpauth://totp/Sample%20Entry%20%232:?secret=JBSWY3DPEHPK3PXP&issuer=Sample%20Entry%20%232";
using (var generator = new QRCoder.QRCodeGenerator())
{
var act = generator.CreateQrCode(code);

act.Should().NotBeNull();
act.Version.Should().Be(8);
act.ModuleMatrix.Should().HaveCount(57);
}
}
}
}
43 changes: 42 additions & 1 deletion KeeTrayTOTP/Libraries/QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public QRCodeGenerator()
this.CreateAlignmentPatternTable();
}

public QRCodeData CreateQrCode(string plainText, ErrorCorrectionLevel eccLevel = QRCodeGenerator.ErrorCorrectionLevel.Q, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1)
public QRCodeData CreateQrCode(string plainText, ErrorCorrectionLevel eccLevel = ErrorCorrectionLevel.Q, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1)
{
EncodingMode encoding = GetEncodingFromPlaintext(plainText, forceUtf8);
var codedText = this.PlainTextToBinary(plainText, encoding, eciMode, utf8BOM, forceUtf8);
Expand Down Expand Up @@ -569,6 +569,47 @@ private static bool IsBlocked(Rectangle r1, List<Rectangle> blockedModules)

private static class MaskPattern
{
// NOTE: Methods are invoked via reflection (typeof(MaskPattern).GetMethods()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't do much with the qr code stuff. Do you know why they're using reflection for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not. The files were copied over from a nuget package (https://github.com/codebude/QRCoder) when the QR Dialog was added. When cleaning up I did not notice that reflection was used. The irony is that in the meantime that package moved away from reflection as well (https://github.com/codebude/QRCoder/blob/master/QRCoder/QRCodeGenerator.cs), so I could update and clean up as well.

I'm struggling a bit with how to handle dependencies in the plgx scenario. I'd rather add a reference to the package but I've not found a way to get this working.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a next step we could grab the latest version of the files from the QRCode repository. Will add that as an item on the board.

public static bool Pattern1(int x, int y)
{
return (x + y) % 2 == 0;
}

public static bool Pattern2(int x, int y)
{
return y % 2 == 0;
}

public static bool Pattern3(int x, int y)
{
return x % 3 == 0;
}

public static bool Pattern4(int x, int y)
{
return (x + y) % 3 == 0;
}

public static bool Pattern5(int x, int y)
{
return ((int)(Math.Floor(y / 2d) + Math.Floor(x / 3d)) % 2) == 0;
}

public static bool Pattern6(int x, int y)
{
return ((x * y) % 2) + ((x * y) % 3) == 0;
}

public static bool Pattern7(int x, int y)
{
return (((x * y) % 2) + ((x * y) % 3)) % 2 == 0;
}

public static bool Pattern8(int x, int y)
{
return (((x + y) % 2) + ((x * y) % 3)) % 2 == 0;
}

public static int Score(ref QRCodeData qrCode)
{
int score1 = 0,
Expand Down
2 changes: 0 additions & 2 deletions KeeTrayTOTP/ShowQR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ private void ShowQR_Load(object sender, EventArgs e)

private void GenerateQRCode()
{


var code = string.Format("otpauth://totp/{0}:{1}?secret={2}&issuer={0}", Uri.EscapeDataString(IssuerText.Text), Uri.EscapeDataString(UsernameText.Text), this.seed);

using (var qrGenerator = new QRCodeGenerator())
Expand Down