Compare commits
4 Commits
7cb3636328
...
ca6a51bf36
| Author | SHA1 | Date | |
|---|---|---|---|
| ca6a51bf36 | |||
| aea7050a72 | |||
| c2f301a6b1 | |||
| 950e4fb30e |
@@ -6,8 +6,8 @@ import (
|
|||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/k4lipso/pentapass/rpc"
|
"github.com/k4lipso/pentapass/internal/rpc"
|
||||||
"github.com/k4lipso/pentapass/crypto"
|
"github.com/k4lipso/pentapass/internal/crypto"
|
||||||
. "github.com/k4lipso/pentapass/internal/log"
|
. "github.com/k4lipso/pentapass/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ import (
|
|||||||
ipfslite "github.com/hsanjuan/ipfs-lite"
|
ipfslite "github.com/hsanjuan/ipfs-lite"
|
||||||
badger "github.com/ipfs/go-ds-badger2"
|
badger "github.com/ipfs/go-ds-badger2"
|
||||||
|
|
||||||
"github.com/k4lipso/pentapass/storage"
|
"github.com/k4lipso/pentapass/internal/storage"
|
||||||
"github.com/k4lipso/pentapass/rpc"
|
"github.com/k4lipso/pentapass/internal/rpc"
|
||||||
"github.com/k4lipso/pentapass/crypto/age"
|
"github.com/k4lipso/pentapass/internal/crypto/age"
|
||||||
. "github.com/k4lipso/pentapass/internal/log"
|
. "github.com/k4lipso/pentapass/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -10,9 +10,9 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/k4lipso/pentapass/storage"
|
"github.com/k4lipso/pentapass/internal/storage"
|
||||||
"github.com/k4lipso/pentapass/crypto"
|
"github.com/k4lipso/pentapass/internal/crypto"
|
||||||
"github.com/k4lipso/pentapass/crypto/age"
|
"github.com/k4lipso/pentapass/internal/crypto/age"
|
||||||
. "github.com/k4lipso/pentapass/internal/log"
|
. "github.com/k4lipso/pentapass/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,8 +34,8 @@ import (
|
|||||||
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
|
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
|
||||||
|
|
||||||
agelib "filippo.io/age"
|
agelib "filippo.io/age"
|
||||||
password "github.com/k4lipso/pentapass/crypto"
|
password "github.com/k4lipso/pentapass/internal/crypto"
|
||||||
"github.com/k4lipso/pentapass/crypto/age"
|
"github.com/k4lipso/pentapass/internal/crypto/age"
|
||||||
. "github.com/k4lipso/pentapass/internal/log"
|
. "github.com/k4lipso/pentapass/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
Reference in New Issue
Block a user