-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from RicYaben/ssh-plugin
Fixes SSH protocol and clean the app
- Loading branch information
Showing
19 changed files
with
163 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package plugins | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
|
||
"github.com/riotpot/internal/logger" | ||
) | ||
|
||
type ( | ||
KeyType string | ||
KeySize int | ||
) | ||
|
||
const ( | ||
Public KeyType = "public" | ||
Private KeyType = "private" | ||
|
||
InsecureKey KeySize = 1024 | ||
LiteKey KeySize = 2048 | ||
DefaultKey KeySize = 4096 | ||
) | ||
|
||
type CKey interface { | ||
Generate() []byte | ||
GetPEM() []byte | ||
SetPEM(pem []byte) | ||
} | ||
|
||
type AbstractKey struct { | ||
CKey | ||
pem []byte | ||
} | ||
|
||
func (k *AbstractKey) GetPEM() []byte { | ||
return k.pem | ||
} | ||
|
||
func (k *AbstractKey) SetPEM(pem []byte) { | ||
k.pem = pem | ||
} | ||
|
||
type PrivateKey struct { | ||
key AbstractKey | ||
priv *rsa.PrivateKey | ||
} | ||
|
||
func (k *PrivateKey) GetPEM() []byte { | ||
return k.key.GetPEM() | ||
} | ||
|
||
func (k *PrivateKey) SetPEM(pem []byte) { | ||
k.key.SetPEM(pem) | ||
} | ||
|
||
func (k *PrivateKey) SetKey(key *rsa.PrivateKey) { | ||
k.priv = key | ||
} | ||
|
||
// Function to Generate and store a private RSA key and PEM | ||
func (k *PrivateKey) Generate(size KeySize) (cert []byte) { | ||
reader := rand.Reader | ||
priv, err := rsa.GenerateKey(reader, int(size)) | ||
if err != nil { | ||
logger.Log.Fatal().Err(err) | ||
} | ||
|
||
err = priv.Validate() | ||
if err != nil { | ||
logger.Log.Fatal().Err(err) | ||
} | ||
|
||
block := &pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Bytes: x509.MarshalPKCS1PrivateKey(priv), | ||
} | ||
cert = pem.EncodeToMemory(block) | ||
|
||
k.SetKey(priv) | ||
k.SetPEM(cert) | ||
return | ||
} | ||
|
||
func NewPrivateKey(size KeySize) *PrivateKey { | ||
k := &PrivateKey{} | ||
|
||
k.Generate(size) | ||
return k | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.