diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..cddf5d9 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/sample/bin/Debug/netcoreapp2.0/sample.dll", + "args": [], + "cwd": "${workspaceFolder}/sample", + // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window + "console": "internalConsole", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ,] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..25d6faa --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/sample/sample.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 996502a..b806360 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,71 @@ After searching around for a solution to creating SSH keys using C# and dotnet core I came across the following stackoverflow posts which explain how to convert from `RSACryptoServiceProvider` to values suitable for use with ssh. -# References +## Supported Platforms + +- .NET 4.5 (Desktop / Server) +- .NET Standard 2.0 + +## Installation + +SshKeyGenerator is a library for .NET and is available on NuGet: + +``` +Install-Package SshKeyGenerator +``` + +or + +``` +dotnet add package SshKeyGenerator +``` + +## Usage Example + +``` +var keygen = new SshKeyGenerator(2048); + +var privateKey = keygen.ToPrivateKey(); +Console.WriteLine(privateKey); + +var publicSshKey = keygen.ToRfcPublicKey(); +Console.WriteLine(publicSshKey); + +var publicSshKeyWithComment = keygen.ToRfcPublicKey("user@domain.com"); +Console.WriteLine(publicSshKeyWithComment); +``` + +### Sample Output + +#### keygen.ToPrivateKey() + +``` +-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEAs1O/9F2XlZVaVWEUYfflxx0wDG9FE09hQF2wngA6EemHpe6I +hvdwvQ7obrZ85jSYdBxBRjNAYF3T9+wU+8w6m4qZCVYvqqRjszN8j3VwUBLzWilt +... +wDGzJIkmbhANi+252pH6PAuWjZPfz8COGA6QPPH0/khpHPj1LY4mi5Ubi3YT1uRo +dOGttjtkj9fLX5IQ81G9HR0MdT1Gd4fPenYPOUCgCPB5adpT1BB+ +-----END RSA PRIVATE KEY----- +``` + +#### keygen.ToRfcPublicKey() + +``` +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzU7/0XZeVlVpVYRRh9 +... ++ohFsbcbAMcfLWGUdouyHvLvF21G0z50z2i2CrU7WW4WdrGGfxhU3TeRTXd7Skwk/K7iBBn3oc/xct generated-key +``` + +#### keygen.ToRfcPublicKey("user@domain.com") + +``` +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzU7/0XZeVlVpVYRRh9 +... ++ohFsbcbAMcfLWGUdouyHvLvF21G0z50z2i2CrU7WW4WdrGGfxhU3TeRTXd7Skwk/K7iBBn3oc/xct user@domain.com +``` + +## References - Exporting Private Key: https://stackoverflow.com/a/23739932 - Exporting Public Key: https://stackoverflow.com/a/25591659 diff --git a/SshKeyGenerator.sln b/SshKeyGenerator.sln index 0f64a71..c7d882e 100644 --- a/SshKeyGenerator.sln +++ b/SshKeyGenerator.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.26124.0 MinimumVisualStudioVersion = 15.0.26124.0 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SshKeyGenerator", "src\SshKeyGenerator.csproj", "{D01AA9BC-6763-4E27-A115-4C6D61B94787}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sample", "sample\sample.csproj", "{1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,5 +32,17 @@ Global {D01AA9BC-6763-4E27-A115-4C6D61B94787}.Release|x64.Build.0 = Release|x64 {D01AA9BC-6763-4E27-A115-4C6D61B94787}.Release|x86.ActiveCfg = Release|x86 {D01AA9BC-6763-4E27-A115-4C6D61B94787}.Release|x86.Build.0 = Release|x86 + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Debug|x64.ActiveCfg = Debug|x64 + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Debug|x64.Build.0 = Debug|x64 + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Debug|x86.ActiveCfg = Debug|x86 + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Debug|x86.Build.0 = Debug|x86 + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Release|Any CPU.Build.0 = Release|Any CPU + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Release|x64.ActiveCfg = Release|x64 + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Release|x64.Build.0 = Release|x64 + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Release|x86.ActiveCfg = Release|x86 + {1D3CAFD6-22ED-4D96-AAB2-8207DE83B181}.Release|x86.Build.0 = Release|x86 EndGlobalSection EndGlobal diff --git a/sample/Program.cs b/sample/Program.cs new file mode 100644 index 0000000..ee661af --- /dev/null +++ b/sample/Program.cs @@ -0,0 +1,21 @@ +using System; + +namespace SshKeyGenerator +{ + class Program + { + static void Main(string[] args) + { + var keygen = new SshKeyGenerator(2048); + + var privateKey = keygen.ToPrivateKey(); + Console.WriteLine(privateKey); + + var publicSshKey = keygen.ToRfcPublicKey(); + Console.WriteLine(publicSshKey); + + var publicSshKeyWithComment = keygen.ToRfcPublicKey("user@domain.com"); + Console.WriteLine(publicSshKeyWithComment); + } + } +} diff --git a/sample/sample.csproj b/sample/sample.csproj new file mode 100644 index 0000000..99d3273 --- /dev/null +++ b/sample/sample.csproj @@ -0,0 +1,12 @@ + + + + + + + + Exe + netcoreapp2.0 + + + diff --git a/src/SshKeyGenerator.cs b/src/SshKeyGenerator.cs index c086ce9..97616af 100644 --- a/src/SshKeyGenerator.cs +++ b/src/SshKeyGenerator.cs @@ -52,8 +52,8 @@ public string ToPrivateKey() return outputStream.ToString(); } } - } - + } + public string ToRfcPublicKey() => ToRfcPublicKey("generated-key"); public string ToRfcPublicKey(string comment)