feat(init): First commit
This commit is contained in:
commit
f92368748a
22 changed files with 1298 additions and 0 deletions
31
pkg/crypto/key.go
Normal file
31
pkg/crypto/key.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/pem"
|
||||
)
|
||||
|
||||
func GenerateKeyPair(bytes int) (string, string, error) {
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, bytes)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
err = privateKey.Validate()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
privBlock := pem.Block{
|
||||
Type: "PRIVATE KEY",
|
||||
Headers: nil,
|
||||
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
||||
}
|
||||
|
||||
privateKeyPEM := pem.EncodeToMemory(&privBlock)
|
||||
|
||||
return string(privateKeyPEM), string(base64.StdEncoding.EncodeToString(x509.MarshalPKCS1PublicKey(&privateKey.PublicKey))), nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue