going somewhere
This commit is contained in:
1 parent
6b3c99664c
commit
209af0d820
16 files changed
+549
-131
No files matched your search
@@ -0,0 +1,98 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/didiercrunch/filou/keys"
|
||||
"github.com/didiercrunch/filou/shared"
|
||||
)
|
||||
|
||||
type ContractsGenerator struct {
|
||||
publicKey *keys.RSAPublicKey
|
||||
privateKey *keys.RSAPrivateKey
|
||||
|
||||
privateContract *PrivateContract
|
||||
publicContract *PublicContract
|
||||
}
|
||||
|
||||
func (this *ContractsGenerator) 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 *ContractsGenerator) saveContractAsJSON(contract interface{}, filepath string) error {
|
||||
w, err := os.Create(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(contract)
|
||||
}
|
||||
|
||||
func (this *ContractsGenerator) saveContracts(dirPath string) error {
|
||||
if err := this.ensureDirectoryExists(dirPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := this.saveContractAsJSON(this.publicContract, path.Join(dirPath, "public_contract.json")); err != nil {
|
||||
return err
|
||||
}
|
||||
return this.saveContractAsJSON(this.privateContract, path.Join(dirPath, "private_contract.json"))
|
||||
}
|
||||
|
||||
func (this *ContractsGenerator) createRSAKeys(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())
|
||||
}
|
||||
|
||||
this.privateKey = keys.NewRSAPrivateKey(rsaPrivateKey)
|
||||
this.publicKey = keys.NewRSAPublicKey(&rsaPrivateKey.PublicKey)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *ContractsGenerator) createPrivateContractWithGoodDefaultValues(key *keys.RSAPrivateKey) {
|
||||
this.privateContract = new(PrivateContract)
|
||||
this.privateContract.AcceptedAsynchronousEncryptionScheme = []string{"rsa_oaep"}
|
||||
this.privateContract.AcceptedSignatureScheme = []string{"rsa_pss"}
|
||||
this.privateContract.AcceptedBlockCypherModes = []string{"ofb"}
|
||||
this.privateContract.AcceptedBlockCyphers = []string{"aes"}
|
||||
this.privateContract.RSAPrivateKey = []*keys.RSAPrivateKey{key}
|
||||
this.privateContract.RSAPublicKey = []*keys.RSAPublicKey{}
|
||||
}
|
||||
|
||||
func (this *ContractsGenerator) createPublicContractWithGoodDefaultValues(key *keys.RSAPublicKey) {
|
||||
this.publicContract = new(PublicContract)
|
||||
this.privateContract.AcceptedAsynchronousEncryptionScheme = []string{"rsa_oaep"}
|
||||
this.privateContract.AcceptedSignatureScheme = []string{"rsa_pss"}
|
||||
this.privateContract.AcceptedBlockCypherModes = []string{"ofb"}
|
||||
this.privateContract.AcceptedBlockCyphers = []string{"aes"}
|
||||
this.privateContract.RSAPublicKey = []*keys.RSAPublicKey{key}
|
||||
}
|
||||
|
||||
func (this *ContractsGenerator) createContracts(keySize int) error {
|
||||
if err := this.createRSAKeys(keySize); err != nil {
|
||||
return err
|
||||
}
|
||||
this.createPrivateContractWithGoodDefaultValues(this.privateKey)
|
||||
this.createPublicContractWithGoodDefaultValues(this.publicKey)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *ContractsGenerator) CreateContracts(size int) error {
|
||||
if err := this.createContracts(size); err != nil {
|
||||
return err
|
||||
}
|
||||
return this.saveContracts(shared.GetEcryptoDir())
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateRSAKey(t *testing.T) {
|
||||
kc := new(ContractsGenerator)
|
||||
if err := kc.createRSAKeys(100); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if kc.privateKey == nil {
|
||||
t.Fail()
|
||||
}
|
||||
if kc.publicKey == nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveContractAsJSON(t *testing.T) {
|
||||
cg := new(ContractsGenerator)
|
||||
if err := cg.createContracts(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 := cg.saveContractAsJSON(cg.privateContract, file.Name()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
data, err := ioutil.ReadFile(file.Name())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
privC := new(PrivateContract)
|
||||
if err := json.Unmarshal(data, privC); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(privC, cg.privateContract) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureDirectoryExists(t *testing.T) {
|
||||
kc := new(ContractsGenerator)
|
||||
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