goint there
This commit is contained in:
1 parent
776c73b91c
commit
c94a2042f1
7 files changed
+292
-36
No files matched your search
@@ -3,7 +3,11 @@ package main
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/didiercrunch/ecrypto/shared"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
type KeyGenerator struct {
|
||||
@@ -11,6 +15,38 @@ type KeyGenerator struct {
|
||||
privateKey *PrivateKey
|
||||
}
|
||||
|
||||
func (this *KeyGenerator) ensureDirectoryExists(dir string) error {
|
||||
stat, err := os.Stat(dir)
|
||||
if os.IsNotExist(err) {
|
||||
return os.Mkdir(dir, 0700)
|
||||
} else if err != nil {
|
||||
return err
|
||||
} else if !stat.IsDir() {
|
||||
return errors.New(dir + " is not a directory")
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (this *KeyGenerator) saveKeyAsJSON(key interface{}, filepath string) error {
|
||||
w, err := os.Create(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(key)
|
||||
}
|
||||
|
||||
func (this *KeyGenerator) saveKeys(dirPath string) error {
|
||||
if err := this.ensureDirectoryExists(dirPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := this.saveKeyAsJSON(this.publicKey, path.Join(dirPath, "publickey.json")); err != nil {
|
||||
return err
|
||||
}
|
||||
return this.saveKeyAsJSON(this.privateKey, path.Join(dirPath, "privatekey.json"))
|
||||
}
|
||||
|
||||
func (this *KeyGenerator) createRSAKey(size int) error {
|
||||
rsaPrivateKey, err := rsa.GenerateKey(rand.Reader, size)
|
||||
if err != nil {
|
||||
@@ -24,9 +60,9 @@ func (this *KeyGenerator) createRSAKey(size int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *KeyGenerator) CreateNewKey(size int, password string) error {
|
||||
func (this *KeyGenerator) CreateNewKey(size int) error {
|
||||
if err := this.createRSAKey(size); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return this.saveKeys(shared.GetEcryptoDir())
|
||||
}
|
||||
Reference in new issue
Block a user