some work done
This commit is contained in:
1 parent
1230137626
commit
d2cd50b2e3
8 files changed
+150
-132
No files matched your search
@@ -4,10 +4,11 @@ import (
|
|||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
. "github.com/didiercrunch/filou/helper"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/didiercrunch/filou/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewMockAESOFBEncrypter() *AESOFBEncrypter {
|
func NewMockAESOFBEncrypter() *AESOFBEncrypter {
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ import (
|
|||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/didiercrunch/filou/helper"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/didiercrunch/filou/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHashIOReader(t *testing.T) {
|
func TestHashIOReader(t *testing.T) {
|
||||||
|
|||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package keys
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
// All the method to encode/decode keys to json, yaml...
|
||||||
|
|
||||||
|
type encodingRSAKey struct {
|
||||||
|
N string
|
||||||
|
P string
|
||||||
|
Q string
|
||||||
|
D string
|
||||||
|
E int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *encodingRSAKey) CreateFromRSAPrivateKey(key *RSAPrivateKey) *encodingRSAKey {
|
||||||
|
this.N = fmt.Sprintf("%x", key.N)
|
||||||
|
this.P = fmt.Sprintf("%x", key.P)
|
||||||
|
this.Q = fmt.Sprintf("%x", key.Q)
|
||||||
|
this.D = fmt.Sprintf("%x", key.D)
|
||||||
|
this.E = key.E
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *encodingRSAKey) CreateFromRSAPublicKey(key *RSAPublicKey) *encodingRSAKey {
|
||||||
|
this.N = fmt.Sprintf("%x", key.N)
|
||||||
|
this.E = key.E
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *encodingRSAKey) SetRSAPrivateKey(key *RSAPrivateKey) (*RSAPrivateKey, error) {
|
||||||
|
if i, ok := new(big.Int).SetString(this.N, 16); !ok {
|
||||||
|
return nil, errors.New("cannot parse string has hexadecimal string")
|
||||||
|
} else {
|
||||||
|
key.N = i
|
||||||
|
}
|
||||||
|
|
||||||
|
if i, ok := new(big.Int).SetString(this.P, 16); !ok {
|
||||||
|
return nil, errors.New("cannot parse string has hexadecimal string")
|
||||||
|
} else {
|
||||||
|
key.P = i
|
||||||
|
}
|
||||||
|
|
||||||
|
if i, ok := new(big.Int).SetString(this.Q, 16); !ok {
|
||||||
|
return nil, errors.New("cannot parse string has hexadecimal string")
|
||||||
|
} else {
|
||||||
|
key.Q = i
|
||||||
|
}
|
||||||
|
|
||||||
|
if i, ok := new(big.Int).SetString(this.D, 16); !ok {
|
||||||
|
return nil, errors.New("cannot parse string has hexadecimal string")
|
||||||
|
} else {
|
||||||
|
key.D = i
|
||||||
|
}
|
||||||
|
key.E = this.E
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *encodingRSAKey) SetRSAPublicKey(key *RSAPublicKey) (*RSAPublicKey, error) {
|
||||||
|
if i, ok := new(big.Int).SetString(this.N, 16); !ok {
|
||||||
|
return nil, errors.New("cannot parse string has hexadecimal string")
|
||||||
|
} else {
|
||||||
|
key.N = i
|
||||||
|
}
|
||||||
|
key.E = this.E
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RSAPrivateKey) MarshalJSON() ([]byte, error) {
|
||||||
|
e := new(encodingRSAKey)
|
||||||
|
return json.Marshal(e.CreateFromRSAPrivateKey(this))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RSAPrivateKey) UnmarshalJSON(data []byte) error {
|
||||||
|
obj := new(encodingRSAKey)
|
||||||
|
if err := json.Unmarshal(data, obj); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err := obj.SetRSAPrivateKey(this)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RSAPublicKey) MarshalJSON() ([]byte, error) {
|
||||||
|
e := new(encodingRSAKey)
|
||||||
|
return json.Marshal(e.CreateFromRSAPublicKey(this))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *RSAPublicKey) UnmarshalJSON(data []byte) error {
|
||||||
|
obj := new(encodingRSAKey)
|
||||||
|
if err := json.Unmarshal(data, obj); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err := obj.SetRSAPublicKey(this)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package keys
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/didiercrunch/filou/helper"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRSAPrivateKeyJsonification(t *testing.T) {
|
||||||
|
key := &RSAPrivateKey{B(1023), B(2112), B(328723), B(2332), 89}
|
||||||
|
data, err := json.Marshal(key)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
key2 := new(RSAPrivateKey)
|
||||||
|
if err := json.Unmarshal(data, key2); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(key, key2) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRSAPublicKeyJsonification(t *testing.T) {
|
||||||
|
key := &RSAPublicKey{B(1023), 89}
|
||||||
|
data, err := json.Marshal(key)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
key2 := new(RSAPublicKey)
|
||||||
|
if err := json.Unmarshal(data, key2); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(key, key2) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,6 @@ package keys
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"github.com/didiercrunch/filou/shared"
|
"github.com/didiercrunch/filou/shared"
|
||||||
@@ -43,95 +40,6 @@ type RSAPrivateKey struct {
|
|||||||
E int `json:"e,omitempty"`
|
E int `json:"e,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type encodingRSAKey struct {
|
|
||||||
N string
|
|
||||||
P string
|
|
||||||
Q string
|
|
||||||
D string
|
|
||||||
E int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *encodingRSAKey) CreateFromRSAPrivateKey(key *RSAPrivateKey) *encodingRSAKey {
|
|
||||||
this.N = fmt.Sprintf("%x", key.N)
|
|
||||||
this.P = fmt.Sprintf("%x", key.P)
|
|
||||||
this.Q = fmt.Sprintf("%x", key.Q)
|
|
||||||
this.D = fmt.Sprintf("%x", key.D)
|
|
||||||
this.E = key.E
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *encodingRSAKey) CreateFromRSAPublicKey(key *RSAPublicKey) *encodingRSAKey {
|
|
||||||
this.N = fmt.Sprintf("%x", key.N)
|
|
||||||
this.E = key.E
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *encodingRSAKey) SetRSAPrivateKey(key *RSAPrivateKey) (*RSAPrivateKey, error) {
|
|
||||||
if i, ok := new(big.Int).SetString(this.N, 16); !ok {
|
|
||||||
return nil, errors.New("cannot parse string has hexadecimal string")
|
|
||||||
} else {
|
|
||||||
key.N = i
|
|
||||||
}
|
|
||||||
|
|
||||||
if i, ok := new(big.Int).SetString(this.P, 16); !ok {
|
|
||||||
return nil, errors.New("cannot parse string has hexadecimal string")
|
|
||||||
} else {
|
|
||||||
key.P = i
|
|
||||||
}
|
|
||||||
|
|
||||||
if i, ok := new(big.Int).SetString(this.Q, 16); !ok {
|
|
||||||
return nil, errors.New("cannot parse string has hexadecimal string")
|
|
||||||
} else {
|
|
||||||
key.Q = i
|
|
||||||
}
|
|
||||||
|
|
||||||
if i, ok := new(big.Int).SetString(this.D, 16); !ok {
|
|
||||||
return nil, errors.New("cannot parse string has hexadecimal string")
|
|
||||||
} else {
|
|
||||||
key.D = i
|
|
||||||
}
|
|
||||||
key.E = this.E
|
|
||||||
return key, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *encodingRSAKey) SetRSAPublicKey(key *RSAPublicKey) (*RSAPublicKey, error) {
|
|
||||||
if i, ok := new(big.Int).SetString(this.N, 16); !ok {
|
|
||||||
return nil, errors.New("cannot parse string has hexadecimal string")
|
|
||||||
} else {
|
|
||||||
key.N = i
|
|
||||||
}
|
|
||||||
key.E = this.E
|
|
||||||
return key, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *RSAPrivateKey) MarshalJSON() ([]byte, error) {
|
|
||||||
e := new(encodingRSAKey)
|
|
||||||
return json.Marshal(e.CreateFromRSAPrivateKey(this))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *RSAPrivateKey) UnmarshalJSON(data []byte) error {
|
|
||||||
obj := new(encodingRSAKey)
|
|
||||||
if err := json.Unmarshal(data, obj); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err := obj.SetRSAPrivateKey(this)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *RSAPublicKey) MarshalJSON() ([]byte, error) {
|
|
||||||
e := new(encodingRSAKey)
|
|
||||||
return json.Marshal(e.CreateFromRSAPublicKey(this))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *RSAPublicKey) UnmarshalJSON(data []byte) error {
|
|
||||||
obj := new(encodingRSAKey)
|
|
||||||
if err := json.Unmarshal(data, obj); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err := obj.SetRSAPublicKey(this)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
type AcceptMethods struct {
|
type AcceptMethods struct {
|
||||||
BlocCypher string
|
BlocCypher string
|
||||||
BlockCypherMode string
|
BlockCypherMode string
|
||||||
|
|||||||
@@ -2,9 +2,7 @@ package keys
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"encoding/json"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "github.com/didiercrunch/filou/helper"
|
. "github.com/didiercrunch/filou/helper"
|
||||||
@@ -46,37 +44,3 @@ func TestGetDefaultRSAPrivateKey(t *testing.T) {
|
|||||||
t.Error("bad D")
|
t.Error("bad D")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRSAPrivateKeyJsonification(t *testing.T) {
|
|
||||||
key := &RSAPrivateKey{B(1023), B(2112), B(328723), B(2332), 89}
|
|
||||||
data, err := json.Marshal(key)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
key2 := new(RSAPrivateKey)
|
|
||||||
if err := json.Unmarshal(data, key2); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(key, key2) {
|
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRSAPublicKeyJsonification(t *testing.T) {
|
|
||||||
key := &RSAPublicKey{B(1023), 89}
|
|
||||||
data, err := json.Marshal(key)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
key2 := new(RSAPublicKey)
|
|
||||||
if err := json.Unmarshal(data, key2); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(key, key2) {
|
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,9 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
"github.com/didiercrunch/filou/keygenerator"
|
"github.com/didiercrunch/filou/keygenerator"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var createKeyCommand = cli.Command{
|
var createKeyCommand = cli.Command{
|
||||||
|
|||||||
+2
-1
@@ -1,8 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/codegangsta/cli"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/codegangsta/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateKeyCommand(t *testing.T) {
|
func TestCreateKeyCommand(t *testing.T) {
|
||||||
|
|||||||
Reference in new issue
Block a user