Skip to content

Commit

Permalink
Add samples and better documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawInnes committed Jul 21, 2018
1 parent a8b8a8c commit e22cbb2
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 3 deletions.
28 changes: 28 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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}"
}
,]
}
15 changes: 15 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/sample/sample.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
66 changes: 65 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]");
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("[email protected]")

```
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzU7/0XZeVlVpVYRRh9
...
+ohFsbcbAMcfLWGUdouyHvLvF21G0z50z2i2CrU7WW4WdrGGfxhU3TeRTXd7Skwk/K7iBBn3oc/xct [email protected]
```

## References

- Exporting Private Key: https://stackoverflow.com/a/23739932
- Exporting Public Key: https://stackoverflow.com/a/25591659
Expand Down
14 changes: 14 additions & 0 deletions SshKeyGenerator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
21 changes: 21 additions & 0 deletions sample/Program.cs
Original file line number Diff line number Diff line change
@@ -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("[email protected]");
Console.WriteLine(publicSshKeyWithComment);
}
}
}
12 changes: 12 additions & 0 deletions sample/sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
<ProjectReference Include="..\src\SshKeyGenerator.csproj" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

</Project>
4 changes: 2 additions & 2 deletions src/SshKeyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public string ToPrivateKey()
return outputStream.ToString();
}
}
}

}

public string ToRfcPublicKey() => ToRfcPublicKey("generated-key");

public string ToRfcPublicKey(string comment)
Expand Down

0 comments on commit e22cbb2

Please sign in to comment.