Add Wireguard KeyGen Util
This commit is contained in:
47
tools/AS1024.Wireguard.Utils.Cli/Program.cs
Normal file
47
tools/AS1024.Wireguard.Utils.Cli/Program.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using AS1024.Wireguard.Utils;
|
||||
|
||||
const string Help =
|
||||
"""
|
||||
wgkey - WireGuard key utility
|
||||
|
||||
wgkey gen
|
||||
wgkey pub <private-base64>
|
||||
wgkey decode <key-base64>
|
||||
""";
|
||||
|
||||
if (args.Length == 0 || args[0] is "-h" or "--help" or "help")
|
||||
{
|
||||
Console.WriteLine(Help);
|
||||
return 0;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// TODO: stdin mode?
|
||||
switch (args[0])
|
||||
{
|
||||
case "gen":
|
||||
{
|
||||
var priv = WireGuardKeyUtils.GeneratePrivateKey();
|
||||
var pub = WireGuardKeyUtils.GetPublicKey(priv);
|
||||
Console.WriteLine($"private={Convert.ToBase64String(priv)}");
|
||||
Console.WriteLine($"public={Convert.ToBase64String(pub)}");
|
||||
return 0;
|
||||
}
|
||||
case "pub" when args.Length == 2:
|
||||
Console.WriteLine(Convert.ToBase64String(
|
||||
WireGuardKeyUtils.GetPublicKey(WireGuardKeyUtils.FromBase64(args[1]))));
|
||||
return 0;
|
||||
case "decode" when args.Length == 2:
|
||||
Console.WriteLine(Convert.ToHexString(WireGuardKeyUtils.FromBase64(args[1])).ToLowerInvariant());
|
||||
return 0;
|
||||
default:
|
||||
Console.Error.WriteLine(Help);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
catch (Exception e) when (e is ArgumentException or FormatException)
|
||||
{
|
||||
Console.Error.WriteLine(e.Message);
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user