Skip to content

Commit

Permalink
Merge branch 'master' into wpa2
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 authored Apr 13, 2024
2 parents f74e547 + 56ace43 commit f0a7865
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/wf-build-release-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ jobs:
needs: test
runs-on: windows-2019
env:
GH_PKG_SEC: ${{ secrets.GH_PKG_REPO }}
GH_PKG_SEC: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wf-build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ jobs:
needs: test
runs-on: windows-2019
env:
GH_PKG_SEC: ${{ secrets.GH_PKG_REPO }}
GH_PKG_SEC: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
Expand Down
4 changes: 2 additions & 2 deletions QRCoder/ASCIIQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString
var lineBuilder = new StringBuilder();
for (var x = 0; x < QrCodeData.ModuleMatrix.Count - quietZonesModifier; x++)
{
var module = QrCodeData.ModuleMatrix[x + quietZonesOffset][((y + verticalNumberOfRepeats) / verticalNumberOfRepeats - 1)+quietZonesOffset];
var module = QrCodeData.ModuleMatrix[((y + verticalNumberOfRepeats) / verticalNumberOfRepeats - 1)+quietZonesOffset][x + quietZonesOffset];
for (var i = 0; i < repeatPerModule; i++)
{
lineBuilder.Append(module ? darkColorString : whiteSpaceString);
Expand All @@ -75,4 +75,4 @@ public static string GetQRCode(string plainText, int pixelsPerModule, string dar
return qrCode.GetGraphic(pixelsPerModule, darkColorString, whiteSpaceString, drawQuietZones, endOfLine);
}
}
}
}
17 changes: 11 additions & 6 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,7 @@ private void ProcessCommonFields(StringBuilder sb)
}
string strippedSecret = Secret.Replace(" ", "");
string escapedIssuer = null;
string escapedLabel = null;
string label = null;

if (!String40Methods.IsNullOrWhiteSpace(Issuer))
Expand All @@ -2024,18 +2025,22 @@ private void ProcessCommonFields(StringBuilder sb)
escapedIssuer = Uri.EscapeDataString(Issuer);
}

if (!String40Methods.IsNullOrWhiteSpace(Label) && Label.Contains(":"))
if (!String40Methods.IsNullOrWhiteSpace(Label))
{
throw new Exception("Label must not have a ':'");
if (Label.Contains(":"))
{
throw new Exception("Label must not have a ':'");
}
escapedLabel = Uri.EscapeDataString(Label);
}

if (Label != null && Issuer != null)
if (escapedLabel != null && escapedIssuer != null)
{
label = Issuer + ":" + Label;
label = escapedIssuer + ":" + escapedLabel;
}
else if (Issuer != null)
else if (escapedIssuer != null)
{
label = Issuer;
label = escapedIssuer;
}

if (label != null)
Expand Down
9 changes: 1 addition & 8 deletions QRCoder/PostscriptQRCode.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
#if !NETSTANDARD1_3
using System;
using System.Drawing;
using static QRCoder.QRCodeGenerator;

namespace QRCoder
{

#if NET6_0_WINDOWS
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public class PostscriptQRCode : AbstractQRCode, IDisposable
{
/// <summary>
Expand Down Expand Up @@ -143,9 +140,6 @@ sc sc scale
";
}

#if NET6_0_WINDOWS
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public static class PostscriptQRCodeHelper
{
public static string GetQRCode(string plainText, int pointsPerModule, string darkColorHex, string lightColorHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, bool drawQuietZones = true, bool epsFormat = false)
Expand All @@ -157,5 +151,4 @@ public static string GetQRCode(string plainText, int pointsPerModule, string dar
}
}
}

#endif
18 changes: 17 additions & 1 deletion QRCoder/SvgQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public SvgLogo(Bitmap iconRasterized, int iconSizePercent = 15, bool fillLogoBac
using (var bitmap = new Bitmap(iconRasterized))
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
_logoData = Convert.ToBase64String(ms.GetBuffer(), Base64FormattingOptions.None);
_logoData = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length, Base64FormattingOptions.None);
}
}
_mediaType = MediaType.PNG;
Expand All @@ -312,6 +312,22 @@ public SvgLogo(string iconVectorized, int iconSizePercent = 15, bool fillLogoBac
_isEmbedded = iconEmbedded;
}

/// <summary>
/// Create a logo object to be used in SvgQRCode renderer
/// </summary>
/// <param name="iconRasterized">Logo to be rendered as PNG</param>
/// <param name="iconSizePercent">Degree of percentage coverage of the QR code by the logo</param>
/// <param name="fillLogoBackground">If true, the background behind the logo will be cleaned</param>
public SvgLogo(byte[] iconRasterized, int iconSizePercent = 15, bool fillLogoBackground = true)
{
_iconSizePercent = iconSizePercent;
_logoData = Convert.ToBase64String(iconRasterized, Base64FormattingOptions.None);
_mediaType = MediaType.PNG;
_fillLogoBackground = fillLogoBackground;
_logoRaw = iconRasterized;
_isEmbedded = false;
}

/// <summary>
/// Returns the raw logo's data
/// </summary>
Expand Down
12 changes: 6 additions & 6 deletions QRCoderTests/AsciiQRCodeRendererTests.cs

Large diffs are not rendered by default.

35 changes: 33 additions & 2 deletions QRCoderTests/PayloadGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2669,7 +2669,22 @@ public void one_time_password_generator_time_based_generates_with_standard_optio
Label = "[email protected]",
};

pg.ToString().ShouldBe("otpauth://totp/Google:[email protected]?secret=pwq65q55&issuer=Google");
pg.ToString().ShouldBe("otpauth://totp/Google:test%40google.com?secret=pwq65q55&issuer=Google");
}


