Skip to content

Commit

Permalink
Fix a nullreference in QRCodeGenerator (#121)
Browse files Browse the repository at this point in the history
* Add reproduction
* Add back Pattern methods invoked via reflection
* Fix formatting
  • Loading branch information
robinvanpoppel committed Apr 15, 2020
1 parent 962e942 commit a983d9e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
23 changes: 23 additions & 0 deletions KeeTrayTOTP.Tests/QRCodeGeneratorTests.cs
@@ -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
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()))
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
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

0 comments on commit a983d9e

Please sign in to comment.