-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- The captcha length is changeable (min. six characters tho) - The image dimension and random characters depend on the length of the captcha - You can change the quality of the image (harder or easier to read) - Minor additional changes like comments and properties etc.
- Loading branch information
Showing
6 changed files
with
187 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,51 @@ | ||
using System.Text; | ||
using SkiaSharp; | ||
using System.Text; | ||
|
||
namespace BlazorVerificationCaptcha | ||
{ | ||
internal static class Tools | ||
{ | ||
internal static readonly string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | ||
internal static readonly string AlphabetAndNumbers = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
internal static readonly SKColor[] ForegroundColors = new SKColor[] { SKColors.Black, SKColors.DarkBlue, SKColors.DarkGreen, SKColors.DarkViolet }; | ||
internal static readonly SKColor[] BackgroundColors = new SKColor[] { SKColors.White, SKColors.Yellow, SKColors.LightBlue, SKColors.OrangeRed }; | ||
private static readonly Random randi = new(); | ||
|
||
private static StringBuilder RandomCaptchaText { get; set; } = new(); | ||
|
||
/// <summary> | ||
/// Also subject to change, I want to allow more or less than six characters of captcha but for this I need to redo the image generation because of the dimensions | ||
/// Generates a random text containing letters and optionally numbers for CAPTCHA purposes. | ||
/// </summary> | ||
/// <param name="AddSpaces"></param> | ||
/// <returns>Random characters as string</returns> | ||
internal static string GenerateRandomText(bool UseNumbers, bool AddSpaces = true) | ||
/// <param name="UseNumbers">Indicates whether to include numbers in the generated text.</param> | ||
/// <param name="CaptchaLength">The desired length of the generated text.</param> | ||
/// <param name="AddSpaces">Indicates whether to add spaces between characters for better readability.</param> | ||
/// <returns>A randomly generated string containing letters and optionally numbers.</returns> | ||
internal static string GenerateRandomText(bool UseNumbers, int CaptchaLength, bool AddSpaces = true) | ||
{ | ||
StringBuilder sb = new(); | ||
int outputLength = 6; | ||
|
||
string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | ||
|
||
if (UseNumbers) | ||
RandomCaptchaText.Clear(); | ||
for (int i = 0; i < CaptchaLength; i++) | ||
{ | ||
chars += "0123456789"; | ||
} | ||
|
||
if (!AddSpaces) | ||
{ | ||
outputLength = 8; | ||
} | ||
if (UseNumbers) | ||
{ | ||
RandomCaptchaText.Append(AlphabetAndNumbers[Random.Shared.Next(AlphabetAndNumbers.Length)]); | ||
} | ||
else | ||
{ | ||
RandomCaptchaText.Append(Alphabet[Random.Shared.Next(Alphabet.Length)]); | ||
} | ||
|
||
for (int i = 0; i < outputLength; i++) | ||
{ | ||
sb.Append(chars[Random.Shared.Next(chars.Length)]); | ||
if (i < outputLength && AddSpaces) | ||
if (AddSpaces && i < CaptchaLength - 1) | ||
{ | ||
sb.Append(' '); | ||
RandomCaptchaText.Append(' '); | ||
} | ||
} | ||
|
||
return sb.ToString(); | ||
return RandomCaptchaText.ToString(); | ||
} | ||
|
||
internal static float RandomFloatValue(int Min, int Max) | ||
{ | ||
return randi.Next(Min, Max); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
@namespace BlazorVerificationCaptcha | ||
|
||
<img src=@imageURL /> | ||
<img src=@ImageURL /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.