Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- Include of 'SkiaSharp.NativeAssets.Linux.NoDependencies' because of an error on linux-systems (See FAQ for more)

- Moved from property CaptchaBackup, to method GenerateCaptchaContent()

- Option to enable or disable the use of numbers inside the captcha

- Changed image from png to jpg, to save a tiny bit of image size
  • Loading branch information
liebki committed Jun 14, 2023
1 parent 6fca86f commit 47e20c1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
7 changes: 3 additions & 4 deletions BlazorVerificationCaptcha.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<PackageIcon>uxwing-icon.png</PackageIcon>
<RepositoryUrl>https://github.com/liebki/BlazorVerificationCaptcha</RepositoryUrl>
<PackageTags>captcha, alternative, simple, blazor, recaptcha alternative</PackageTags>
<PackageReleaseNotes>Created the library</PackageReleaseNotes>
<PackageReleaseNotes>Include of 'SkiaSharp.NativeAssets.Linux.NoDependencies' for linux, moved from property CaptchaBackup, to method GenerateCaptchaContent(), new option to enable or disable the use of numbers inside the captcha, changed the image from png to jpg, to save a tiny bit of image size</PackageReleaseNotes>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<Version>0.1</Version>
<Version>0.1.1</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

Expand All @@ -28,6 +28,7 @@

<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.88.3" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.3" />
</ItemGroup>

<ItemGroup>
Expand All @@ -45,6 +46,4 @@
</None>
</ItemGroup>



</Project>
10 changes: 5 additions & 5 deletions BlazorVerificationCaptcha/CaptchaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public static class CaptchaGenerator
/// The generator is subject to change, I want to make it more difficult to read the captcha-content
/// </summary>
/// <returns>Image as base64</returns>
public static (string imagevalue, string textvalue) GenerateCaptcha()
public static (string imagevalue, string textvalue) GenerateCaptcha(bool IncludeNumbers, string TypefaceFamilyName)
{
const int width = 200;
const int height = 50;

string captchaText = Tools.GenerateRandomText();
string captchaText = Tools.GenerateRandomText(IncludeNumbers);

using SKBitmap bitmap = new(width, height);
using SKCanvas canvas = new(bitmap);
Expand All @@ -24,7 +24,7 @@ public static (string imagevalue, string textvalue) GenerateCaptcha()
paint.TextSize = RandomFloatValue(19, 25);
paint.Color = SKColors.Black;

using (SKTypeface typeface = SKTypeface.FromFamilyName("Arial"))
using (SKTypeface typeface = SKTypeface.FromFamilyName(TypefaceFamilyName))
using (SKFont font = new(typeface, paint.TextSize))
{
paint.ImageFilter = SKImageFilter.CreateBlur(1, 1);
Expand All @@ -37,7 +37,7 @@ public static (string imagevalue, string textvalue) GenerateCaptcha()
}

using SKImage image = SKImage.FromBitmap(bitmap);
using SKData data = image.Encode(SKEncodedImageFormat.Png, 100);
using SKData data = image.Encode(SKEncodedImageFormat.Jpeg, 100);

return (ConvStreamToBase64(data), captchaText);
}
Expand Down Expand Up @@ -74,7 +74,7 @@ private static string ConvStreamToBase64(SKData data)
byte[] byteArray = ms.ToArray();
string b64String = Convert.ToBase64String(byteArray);

return $"data:image/png;base64,{b64String}";
return $"data:image/jpg;base64,{b64String}";
}

private static float RandomFloatValue(int min, int max)
Expand Down
13 changes: 9 additions & 4 deletions BlazorVerificationCaptcha/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ internal static class Tools
/// </summary>
/// <param name="AddSpaces"></param>
/// <returns>Random characters as string</returns>
internal static string GenerateRandomText(bool AddSpaces = true)
internal static string GenerateRandomText(bool UseNumbers, bool AddSpaces = true)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder sb = new();
int outputLength = 6;

string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

if (UseNumbers)
{
chars += "0123456789";
}

if (!AddSpaces)
{
outputLength = 8;
}

StringBuilder sb = new();

for (int i = 0; i < outputLength; i++)
{
sb.Append(chars[Random.Shared.Next(chars.Length)]);
Expand Down
16 changes: 4 additions & 12 deletions BlazorVerificationCaptcha/VerificationCaptcha.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@
{
partial class VerificationCaptcha
{
public static string CaptchaBackup
{
get
{
return RenewCaptchaContent();
}
}

private static string imageURL = string.Empty;

private static string RenewCaptchaContent()
public static string GenerateCaptchaContent(bool IncludeNumbers = true, string TypefaceFamilyName = "Arial")
{
(string image, string text) ImageText = CaptchaGenerator.GenerateCaptcha();
imageURL = ImageText.image;
(string image, string text) = CaptchaGenerator.GenerateCaptcha(IncludeNumbers, TypefaceFamilyName);
imageURL = image;

return ImageText.text.Replace(" ", string.Empty);
return text.Replace(" ", string.Empty);
}
}
}
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ A simple solution to verify users using a simple captcha with a current limit of

### NuGet/Dependencies used
- [SkiaSharp](https://www.nuget.org/packages/SkiaSharp/)
- [SkiaSharp.NativeAssets.Linux.NoDependencies](https://www.nuget.org/packages/SkiaSharp.NativeAssets.Linux.NoDependencies/) - Needed for Linux (See FAQ)!

## Features

Expand Down Expand Up @@ -36,7 +37,7 @@ To use BlazorVerificationCaptcha in your Blazor application, follow these steps:
1. Install the BlazorVerificationCaptcha package from NuGet.
2. Add ```@using BlazorVerificationCaptcha``` inside the _Imports.razor
3. Implement the captcha component in your desired location, by adding ```<VerificationCaptcha />```
4. Add ```ExampleVariable = VerificationCaptcha.CaptchaBackup;``` inside ```OnInitializedAsync()```, as this code generates the image
4. Add ```ExampleVariable = VerificationCaptcha.GenerateCaptchaContent();``` inside ```OnInitializedAsync()```, as this code generates the image

## Example (Component)

Expand All @@ -51,7 +52,7 @@ Here is an example of how you can use BlazorVerificationCaptcha in your Blazor c
protected override Task OnInitializedAsync()
{
ExampleVariable = VerificationCaptcha.CaptchaBackup;
ExampleVariable = VerificationCaptcha.GenerateCaptchaContent();
return base.OnInitializedAsync();
}
}
Expand Down Expand Up @@ -82,6 +83,12 @@ you can easily just do ```if(ExampleVariable.Equals(ExampleInputVariable, String

## FAQ

#### Notes on linux

I tried using it on linux I learned that in order to run it there you eventually have to install missing packages, I'm running CentOs so I executed:
```yum install fontconfig freetype freetype-devel fontconfig-devel libstdc++```
It could be that only 'fontconfig' is needed but I wanted to be sure, so I the captcha is only showing colored backgrounds and no content, install these dependencies you need them.

#### What do I have to be aware of?

The image is displayed using base64, which adds some overhead. You should test if this method meets your requirements. In future updates, an alternative way of displaying the captcha image will be implemented.
Expand Down

0 comments on commit 47e20c1

Please sign in to comment.