SSHSecure/sshkeys/rsa.go

24 lines
493 B
Go
Raw Normal View History

package sshkeys
import (
"crypto/rand"
"crypto/rsa"
)
func (k *SSHPrivKey) generateRsa() error {
if k.BitSize == 0 {
k.BitSize = defRSABitSize
}
if k.Key != nil || k.PublicKey.Key != nil {
return nil // A no-op; key already exists.
}
if sk, err := rsa.GenerateKey(rand.Reader, int(k.BitSize)); err != nil {
return err
} else {
k.Key = sk // See https://golang.org/pkg/crypto/rsa/#PrivateKey
k.PublicKey.KeyType = KeyRsa
k.PublicKey.Key = k.Key.PublicKey
}
return nil
}