-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommandLineAuth.cs
79 lines (74 loc) · 2.18 KB
/
CommandLineAuth.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CLARiNET
{
internal class CommandLineAuth
{
public static string UsernameOption(string userName)
{
// Username
if (String.IsNullOrEmpty(userName))
{
Console.WriteLine("Enter the username:\n");
userName = Console.ReadLine().Trim();
Console.WriteLine("");
}
return userName;
}
public static string PasswordOption(string password)
{
if (String.IsNullOrEmpty(password))
{
Console.WriteLine("Enter the password (will not be displayed):\n");
password = PasswordPrompt();
}
else
{
password = Crypto.Unprotect(password);
}
return password;
}
public static string PasswordPrompt()
{
string pass = string.Empty;
ConsoleKey key;
do
{
var keyInfo = Console.ReadKey(intercept: true);
key = keyInfo.Key;
if (key == ConsoleKey.Backspace && pass.Length > 0)
{
Console.Write("\b \b");
pass = pass[0..^1];
}
else if (!char.IsControl(keyInfo.KeyChar))
{
Console.Write("*");
pass += keyInfo.KeyChar;
}
} while (key != ConsoleKey.Enter);
return pass;
}
public static void OptionsEncrypt()
{
Console.WriteLine("Enter a password to encrypt:\n");
string pass = CommandLineAuth.PasswordPrompt();
string encPass = "";
try
{
encPass = Crypto.Protect(pass);
}
// Perform a retry
catch
{
encPass = Crypto.Protect(pass);
}
Console.WriteLine("\n\n" + encPass);
Console.WriteLine("\n");
}
}
}