52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package tools
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"fmt"
|
|
"os"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/config"
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
|
)
|
|
|
|
func LoadKeyFromFilePrivate() (crypto.PrivKey, error) {
|
|
path := config.GetConfig().PrivateKeyPath
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
block, _ := pem.Decode(data)
|
|
keyAny, err := x509.ParsePKCS8PrivateKey(block.Bytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
edKey, ok := keyAny.(ed25519.PrivateKey)
|
|
if !ok {
|
|
return nil, fmt.Errorf("not an ed25519 key")
|
|
}
|
|
return crypto.UnmarshalEd25519PrivateKey(edKey)
|
|
}
|
|
|
|
func LoadKeyFromFilePublic() (crypto.PubKey, error) {
|
|
path := config.GetConfig().PublicKeyPath
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
block, _ := pem.Decode(data)
|
|
keyAny, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
edKey, ok := keyAny.(ed25519.PublicKey)
|
|
if !ok {
|
|
return nil, fmt.Errorf("not an ed25519 key")
|
|
}
|
|
// Try to unmarshal as libp2p private key (supports ed25519, rsa, etc.)
|
|
return crypto.UnmarshalEd25519PublicKey(edKey)
|
|
}
|