-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e148114
commit d3990a5
Showing
4 changed files
with
81 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
"golang.org/x/crypto/ssh" | ||
"strings" | ||
) | ||
|
||
func CheckPrivateKey(privateKey, publicKey string) bool { | ||
//check key | ||
pub, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKey)) | ||
if err != nil { | ||
fmt.Println("error in parsing public key") | ||
return false | ||
} | ||
|
||
private, err := ssh.ParsePrivateKey([]byte(privateKey)) | ||
if err != nil { | ||
fmt.Println("error in parsing private key") | ||
return false | ||
} | ||
|
||
return bytes.Equal(private.PublicKey().Marshal(), pub.Marshal()) | ||
} | ||
|
||
func GenerateSSHKeyPair() (string, string, error) { | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 1024) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
// generate and write private key as PEM | ||
var privKeyBuf strings.Builder | ||
|
||
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)} | ||
if err := pem.Encode(&privKeyBuf, privateKeyPEM); err != nil { | ||
return "", "", err | ||
} | ||
|
||
// generate and write public key | ||
pub, err := ssh.NewPublicKey(&privateKey.PublicKey) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
var pubKeyBuf strings.Builder | ||
pubKeyBuf.Write(ssh.MarshalAuthorizedKey(pub)) | ||
|
||
return pubKeyBuf.String(), privKeyBuf.String(), nil | ||
} |
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