public static bool IsValidZeroFormat(string lowerCaseIntroducedNumber, int initialIndex, out int newIndex) { const string AllowedChars = ".e"; newIndex = initialIndex; if (lowerCaseIntroducedNumber == null) { return(false); } if (lowerCaseIntroducedNumber[initialIndex] != '0') { return(true); } newIndex++; bool existNextChar = lowerCaseIntroducedNumber.Length > newIndex; if (!existNextChar) { return(true); } return(AllowedChars.IndexOf(lowerCaseIntroducedNumber[initialIndex + 1]) >= 0); }
/// <summary> /// Get a secure random string. /// </summary> /// <param name="length">String length</param> /// <param name="allowedChars">Which characters to use.</param> /// <returns></returns> public static string SecureRandom(int length, AllowedChars allowedChars = AllowedChars.All) { var chars = string.Empty; if (allowedChars.HasFlag(AllowedChars.AlphabetMin)) { chars += AlphabetMin; } if (allowedChars.HasFlag(AllowedChars.AlphabetMaj)) { chars += AlphabetMaj; } if (allowedChars.HasFlag(AllowedChars.Numbers)) { chars += Numbers; } if (allowedChars.HasFlag(AllowedChars.SpecialChars)) { chars += SpecialChars; } if (allowedChars.HasFlag(AllowedChars.Spaces)) { chars += Space; } var stringChars = new char[length]; for (int i = 0; i < stringChars.Length; i++) { stringChars[i] = chars[RandomNumberGenerator.GetInt32(chars.Length)]; } return(new string(stringChars)); }
/// <summary> /// Url encoding some string /// </summary> /// <param name="value">target</param> /// <param name="encoding">using encode</param> /// <param name="upper">helix cast to upper</param> /// <returns>encoded string</returns> public static string UrlEncode(string value, Encoding encoding, bool upper) { StringBuilder result = new StringBuilder(); byte[] data = encoding.GetBytes(value); int len = data.Length; for (int i = 0; i < len; i++) { int c = data[i]; if (c < 0x80 && AllowedChars.IndexOf((char)c) != -1) { result.Append((char)c); } else { if (upper) { result.Append('%' + String.Format("{0:X2}", (int)data[i])); } else { result.Append('%' + String.Format("{0:x2}", (int)data[i])); } } } return(result.ToString()); }
public static bool IsValidPointFormat(string lowerCaseIntroducedNumber, int index, out int incrementIndex) { const string AllowedChars = "0123456789"; incrementIndex = 1; if (lowerCaseIntroducedNumber == null) { return(false); } if (lowerCaseIntroducedNumber[index] != '.') { return(true); } bool pointOnFirstPosition = index == 0; bool existNextChar = lowerCaseIntroducedNumber.Length > index + 1; if (pointOnFirstPosition || !existNextChar) { return(false); } incrementIndex++; bool nextCharIsNumber = AllowedChars.IndexOf(lowerCaseIntroducedNumber[index + 1]) >= 0; bool noExistAnotherPoint = lowerCaseIntroducedNumber.IndexOf('.', index + 1) == -1; return(nextCharIsNumber && noExistAnotherPoint); }
/// <inheritdoc/> public async Task <string> GeneratePassword(int length, AllowedChars allowedChars, bool asBase64) { var allowed = string.Empty; if (allowedChars == AllowedChars.All) { allowed = kUppercaseLetters + kLowercaseLetters + kDigits + kSpecial; } else { if ((allowedChars & AllowedChars.Uppercase) == AllowedChars.Uppercase) { allowed += kUppercaseLetters; } if ((allowedChars & AllowedChars.Lowercase) == AllowedChars.Lowercase) { allowed += kLowercaseLetters; } if ((allowedChars & AllowedChars.Digits) == AllowedChars.Digits) { allowed += kDigits; } if ((allowedChars & AllowedChars.Special) == AllowedChars.Special) { allowed += kSpecial; } } return(await GeneratePassword(length, allowed, asBase64)); }
/// <summary> /// Convert data to decimal base to specified base /// </summary> /// <param name="sourceValue">Source base-N string</param> /// <param name="baseN">Base-N</param> /// <returns></returns> public static int ConvertFromBaseN(string sourceValue, int baseN) { //Validazione argomenti if (string.IsNullOrEmpty(sourceValue)) { throw new ArgumentNullException(nameof(sourceValue)); } if (baseN <= 0) { throw new ArgumentOutOfRangeException(nameof(baseN)); } //Definisco i caratteri utilizzati const string AllowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Verifico che la base specificata sia valida if (baseN > AllowedChars.Length) { throw new InvalidProgramException( $"Base-N value must be greater then zero and lower or equals to {AllowedChars.Length}."); } //Eseguo l'uppercase della stringa string elaboratedSource = sourceValue.ToUpper(); int decimalValue = 0; //Recupero solo i caratteri contemplati nella base definita //quindi separo in caratteri singoli la stringa sorgente string baseChars = AllowedChars.Substring(0, baseN); char[] allChars = elaboratedSource.ToArray(); //Scorro tutti i caratteri (partendo dall'ultimo) for (int i = 0; i < allChars.Length; i++) { //Recupero il carattere da elaborare var currentChar = allChars[i]; //Calcolo il peso del carattere all'interno dei caratteri contemplati var weight = baseChars.IndexOf(currentChar); //Calcolo il moltiplicatore var multiplier = (allChars.Length - 1) - i; //Calcolo il valore decimale del carattere corrente come //posizione dello stesso nella stringa dei caratteri permessi //moltiplicato per la base, elevata alla posizione del carattere //all'interno della stringa sorgente (es. "100" in binario equivale //a 2(base)^2(posizione in stringa) * 1(posizione di "1" in "01") double currentDecimalValue = Math.Pow(baseN, multiplier) * weight; //Sommo il valore al precedente decimalValue = decimalValue + (int)Math.Round(currentDecimalValue, 0); } //Emetto il decimale return(decimalValue); }
private void InitializeAllowedSet() { AllowedChars.Add(','); AllowedChars.Add(' '); AllowedChars.Add('.'); AllowedChars.Add('\''); AllowedChars.Add('\"'); AllowedChars.Add('?'); AllowedChars.Add(':'); AllowedChars.Add(';'); }
private bool KeyIsValid(string id) { if (string.IsNullOrWhiteSpace(id)) { return(false); } if (id.ToCharArray() .Any(o => !AllowedChars.ContainsKey(o))) { return(false); } return(true); }
public string CreatePassword(int length) { const string AllowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#@$^*()"; Random rnd = new Random(); string newPassword = ""; int AllowedCharsLen = AllowedChars.Length; for (int i = 0; i < length; i++) { newPassword += AllowedChars.Substring(rnd.Next(0, AllowedCharsLen), 1); } return(newPassword); }
public void ExecuteSelectedCommandAndDefault() { if (AllowedChars.Contains(activeCommandKey)) { var command = dictionary[activeCommandKey]; if (!command.IsContinious()) { activeCommandKey = ' '; } command.Execute(); General.Execute(); } }
/// <summary> /// Convert decimal value to base-N string /// </summary> /// <param name="decimalValue">Decimal value</param> /// <param name="baseN">Base-N</param> /// <returns>Returns converted value</returns> public static string ConvertToBaseN(int decimalValue, int baseN) { //Validazione argomenti if (decimalValue < 0) { throw new ArgumentOutOfRangeException( nameof(decimalValue), "Value base-10 must be greater then zero."); } if (baseN < 0 || baseN > 36) { throw new ArgumentOutOfRangeException( nameof(baseN), "Base-N value must be greater then zero and lower or equals t0 36."); } //Definisco i caratteri utilizzati const string AllowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Imposto una stringa di uscita e i valori resto string outString = ""; int divideResult = decimalValue; //Eseguo un ciclo sull'elemento valore da convertire in "base N" //eseguendo una divisione con resto ad ogni ciclo do { //Eseguo la divisione tra il risultato della precedente divisione //e il valore di base, recuperando il resto e impostando il nuovo risultato int divideRemaining; divideResult = Math.DivRem(divideResult, baseN, out divideRemaining); //Eseguo la conversione del resto nel carattere di base 36 //quindi lo accodo a sinistra della stringa di uscita outString = AllowedChars.ToArray()[divideRemaining] + outString; } //Eseguo il ciclo finchè ho un risultato maggiore di zero while (divideResult > 0); //Ritorno la stringa convertita return(outString); }
private static bool ValidateHairColour(string input) { const string AllowedChars = "abcdef1234567890"; if (7 > input.Length) { return(false); } if ('#' != input[0]) { return(false); } foreach (var ch in input.ToLowerInvariant().Skip(1)) { if (false == AllowedChars.Contains(ch)) { return(false); } } return(true); }
public static bool IsValidEFormat(string lowerCaseIntroducedNumber, int index, out int incrementIndex) { const int OnlyNumbers = 2; const string AllowedChars = "-+0123456789"; incrementIndex = 1; if (lowerCaseIntroducedNumber == null) { return(false); } if (lowerCaseIntroducedNumber[index] != 'e') { return(true); } bool charEOnFirstPosition = index == 0; bool existNextChar = lowerCaseIntroducedNumber.Length > index + incrementIndex; if (charEOnFirstPosition || !existNextChar) { return(false); } bool existAnotherE = lowerCaseIntroducedNumber.IndexOf('e', index + incrementIndex) >= 0; bool existPointInFront = lowerCaseIntroducedNumber.IndexOf('.', index + incrementIndex) >= 0; if (existAnotherE || existPointInFront) { return(false); } bool nextCharIsNumber = AllowedChars.IndexOf(lowerCaseIntroducedNumber[index + incrementIndex], OnlyNumbers) != -1; if (nextCharIsNumber) { incrementIndex++; return(true); } bool isMinusOrPlus = AllowedChars.IndexOf(lowerCaseIntroducedNumber[index + incrementIndex], 0, OnlyNumbers) != -1; if (!isMinusOrPlus) { return(false); } incrementIndex++; existNextChar = lowerCaseIntroducedNumber.Length > index + incrementIndex; if (!existNextChar) { return(false); } nextCharIsNumber = AllowedChars.IndexOf(lowerCaseIntroducedNumber[index + incrementIndex], OnlyNumbers) != -1; incrementIndex++; return(nextCharIsNumber); }