[Fact]
[Category("PayloadGenerator/OneTimePassword")]
public void one_time_password_generator_time_based_generates_with_standard_options_escapes_issuer_and_label()
{
var pg = new PayloadGenerator.OneTimePassword
{
Secret = "pwq6 5q55",
Issuer = "Google Google",
Label = "test/[email protected]",
};

pg.ToString().ShouldBe("otpauth://totp/Google%20Google:test%2Ftest%40google.com?secret=pwq65q55&issuer=Google%20Google");
}


Expand All @@ -2686,7 +2701,23 @@ public void one_time_password_generator_hmac_based_generates_with_standard_optio
Counter = 500,
};

pg.ToString().ShouldBe("otpauth://hotp/Google:[email protected]?secret=pwq65q55&issuer=Google&counter=500");
pg.ToString().ShouldBe("otpauth://hotp/Google:test%40google.com?secret=pwq65q55&issuer=Google&counter=500");
}

[Fact]
[Category("PayloadGenerator/OneTimePassword")]
public void one_time_password_generator_hmac_based_generates_with_standard_options_escapes_issuer_and_label()
{
var pg = new PayloadGenerator.OneTimePassword
{
Secret = "pwq6 5q55",
Issuer = "Google Google",
Label = "test/[email protected]",
Type = PayloadGenerator.OneTimePassword.OneTimePasswordAuthType.HOTP,
Counter = 500,
};

pg.ToString().ShouldBe("otpauth://hotp/Google%20Google:test%2Ftest%40google.com?secret=pwq65q55&issuer=Google%20Google&counter=500");
}


Expand Down
91 changes: 91 additions & 0 deletions QRCoderTests/PostscriptQRCodeRendererTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#if !NETCOREAPP1_1
using QRCoder;
using QRCoderTests.Helpers;
using QRCoderTests.Helpers.XUnitExtenstions;
using Shouldly;
using System;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using Xunit;

namespace QRCoderTests
{
public class PostscriptQRCodeRendererTests
{
[Fact]
[Category("QRRenderer/PostscriptQRCode")]
public void can_render_postscript_qrcode_simple()
{
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var ps = new PostscriptQRCode(data).GetGraphic(5);

var result = HelperFunctions.StringToHash(RemoveCreationDate(ps));
result.ShouldBe("06b90d1e64bf022a248453e5f91101a0");
}

[Fact]
[Category("QRRenderer/PostscriptQRCode")]
public void can_render_postscript_qrcode_eps()
{
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var ps = new PostscriptQRCode(data).GetGraphic(5, true);

var result = HelperFunctions.StringToHash(RemoveCreationDate(ps));
result.ShouldBe("50f6152cdb0b685595d80e7888712d3b");
}

[Fact]
[Category("QRRenderer/PostscriptQRCode")]
public void can_render_postscript_qrcode_size()
{
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var ps = new PostscriptQRCode(data).GetGraphic(new Size(33, 33));

var result = HelperFunctions.StringToHash(RemoveCreationDate(ps));
result.ShouldBe("49c7faaafef312eb4b6ea1fec195e63d");
}

[Fact]
[Category("QRRenderer/PostscriptQRCode")]
public void can_render_postscript_qrcode_size_no_quiet_zones()
{
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var ps = new PostscriptQRCode(data).GetGraphic(new Size(50, 50), false);

var result = HelperFunctions.StringToHash(RemoveCreationDate(ps));
result.ShouldBe("9bfa0468e125d9815a39902133a10762");
}

[Fact]
[Category("QRRenderer/PostscriptQRCode")]
public void can_render_postscript_qrcode_colors()
{
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var ps = new PostscriptQRCode(data).GetGraphic(5, Color.Red, Color.Blue);

var result = HelperFunctions.StringToHash(RemoveCreationDate(ps));
result.ShouldBe("2e001d7f67a446eb1b5df32ff5321808");
}

private static string RemoveCreationDate(string text)
{
// Regex pattern to match lines that start with %%CreationDate: followed by any characters until the end of the line
string pattern = @"%%CreationDate:.*\r?\n?";

// Use Regex.Replace to remove matching lines
return Regex.Replace(text, pattern, string.Empty, RegexOptions.Multiline);
}
}
}
#endif
23 changes: 21 additions & 2 deletions QRCoderTests/SvgQRCodeRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void can_render_svg_qrcode_without_quietzones_hex()
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
[Fact]
[Category("QRRenderer/SvgQRCode")]
public void can_render_svg_qrcode_with_png_logo()
public void can_render_svg_qrcode_with_png_logo_bitmap()
{
//Create QR code
var gen = new QRCodeGenerator();
Expand All @@ -120,10 +120,29 @@ public void can_render_svg_qrcode_with_png_logo()
var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj);

var result = HelperFunctions.StringToHash(svg);
result.ShouldBe("78e02e8ba415f15817d5ed88c4afca31");
result.ShouldBe("78e02e8ba415f15817d5ed88c4afca31");
}
#endif

[Fact]
[Category("QRRenderer/SvgQRCode")]
public void can_render_svg_qrcode_with_png_logo_bytearray()
{
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);

//Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909346
var logoBitmap = System.IO.File.ReadAllBytes(GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png");
var logoObj = new SvgQRCode.SvgLogo(iconRasterized: logoBitmap, 15);
logoObj.GetMediaType().ShouldBe<SvgQRCode.SvgLogo.MediaType>(SvgQRCode.SvgLogo.MediaType.PNG);

var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj);

var result = HelperFunctions.StringToHash(svg);
result.ShouldBe("7d53f25af04e52b20550deb2e3589e96");
}

[Fact]
[Category("QRRenderer/SvgQRCode")]
public void can_render_svg_qrcode_with_svg_logo_embedded()
Expand Down

0 comments on commit f0a7865

Please sign in to comment.