refactoring
This commit is contained in:
1 parent
c94a2042f1
commit
c9f416dbd3
7 files changed
+36
-19
No files matched your search
@@ -0,0 +1,69 @@
|
||||
package keygenerator
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/didiercrunch/ecrypto/keys"
|
||||
"github.com/didiercrunch/ecrypto/shared"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
type KeyGenerator struct {
|
||||
publicKey *keys.PublicKey
|
||||
privateKey *keys.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 {
|
||||
return errors.New("cannot generate the rsa public/private key pair\n" + err.Error())
|
||||
}
|
||||
|
||||
if this.privateKey, err = keys.GetDefaultRSAPrivateKey(rsaPrivateKey); err != nil {
|
||||
return errors.New("cannot generate the rsa public/private key pair\n" + err.Error())
|
||||
}
|
||||
this.publicKey = keys.GetDefaultRSAPublicKey(&rsaPrivateKey.PublicKey)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *KeyGenerator) CreateNewKey(size int) error {
|
||||
if err := this.createRSAKey(size); err != nil {
|
||||
return err
|
||||
}
|
||||
return this.saveKeys(shared.GetEcryptoDir())
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package keygenerator
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/didiercrunch/ecrypto/keys"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateRSAKey(t *testing.T) {
|
||||
kc := new(KeyGenerator)
|
||||
if err := kc.createRSAKey(100); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if kc.privateKey == nil {
|
||||
t.Fail()
|
||||
}
|
||||
if kc.publicKey == nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveKeyAsJSON(t *testing.T) {
|
||||
kc := new(KeyGenerator)
|
||||
if err := kc.CreateNewKey(100); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
file, err := ioutil.TempFile(os.TempDir(), "prefix")
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
if err := kc.saveKeyAsJSON(kc.privateKey, file.Name()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
data, err := ioutil.ReadFile(file.Name())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
pk := new(keys.PrivateKey)
|
||||
if err := json.Unmarshal(data, pk); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(pk.Key, kc.privateKey.Key) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureDirectoryExists(t *testing.T) {
|
||||
kc := new(KeyGenerator)
|
||||
fooDir := path.Join(os.TempDir(), "fds89d")
|
||||
defer os.Remove(fooDir)
|
||||
if err := kc.ensureDirectoryExists(fooDir); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if _, err := os.Stat(fooDir); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if err := kc.ensureDirectoryExists(fooDir); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user