Compare commits

..

4 Commits

Author SHA1 Message Date
ca6a51bf36 mv storage -> internal/storage 2024-10-10 10:38:51 +02:00
aea7050a72 mv rpc -> internal/rpc 2024-10-10 10:37:57 +02:00
c2f301a6b1 mv crypto -> internal/crypto 2024-10-10 10:37:35 +02:00
950e4fb30e delete rsa impl 2024-10-10 10:33:15 +02:00
7 changed files with 10 additions and 157 deletions

View File

@@ -6,8 +6,8 @@ import (
"github.com/spf13/cobra"
"github.com/k4lipso/pentapass/rpc"
"github.com/k4lipso/pentapass/crypto"
"github.com/k4lipso/pentapass/internal/rpc"
"github.com/k4lipso/pentapass/internal/crypto"
. "github.com/k4lipso/pentapass/internal/log"
)

View File

@@ -8,9 +8,9 @@ import (
ipfslite "github.com/hsanjuan/ipfs-lite"
badger "github.com/ipfs/go-ds-badger2"
"github.com/k4lipso/pentapass/storage"
"github.com/k4lipso/pentapass/rpc"
"github.com/k4lipso/pentapass/crypto/age"
"github.com/k4lipso/pentapass/internal/storage"
"github.com/k4lipso/pentapass/internal/rpc"
"github.com/k4lipso/pentapass/internal/crypto/age"
. "github.com/k4lipso/pentapass/internal/log"
)

View File

@@ -1,147 +0,0 @@
package rsa
import (
"fmt"
"errors"
"os"
"log"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
)
var (
privateKeyName = "rsa_private_key.pem"
publicKeyName = "rsa_public_key.pem"
)
func GenerateKeys(path string) (*rsa.PrivateKey, *rsa.PublicKey, error) {
// Generate a new RSA key pair
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, nil, err
}
// Save the private key to a file
privateKeyPath := path + privateKeyName
privateKeyFile, err := os.Create(privateKeyPath)
if err != nil {
return nil, nil, err
}
defer privateKeyFile.Close()
privateKeyPEM := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
})
_, err = privateKeyFile.Write(privateKeyPEM)
if err != nil {
return nil, nil, err
}
fmt.Printf("Private key saved to %s\n", privateKeyPath)
// Extract and save the public key
publicKeyPath := path + publicKeyName
publicKeyFile, err := os.Create(publicKeyPath)
if err != nil {
return nil, nil, err
}
defer publicKeyFile.Close()
publicKey := &privateKey.PublicKey
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return nil, nil, err
}
publicKeyPEM := pem.EncodeToMemory(&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: publicKeyBytes,
})
_, err = publicKeyFile.Write(publicKeyPEM)
if err != nil {
return nil, nil, err
}
fmt.Printf("Public key saved to %s\n", publicKeyPath)
return privateKey, publicKey, nil
}
func LoadKeys(path string) (*rsa.PrivateKey, *rsa.PublicKey, error) {
// Load private key from file
privateKeyPath := path + privateKeyName
privateKeyData, err := os.ReadFile(privateKeyPath)
if err != nil {
log.Fatalf("Failed to read private key: %v", err)
return nil, nil, err
}
block, _ := pem.Decode(privateKeyData)
if block == nil || block.Type != "RSA PRIVATE KEY" {
log.Fatalf("Failed to decode PEM block containing private key")
return nil, nil, err
}
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
log.Fatalf("Failed to parse private key: %v", err)
return nil, nil, err
}
// Load public key from file
publicKeyPath := path + publicKeyName
publicKeyData, err := os.ReadFile(publicKeyPath)
if err != nil {
log.Fatalf("Failed to read public key: %v", err)
return nil, nil, err
}
block, _ = pem.Decode(publicKeyData)
if block == nil || block.Type != "RSA PUBLIC KEY" {
log.Fatalf("Failed to decode PEM block containing public key")
return nil, nil, err
}
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
log.Fatalf("Failed to parse public key: %v", err)
return nil, nil, err
}
publicKey := publicKeyInterface.(*rsa.PublicKey)
return privateKey, publicKey, nil
}
func LoadOrGenerateKeys(path string) (*rsa.PrivateKey, *rsa.PublicKey, error) {
_, err := os.Open(path + privateKeyName)
if errors.Is(err, os.ErrNotExist) {
fmt.Println("Not Exists - Generate Keys")
return GenerateKeys(path)
}
return LoadKeys(path)
}
func Encrypt(data []byte, key *rsa.PublicKey) ([]byte, error) {
ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, key, data)
if err != nil {
log.Fatalf("Failed to encrypt: %v", err)
return nil, err
}
return ciphertext, nil
}
func Decrypt(data []byte, key *rsa.PrivateKey) ([]byte, error) {
decrypted, err := rsa.DecryptPKCS1v15(rand.Reader, key, data)
if err != nil {
log.Fatalf("Failed to decrypt: %v", err)
return nil, err
}
return decrypted, nil
}

View File

@@ -10,9 +10,9 @@ import (
"os/signal"
"syscall"
"github.com/k4lipso/pentapass/storage"
"github.com/k4lipso/pentapass/crypto"
"github.com/k4lipso/pentapass/crypto/age"
"github.com/k4lipso/pentapass/internal/storage"
"github.com/k4lipso/pentapass/internal/crypto"
"github.com/k4lipso/pentapass/internal/crypto/age"
. "github.com/k4lipso/pentapass/internal/log"
)

View File

@@ -34,8 +34,8 @@ import (
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
agelib "filippo.io/age"
password "github.com/k4lipso/pentapass/crypto"
"github.com/k4lipso/pentapass/crypto/age"
password "github.com/k4lipso/pentapass/internal/crypto"
"github.com/k4lipso/pentapass/internal/crypto/age"
. "github.com/k4lipso/pentapass/internal/log"
)