private void ValidateSecretKey(byte[] input, ValidationContext <IAccessKey> context) { if (!_inputValidator.TryValidateAccessKey(input, out ValidationStatus status, out string?allowed)) { context.AddFailure("Invalid secret key: " + ValidationMessages.GetMessage(status, allowed)); } }
public static void ValidateAccessKeyAndThrow(this IInputValidator validator, byte[]?accessKey) { if (validator.TryValidateAccessKey(accessKey, out ValidationStatus status, out string?message)) { return; } throw new ArgumentException("Invalid access key: " + ValidationMessages.GetMessage(status, message), nameof(accessKey)); }
private byte[] GetAccessKey() { char[]? enteredAccessKey = null; byte[]? utf8AccessKey = null; bool validAccessKey = true; Console.WriteLine(); Console.WriteLine("Enter your access key - Example: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); do { if (!validAccessKey) { Console.Error.WriteLine("Invalid access key. Try again."); Array.Clear(enteredAccessKey !, 0, enteredAccessKey !.Length); Array.Clear(utf8AccessKey !, 0, utf8AccessKey !.Length); } enteredAccessKey = ConsoleHelper.ReadSecret(40); //Now we trim any whitespace characters the user might have entered int end; int start; for (start = 0; start < enteredAccessKey.Length;) { if (!char.IsWhiteSpace(enteredAccessKey[start])) { break; } start++; } for (end = enteredAccessKey.Length - 1; end >= start; end--) { if (!char.IsWhiteSpace(enteredAccessKey[end])) { break; } } if (start != 0 || end != enteredAccessKey.Length - 1) { char[] trimmed = new char[end + 1 - start]; int count = 0; for (int i = start; i < end + 1; i++, count++) { trimmed[count] = enteredAccessKey[i]; } Array.Clear(enteredAccessKey, 0, enteredAccessKey.Length); enteredAccessKey = trimmed; } utf8AccessKey = Encoding.UTF8.GetBytes(enteredAccessKey); } while (!(validAccessKey = _inputValidator.TryValidateAccessKey(utf8AccessKey, out _, out _))); //Clear the access key from memory Array.Clear(enteredAccessKey, 0, enteredAccessKey.Length); return(utf8AccessKey); }