You've already forked rsa-file-encryption
inital working commit
This commit is contained in:
53
commands/decrypt.go
Normal file
53
commands/decrypt.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/siteworxpro/rsa-file-encryption/crypt"
|
||||
"github.com/siteworxpro/rsa-file-encryption/printer"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Decrypt(privateKeyPath string, filePath string, outFile string, force bool) error {
|
||||
|
||||
if _, err := os.Stat(privateKeyPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filePath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(outFile); err == nil && !force {
|
||||
return fmt.Errorf("decrypted file already exists (--force, -F) to overwrite")
|
||||
}
|
||||
|
||||
p := printer.NewPrinter()
|
||||
encryptedFile := crypt.EncryptedFile{}
|
||||
|
||||
p.LogInfo("Reading Private Key...")
|
||||
err := encryptedFile.OsReadPrivateKey(privateKeyPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.LogInfo("Reading and decrypting file...")
|
||||
c := make(chan bool)
|
||||
go p.LogSpinner("Decrypting...", c)
|
||||
|
||||
err = encryptedFile.OsReadCipherTextFile(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c <- true
|
||||
|
||||
p.LogInfo("Writing un-encrypted file...")
|
||||
err = encryptedFile.WriteDecryptedFileToDisk(outFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.LogSuccess("Done!")
|
||||
|
||||
return nil
|
||||
}
|
||||
68
commands/encrypt.go
Normal file
68
commands/encrypt.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/siteworxpro/rsa-file-encryption/crypt"
|
||||
"github.com/siteworxpro/rsa-file-encryption/printer"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Encrypt(publicKeyPath string, filePath string, force bool) error {
|
||||
|
||||
if _, err := os.Stat(publicKeyPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filePath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filePath + ".enc"); err == nil && !force {
|
||||
return fmt.Errorf("encrypted file already exists (--force, -F) to overwrite")
|
||||
}
|
||||
|
||||
p := printer.NewPrinter()
|
||||
encryptedFile := crypt.EncryptedFile{}
|
||||
|
||||
p.LogInfo("Reading public key...")
|
||||
err := encryptedFile.OsReadPublicKey(publicKeyPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
size := encryptedFile.PublicKey.Size()
|
||||
if size < 256 {
|
||||
return fmt.Errorf("key to weak. use stronger key > 2048 bits")
|
||||
}
|
||||
|
||||
p.LogInfo("Reading file to encrypt...")
|
||||
err = encryptedFile.OsReadPlainTextFile(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c := make(chan bool)
|
||||
go p.LogSpinner("Encrypting...", c)
|
||||
|
||||
err = encryptedFile.GenerateSymmetricKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = encryptedFile.EncryptFile()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c <- true
|
||||
|
||||
p.LogInfo("Encrypted file successfully")
|
||||
p.LogInfo("Writing file...")
|
||||
err = encryptedFile.WriteEncryptFileToDisk(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.LogSuccess("Done!")
|
||||
return nil
|
||||
}
|
||||
71
commands/generate-keypair.go
Normal file
71
commands/generate-keypair.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user