mirror of https://github.com/abpframework/abp.git
csharpabpc-sharpframeworkblazoraspnet-coredotnet-coreaspnetcorearchitecturesaasdomain-driven-designangularmulti-tenancy
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.1 KiB
73 lines
2.1 KiB
using System;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using SixLabors.ImageSharp.Formats;
|
|
using SixLabors.ImageSharp.Formats.Jpeg;
|
|
using SixLabors.ImageSharp.Formats.Png;
|
|
|
|
namespace Volo.CmsKit.Public.Web.Security.Captcha;
|
|
public static class RandomTextGenerator
|
|
{
|
|
private static readonly char[] AllowedChars = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVXYZW23456789".ToCharArray();
|
|
|
|
public static IImageEncoder GetEncoder(EncoderTypes encoderType)
|
|
{
|
|
IImageEncoder encoder = encoderType switch
|
|
{
|
|
EncoderTypes.Png => new PngEncoder(),
|
|
EncoderTypes.Jpeg => new JpegEncoder(),
|
|
_ => throw new ArgumentException($"Encoder '{encoderType}' not found!")
|
|
};
|
|
|
|
return encoder;
|
|
}
|
|
|
|
public static string GetRandomText(int size)
|
|
{
|
|
var data = new byte[4 * size];
|
|
using (var crypto = new RNGCryptoServiceProvider())
|
|
{
|
|
crypto.GetBytes(data);
|
|
}
|
|
|
|
var result = new StringBuilder(size);
|
|
for (var i = 0; i < size; i++)
|
|
{
|
|
var rnd = BitConverter.ToUInt32(data, i * 4);
|
|
var idx = rnd % AllowedChars.Length;
|
|
result.Append(AllowedChars[idx]);
|
|
}
|
|
|
|
return result.ToString();
|
|
}
|
|
|
|
public static string GetUniqueKey(int size, char[] chars)
|
|
{
|
|
var data = new byte[4 * size];
|
|
using (var crypto = new RNGCryptoServiceProvider())
|
|
{
|
|
crypto.GetBytes(data);
|
|
}
|
|
|
|
var result = new StringBuilder(size);
|
|
|
|
for (var i = 0; i < size; i++)
|
|
{
|
|
var rnd = BitConverter.ToUInt32(data, i * 4);
|
|
var idx = rnd % chars.Length;
|
|
result.Append(chars[idx]);
|
|
}
|
|
|
|
return result.ToString();
|
|
}
|
|
|
|
public static float GenerateNextFloat(double min = -3.40282347E+38, double max = 3.40282347E+38)
|
|
{
|
|
var random = new Random();
|
|
var range = max - min;
|
|
var sample = random.NextDouble();
|
|
var scaled = sample * range + min;
|
|
var result = (float)scaled;
|
|
return result;
|
|
}
|
|
}
|
|
|