You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.0 KiB
38 lines
1.0 KiB
package util
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
var privateKey []byte
|
|
|
|
// CheckForPrivateKey checks if already an private key exists, if not it will be randomly generated
|
|
func CheckForPrivateKey() error {
|
|
privateDatPath := filepath.Join(GetDataDir(), "private.dat")
|
|
privateDatContent, err := ioutil.ReadFile(privateDatPath)
|
|
if err == nil {
|
|
privateKey = privateDatContent
|
|
} else if os.IsNotExist(err) {
|
|
randomGeneratedKey := make([]byte, 256)
|
|
if _, err := rand.Read(randomGeneratedKey); err != nil {
|
|
return errors.Wrap(err, "could not read random bytes")
|
|
}
|
|
if err = ioutil.WriteFile(privateDatPath, randomGeneratedKey, 0644); err != nil {
|
|
return errors.Wrap(err, "could not write private key")
|
|
}
|
|
privateKey = randomGeneratedKey
|
|
} else if err != nil {
|
|
return errors.Wrap(err, "could not read private key")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetPrivateKey returns the private key from the loaded private key
|
|
func GetPrivateKey() []byte {
|
|
return privateKey
|
|
}
|
|
|