inital working commit

This commit is contained in:
2024-04-20 10:33:41 -04:00
commit 1afe0716f5
17 changed files with 1179 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
package commands
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/siteworxpro/rsa-file-encryption/printer"
"os"
)
func GenerateKeypair(bitSize uint, path string, overwrite bool) error {
if bitSize == 0 {
bitSize = 4096
}
if bitSize < 2048 {
return fmt.Errorf("key to weak. size must be greater than 2048")
}
if bitSize > 16384 {
return fmt.Errorf("key to large. size must be less than 16384")
}
if _, err := os.Stat(path); err == nil && !overwrite {
return fmt.Errorf("key file already exists - use another filename or -force (-F) to overwrite")
}
p := printer.NewPrinter()
c := make(chan bool)
go p.LogSpinner("Generating RSA key...", c)
key, err := rsa.GenerateKey(rand.Reader, int(bitSize))
c <- true
if err != nil {
return err
}
pub := key.Public()
keyPEM := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
},
)
pubPEM := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(pub.(*rsa.PublicKey)),
},
)
p.LogInfo("Writing private key...")
err = os.WriteFile(path, keyPEM, 0600)
if err != nil {
return err
}
p.LogInfo("Writing public key...")
err = os.WriteFile(path+".pub", pubPEM, 0644)
if err != nil {
return err
}
p.LogSuccess("Done!")
return nil
}