refactoring

This commit is contained in:
didier.amyot committed 2014-07-30 15:46:07 -04:00
1 parent c94a2042f1
commit c9f416dbd3
7 files changed
+36 -19

No files matched your search

+6 -5
View File
@@ -1,18 +1,19 @@
package main
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 *PublicKey
privateKey *PrivateKey
publicKey *keys.PublicKey
privateKey *keys.PrivateKey
}
func (this *KeyGenerator) ensureDirectoryExists(dir string) error {
@@ -53,10 +54,10 @@ func (this *KeyGenerator) createRSAKey(size int) error {
return errors.New("cannot generate the rsa public/private key pair\n" + err.Error())
}
if this.privateKey, err = GetDefaultRSAPrivateKey(rsaPrivateKey); err != nil {
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 = GetDefaultRSAPublicKey(&rsaPrivateKey.PublicKey)
this.publicKey = keys.GetDefaultRSAPublicKey(&rsaPrivateKey.PublicKey)
return nil
}
+3 -2
View File
@@ -1,7 +1,8 @@
package main
package keygenerator
import (
"encoding/json"
"github.com/didiercrunch/ecrypto/keys"
"io/ioutil"
"os"
"path"
@@ -43,7 +44,7 @@ func TestSaveKeyAsJSON(t *testing.T) {
t.Error(err)
return
}
pk := new(PrivateKey)
pk := new(keys.PrivateKey)
if err := json.Unmarshal(data, pk); err != nil {
t.Error(err)
return
+4 -3
View File
@@ -1,10 +1,11 @@
package main
package keys
import (
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"github.com/didiercrunch/ecrypto/shared"
"math/big"
)
@@ -143,7 +144,7 @@ func GetDefaultRSAPublicKey(publicKey *rsa.PublicKey) *PublicKey {
DEFAULT_BLOCK_CYPHER_MODE,
DEFAULT_HASH_FUNCTION,
}
return &PublicKey{Type: "RSA", Version: VERSION, Key: key, Accept: accept}
return &PublicKey{Type: "RSA", Version: shared.VERSION, Key: key, Accept: accept}
}
func GetDefaultRSAPrivateKey(publicKey *rsa.PrivateKey) (*PrivateKey, error) {
@@ -156,5 +157,5 @@ func GetDefaultRSAPrivateKey(publicKey *rsa.PrivateKey) (*PrivateKey, error) {
DEFAULT_BLOCK_CYPHER_MODE,
DEFAULT_HASH_FUNCTION,
}
return &PrivateKey{Type: "RSA", Version: VERSION, Key: key, Accept: accept}, nil
return &PrivateKey{Type: "RSA", Version: shared.VERSION, Key: key, Accept: accept}, nil
}
@@ -1,4 +1,4 @@
package main
package keys
import (
"crypto/rsa"
+19 -4
View File
@@ -3,20 +3,34 @@ package main
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/didiercrunch/ecrypto/keygenerator"
"os"
)
const VERSION = "0.0.1"
var createKeyCommand = cli.Command{
Name: "createkey",
Usage: "create a pair of public/private key",
Flags: []cli.Flag{
cli.IntFlag{Name: "size, s", Value: 2048, Usage: "key size in bits"},
cli.StringFlag{Name: "password, p", Value: "", Usage: "password to access the private key"},
},
Action: func(c *cli.Context) {
keyGenerator := new(KeyGenerator)
keyGenerator := new(keygenerator.KeyGenerator)
fmt.Println("creating key")
if err := keyGenerator.CreateNewKey(c.Int("size")); err != nil {
fmt.Println("error while creating the key\n", err)
}
},
}
var encryptFileCommand = cli.Command{
Name: "encrypt",
Usage: "encrypt file or directory",
Flags: []cli.Flag{
cli.StringFlag{Name: "file, s", Value: "", Usage: "the file or directory to encrypt."},
cli.StringFlag{Name: "publicKey, k", Value: "", Usage: "the public key to use to encrypt"},
},
Action: func(c *cli.Context) {
keyGenerator := new(keygenerator.KeyGenerator)
fmt.Println("creating key")
if err := keyGenerator.CreateNewKey(c.Int("size")); err != nil {
fmt.Println("error while creating the key\n", err)
@@ -30,6 +44,7 @@ func main() {
app.Usage = "encrypt files"
app.Commands = []cli.Command{
createKeyCommand,
encryptFileCommand,
}
app.Run(os.Args)
+1 -4
View File
@@ -12,14 +12,11 @@ func TestCreateKeyCommand(t *testing.T) {
if c.Int("size") != 22 {
t.Error("wrong size")
}
if c.String("password") != "money" {
t.Error("wrong password")
}
hasBeenCalled = true
}
app := cli.NewApp()
app.Commands = []cli.Command{c}
args := []string{"file", "createkey", "-size", "22", "-password", "money"}
args := []string{"file", "createkey", "-size", "22"}
app.Run(args)
if !hasBeenCalled {
t.Error("sub command *createkey* has not been called")
+2
View File
@@ -7,6 +7,8 @@ import (
"path"
)
const VERSION = "0.0.1"
var ecryptoDir string
func ensureEcryptoDirectoryIsOkay(dir string) error {