You've already forked rsa-file-encryption
Added test for crypt (#1)
* Added test for crypt * Added test for crypt * Added test for crypt * Added test for crypt * Added test for crypt
This commit is contained in:
27
.github/workflows/test.yml
vendored
Normal file
27
.github/workflows/test.yml
vendored
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
name: Go Tests
|
||||||
|
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: [ '1.21.x' ]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Setup Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.go-version }}
|
||||||
|
- name: Install dependencies
|
||||||
|
run: go get .
|
||||||
|
- name: Test with Go
|
||||||
|
run: go test ./... -json -cover > TestResults-${{ matrix.go-version }}.json
|
||||||
|
- name: Upload Go test results
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: Go-results-${{ matrix.go-version }}
|
||||||
|
path: TestResults-${{ matrix.go-version }}.json
|
||||||
@@ -1,27 +1,13 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
|
||||||
"crypto/rsa"
|
|
||||||
"crypto/x509"
|
|
||||||
"encoding/pem"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/siteworxpro/rsa-file-encryption/crypt"
|
||||||
"github.com/siteworxpro/rsa-file-encryption/printer"
|
"github.com/siteworxpro/rsa-file-encryption/printer"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateKeypair(bitSize uint, path string, overwrite bool) error {
|
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 {
|
if _, err := os.Stat(path); err == nil && !overwrite {
|
||||||
return fmt.Errorf("key file already exists - use another filename or -force (-F) to overwrite")
|
return fmt.Errorf("key file already exists - use another filename or -force (-F) to overwrite")
|
||||||
@@ -31,41 +17,24 @@ func GenerateKeypair(bitSize uint, path string, overwrite bool) error {
|
|||||||
c := make(chan bool)
|
c := make(chan bool)
|
||||||
|
|
||||||
go p.LogSpinner("Generating RSA key...", c)
|
go p.LogSpinner("Generating RSA key...", c)
|
||||||
key, err := rsa.GenerateKey(rand.Reader, int(bitSize))
|
|
||||||
|
keyPem, pubPem, err := crypt.GenerateKeyPair(int(bitSize))
|
||||||
|
|
||||||
c <- true
|
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...")
|
p.LogInfo("Writing private key...")
|
||||||
err = os.WriteFile(path, keyPEM, 0600)
|
err = os.WriteFile(path, keyPem, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.LogInfo("Writing public key...")
|
p.LogInfo("Writing public key...")
|
||||||
err = os.WriteFile(path+".pub", pubPEM, 0644)
|
err = os.WriteFile(path+".pub", pubPem, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.LogSuccess("Done!")
|
p.LogSuccess("Done!")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
91
crypt/crypt_test.go
Normal file
91
crypt/crypt_test.go
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
package crypt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLimitsKeySize(t *testing.T) {
|
||||||
|
_, _, err := GenerateKeyPair(512)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("GenerateKeyPair returned no error")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err = GenerateKeyPair(16385)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("GenerateKeyPair returned no error")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err = GenerateKeyPair(0)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("GenerateKeyPair returned an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncryption(t *testing.T) {
|
||||||
|
data := []byte("hello world")
|
||||||
|
|
||||||
|
keyPem, pubPem, err := GenerateKeyPair(2048)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ef := EncryptedFile{
|
||||||
|
plainText: data,
|
||||||
|
PublicPem: pubPem,
|
||||||
|
privatePem: keyPem,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ef.ParsePublicPem()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ef.ParsePrivatePem()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ef.GenerateSymmetricKey()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ef.EncryptFile()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ef.ciphertext) == 0 {
|
||||||
|
t.Error("ciphertext is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ef.nonce) == 0 {
|
||||||
|
t.Error("nonce is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
if bytes.Equal(ef.plainText, ef.ciphertext) {
|
||||||
|
t.Error("ciphertext and plaintext are the same")
|
||||||
|
}
|
||||||
|
|
||||||
|
dc := EncryptedFile{
|
||||||
|
ciphertext: ef.ciphertext,
|
||||||
|
privatePem: keyPem,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = dc.ParsePrivatePem()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = dc.unpackFileAndDecrypt(ef.packFile())
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(ef.plainText, dc.plainText) {
|
||||||
|
t.Error("plaintext and plaintext are different")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
47
crypt/pem.go
Normal file
47
crypt/pem.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package crypt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateKeyPair(bitSize int) ([]byte, []byte, error) {
|
||||||
|
if bitSize == 0 {
|
||||||
|
bitSize = 4096
|
||||||
|
}
|
||||||
|
|
||||||
|
if bitSize < 2048 {
|
||||||
|
return nil, nil, fmt.Errorf("key to weak. size must be greater than 2048")
|
||||||
|
}
|
||||||
|
|
||||||
|
if bitSize > 16384 {
|
||||||
|
return nil, nil, fmt.Errorf("key to large. size must be less than 16384")
|
||||||
|
}
|
||||||
|
|
||||||
|
key, err := rsa.GenerateKey(rand.Reader, bitSize)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
keyPEM := pem.EncodeToMemory(
|
||||||
|
&pem.Block{
|
||||||
|
Type: "RSA PRIVATE KEY",
|
||||||
|
Bytes: x509.MarshalPKCS1PrivateKey(key),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
pub := key.Public()
|
||||||
|
|
||||||
|
pubPEM := pem.EncodeToMemory(
|
||||||
|
&pem.Block{
|
||||||
|
Type: "RSA PUBLIC KEY",
|
||||||
|
Bytes: x509.MarshalPKCS1PublicKey(pub.(*rsa.PublicKey)),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return keyPEM, pubPEM, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user