| | 1 | | using OtpNet; |
| | 2 | | using QRCoder; |
| | 3 | | using Newtonsoft.Json; |
| | 4 | | using System.Globalization; |
| | 5 | | using Newtonsoft.Json.Converters; |
| | 6 | | using System.Text.RegularExpressions; |
| | 7 | |
|
| | 8 | | namespace Syki.Shared; |
| | 9 | |
|
| | 10 | | public static class StringExtensions |
| | 11 | | { |
| | 12 | | public static bool IsEmpty(this string? text) |
| | 13 | | { |
| 1500 | 14 | | return string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text); |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public static bool IsIn(this string? text, params string[] others) |
| | 18 | | { |
| 35 | 19 | | if (text.IsEmpty()) |
| 3 | 20 | | return true; |
| | 21 | |
|
| 150 | 22 | | foreach (var other in others) |
| | 23 | | { |
| 51 | 24 | | if (other.Contains(text!, StringComparison.OrdinalIgnoreCase)) |
| 16 | 25 | | return true; |
| | 26 | | } |
| | 27 | |
|
| 16 | 28 | | return false; |
| | 29 | | } |
| | 30 | |
|
| | 31 | | public static bool HasValue(this string? text) |
| | 32 | | { |
| 1519 | 33 | | return !string.IsNullOrEmpty(text); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | public static string OnlyNumbers(this string text) |
| | 37 | | { |
| 1519 | 38 | | if (text.HasValue()) |
| | 39 | | { |
| 1510 | 40 | | return new string(text.Where(char.IsDigit).ToArray()); |
| | 41 | | } |
| | 42 | |
|
| 9 | 43 | | return ""; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public static string ToSnakeCase(this string input) |
| | 47 | | { |
| 161 | 48 | | if (input.IsEmpty()) { return ""; } |
| | 49 | |
|
| 155 | 50 | | var startUnderscores = Regex.Match(input, @"^_+"); |
| 155 | 51 | | return startUnderscores + Regex.Replace(input, @"([a-z0-9])([A-Z])", "$1_$2").ToLower(); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | public static string ToMfaToken(this string key) |
| | 55 | | { |
| 5 | 56 | | var totp = new Totp(Base32Encoding.ToBytes(key)); |
| 5 | 57 | | return totp.ComputeTotp(); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | public static string ToBase64(this string value) |
| | 61 | | { |
| 2 | 62 | | var bytes = System.Text.Encoding.UTF8.GetBytes(value); |
| 2 | 63 | | return Convert.ToBase64String(bytes); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public static string Format(this decimal value) |
| | 67 | | { |
| 5 | 68 | | return value.ToString("0.00", CultureInfo.InvariantCulture); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | public static string Format(this int value) |
| | 72 | | { |
| 0 | 73 | | return value.ToString("N0", CultureInfo.CreateSpecificCulture("pt-BR")); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | public static bool IsValidEmail(this string email) |
| | 77 | | { |
| 928 | 78 | | if (email.IsEmpty()) return false; |
| 912 | 79 | | return Regex.IsMatch(email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](? |
| | 80 | | } |
| | 81 | |
|
| 1 | 82 | | private static JsonSerializerSettings _settings = new() |
| 1 | 83 | | { |
| 1 | 84 | | Converters = [new StringEnumConverter()], |
| 1 | 85 | | }; |
| | 86 | |
|
| | 87 | | public static string Serialize(this object obj) |
| | 88 | | { |
| 8589 | 89 | | return JsonConvert.SerializeObject(obj, _settings); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | public static Byte[] GenerateQrCodeBytes(this string key, string email) |
| | 93 | | { |
| | 94 | | const string provider = "Syki"; |
| | 95 | |
|
| 0 | 96 | | using var qrGenerator = new QRCodeGenerator(); |
| 0 | 97 | | using var qrCodeData = qrGenerator.CreateQrCode( |
| 0 | 98 | | $"otpauth://totp/{provider}:{email}?secret={key}&issuer={provider}", |
| 0 | 99 | | QRCodeGenerator.ECCLevel.Q |
| 0 | 100 | | ); |
| | 101 | |
|
| 0 | 102 | | var qrCode = new PngByteQRCode(qrCodeData); |
| | 103 | |
|
| 0 | 104 | | return qrCode.GetGraphic(20); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | public static string MinutesToString(this int value) |
| | 108 | | { |
| 17 | 109 | | var hours = value / 60; |
| 17 | 110 | | var minutes = value % 60; |
| | 111 | |
|
| 18 | 112 | | if (hours == 0 && minutes == 0) return "0"; |
| 19 | 113 | | if (hours == 0) return $"{minutes}min"; |
| 22 | 114 | | if (minutes == 0) return $"{hours}h"; |
| | 115 | |
|
| 4 | 116 | | return $"{hours}h e {minutes}min"; |
| | 117 | | } |
| | 118 | | } |