crypto
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package tools
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
@@ -14,16 +13,12 @@ import (
|
|||||||
|
|
||||||
func LoadKeyFromFilePrivate() (crypto.PrivKey, error) {
|
func LoadKeyFromFilePrivate() (crypto.PrivKey, error) {
|
||||||
path := config.GetConfig().PrivateKeyPath
|
path := config.GetConfig().PrivateKeyPath
|
||||||
|
fmt.Println(path)
|
||||||
data, err := os.ReadFile(path)
|
data, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
block, _ := pem.Decode(data)
|
block, _ := pem.Decode(data)
|
||||||
if block == nil {
|
|
||||||
return nil, fmt.Errorf("failed to decode PEM")
|
|
||||||
}
|
|
||||||
|
|
||||||
keyAny, err := x509.ParsePKCS8PrivateKey(block.Bytes)
|
keyAny, err := x509.ParsePKCS8PrivateKey(block.Bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -33,31 +28,16 @@ func LoadKeyFromFilePrivate() (crypto.PrivKey, error) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("not an ed25519 key")
|
return nil, fmt.Errorf("not an ed25519 key")
|
||||||
}
|
}
|
||||||
|
return crypto.UnmarshalEd25519PrivateKey(edKey)
|
||||||
// Convert properly to libp2p key
|
|
||||||
privKey, _, err := crypto.GenerateEd25519Key(
|
|
||||||
bytes.NewReader(edKey.Seed()),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return privKey, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadKeyFromFilePublic() (crypto.PubKey, error) {
|
func LoadKeyFromFilePublic() (crypto.PubKey, error) {
|
||||||
path := config.GetConfig().PublicKeyPath
|
path := config.GetConfig().PublicKeyPath
|
||||||
|
|
||||||
data, err := os.ReadFile(path)
|
data, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
block, _ := pem.Decode(data)
|
block, _ := pem.Decode(data)
|
||||||
if block == nil {
|
|
||||||
return nil, fmt.Errorf("failed to decode PEM")
|
|
||||||
}
|
|
||||||
|
|
||||||
keyAny, err := x509.ParsePKIXPublicKey(block.Bytes)
|
keyAny, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -67,12 +47,6 @@ func LoadKeyFromFilePublic() (crypto.PubKey, error) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("not an ed25519 key")
|
return nil, fmt.Errorf("not an ed25519 key")
|
||||||
}
|
}
|
||||||
|
// Try to unmarshal as libp2p private key (supports ed25519, rsa, etc.)
|
||||||
// Convert Go ed25519 key to libp2p key
|
return crypto.UnmarshalEd25519PublicKey(edKey)
|
||||||
pubKey, err := crypto.UnmarshalEd25519PublicKey(edKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return pubKey, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user