refactoring
This commit is contained in:
1 parent
c94a2042f1
commit
c9f416dbd3
7 files changed
+36
-19
No files matched your search
@@ -1,18 +1,19 @@
|
|||||||
package main
|
package keygenerator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/didiercrunch/ecrypto/keys"
|
||||||
"github.com/didiercrunch/ecrypto/shared"
|
"github.com/didiercrunch/ecrypto/shared"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
type KeyGenerator struct {
|
type KeyGenerator struct {
|
||||||
publicKey *PublicKey
|
publicKey *keys.PublicKey
|
||||||
privateKey *PrivateKey
|
privateKey *keys.PrivateKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *KeyGenerator) ensureDirectoryExists(dir string) error {
|
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())
|
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())
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
package main
|
package keygenerator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/didiercrunch/ecrypto/keys"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@@ -43,7 +44,7 @@ func TestSaveKeyAsJSON(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pk := new(PrivateKey)
|
pk := new(keys.PrivateKey)
|
||||||
if err := json.Unmarshal(data, pk); err != nil {
|
if err := json.Unmarshal(data, pk); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
package main
|
package keys
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/didiercrunch/ecrypto/shared"
|
||||||
"math/big"
|
"math/big"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -143,7 +144,7 @@ func GetDefaultRSAPublicKey(publicKey *rsa.PublicKey) *PublicKey {
|
|||||||
DEFAULT_BLOCK_CYPHER_MODE,
|
DEFAULT_BLOCK_CYPHER_MODE,
|
||||||
DEFAULT_HASH_FUNCTION,
|
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) {
|
func GetDefaultRSAPrivateKey(publicKey *rsa.PrivateKey) (*PrivateKey, error) {
|
||||||
@@ -156,5 +157,5 @@ func GetDefaultRSAPrivateKey(publicKey *rsa.PrivateKey) (*PrivateKey, error) {
|
|||||||
DEFAULT_BLOCK_CYPHER_MODE,
|
DEFAULT_BLOCK_CYPHER_MODE,
|
||||||
DEFAULT_HASH_FUNCTION,
|
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 (
|
import (
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
@@ -3,20 +3,34 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
|
"github.com/didiercrunch/ecrypto/keygenerator"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
const VERSION = "0.0.1"
|
|
||||||
|
|
||||||
var createKeyCommand = cli.Command{
|
var createKeyCommand = cli.Command{
|
||||||
Name: "createkey",
|
Name: "createkey",
|
||||||
Usage: "create a pair of public/private key",
|
Usage: "create a pair of public/private key",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.IntFlag{Name: "size, s", Value: 2048, Usage: "key size in bits"},
|
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) {
|
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")
|
fmt.Println("creating key")
|
||||||
if err := keyGenerator.CreateNewKey(c.Int("size")); err != nil {
|
if err := keyGenerator.CreateNewKey(c.Int("size")); err != nil {
|
||||||
fmt.Println("error while creating the key\n", err)
|
fmt.Println("error while creating the key\n", err)
|
||||||
@@ -30,6 +44,7 @@ func main() {
|
|||||||
app.Usage = "encrypt files"
|
app.Usage = "encrypt files"
|
||||||
app.Commands = []cli.Command{
|
app.Commands = []cli.Command{
|
||||||
createKeyCommand,
|
createKeyCommand,
|
||||||
|
encryptFileCommand,
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Run(os.Args)
|
app.Run(os.Args)
|
||||||
|
|||||||
+1
-4
@@ -12,14 +12,11 @@ func TestCreateKeyCommand(t *testing.T) {
|
|||||||
if c.Int("size") != 22 {
|
if c.Int("size") != 22 {
|
||||||
t.Error("wrong size")
|
t.Error("wrong size")
|
||||||
}
|
}
|
||||||
if c.String("password") != "money" {
|
|
||||||
t.Error("wrong password")
|
|
||||||
}
|
|
||||||
hasBeenCalled = true
|
hasBeenCalled = true
|
||||||
}
|
}
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Commands = []cli.Command{c}
|
app.Commands = []cli.Command{c}
|
||||||
args := []string{"file", "createkey", "-size", "22", "-password", "money"}
|
args := []string{"file", "createkey", "-size", "22"}
|
||||||
app.Run(args)
|
app.Run(args)
|
||||||
if !hasBeenCalled {
|
if !hasBeenCalled {
|
||||||
t.Error("sub command *createkey* has not been called")
|
t.Error("sub command *createkey* has not been called")
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const VERSION = "0.0.1"
|
||||||
|
|
||||||
var ecryptoDir string
|
var ecryptoDir string
|
||||||
|
|
||||||
func ensureEcryptoDirectoryIsOkay(dir string) error {
|
func ensureEcryptoDirectoryIsOkay(dir string) error {
|
||||||
|
|||||||
Reference in new issue
Block a user