change computer
This commit is contained in:
1 parent
52a13b9cab
commit
6b3c99664c
7 files changed
+97
-11
No files matched your search
@@ -0,0 +1,21 @@
|
|||||||
|
package asymetric
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/didiercrunch/filou/contract"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PublicKeyEncryptor interface {
|
||||||
|
Encrypt(data []byte) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Signer interface {
|
||||||
|
Sign(data []byte) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSigner(contract_ contract.AcceptedContract) (Signer, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPublicKeyEncryptor(contract_ contract.AcceptedContract) (Signer, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package asymetric
|
||||||
@@ -3,14 +3,22 @@ package contract
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"crypto/rsa"
|
||||||
"github.com/didiercrunch/filou/helper"
|
"github.com/didiercrunch/filou/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type GetRSAPrivateKey func() (*rsa.PrivateKey, error)
|
||||||
|
|
||||||
type AcceptedContract struct {
|
type AcceptedContract struct {
|
||||||
Hash string
|
Hash string
|
||||||
BlockCipher string
|
BlockCipher string
|
||||||
BlockCipherMode string
|
BlockCipherMode string
|
||||||
AsymetricCryptography string
|
|
||||||
|
AsynchronousEncryptionScheme string
|
||||||
|
SignatureScheme string
|
||||||
|
|
||||||
|
RsaPublicKey *rsa.PublicKey
|
||||||
|
RsaPrivateKey GetRSAPrivateKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func listContains(lst []string, elm string) bool {
|
func listContains(lst []string, elm string) bool {
|
||||||
@@ -31,6 +39,22 @@ func getFirstItemInList1ThatIsAlsoInList2(lst1, lst2 []string) (string, error) {
|
|||||||
return "", errors.New("no items in list1 is in list2")
|
return "", errors.New("no items in list1 is in list2")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getAsynchronousEncryptionScheme(prvCtr *PrivateContract, pubCtr *PublicContract) (string, error) {
|
||||||
|
if b, err := getFirstItemInList1ThatIsAlsoInList2(pubCtr.AcceptedAsynchronousEncryptionScheme, prvCtr.AcceptedAsynchronousEncryptionScheme); err != nil {
|
||||||
|
return "", errors.New("cannot find a asynchronous encryption scheme that satisfy both contracts")
|
||||||
|
} else {
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSignatureScheme(prvCtr *PrivateContract, pubCtr *PublicContract) (string, error) {
|
||||||
|
if b, err := getFirstItemInList1ThatIsAlsoInList2(pubCtr.AcceptedSignatureScheme, prvCtr.AcceptedSignatureScheme); err != nil {
|
||||||
|
return "", errors.New("cannot find a signature scheme that satisfy both contracts")
|
||||||
|
} else {
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getBestBlockCipher(prvCtr *PrivateContract, pubCtr *PublicContract) (string, error) {
|
func getBestBlockCipher(prvCtr *PrivateContract, pubCtr *PublicContract) (string, error) {
|
||||||
if b, err := getFirstItemInList1ThatIsAlsoInList2(pubCtr.AcceptedBlockCyphers, prvCtr.AcceptedBlockCyphers); err != nil {
|
if b, err := getFirstItemInList1ThatIsAlsoInList2(pubCtr.AcceptedBlockCyphers, prvCtr.AcceptedBlockCyphers); err != nil {
|
||||||
return "", errors.New("cannot find a block cypher that satisfy both contracts")
|
return "", errors.New("cannot find a block cypher that satisfy both contracts")
|
||||||
@@ -68,6 +92,12 @@ func GetAcceptedContract(prvCtr *PrivateContract, pubCtr *PublicContract) (*Acce
|
|||||||
if ret.BlockCipherMode, err = getBestBlockCipherMode(prvCtr, pubCtr); err != nil {
|
if ret.BlockCipherMode, err = getBestBlockCipherMode(prvCtr, pubCtr); err != nil {
|
||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
}
|
}
|
||||||
|
if ret.AsynchronousEncryptionScheme, err = getAsynchronousEncryptionScheme(prvCtr, pubCtr); err != nil {
|
||||||
|
errs = append(errs, err.Error())
|
||||||
|
}
|
||||||
|
if ret.SignatureScheme, err = getSignatureScheme(prvCtr, pubCtr); err != nil {
|
||||||
|
errs = append(errs, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
return ret, helper.JoinAsError(errs)
|
return ret, helper.JoinAsError(errs)
|
||||||
}
|
}
|
||||||
@@ -48,6 +48,12 @@ func TestGetAcceptedContract(t *testing.T) {
|
|||||||
pubCtr.AcceptedBlockCypherModes = []string{"ofb"}
|
pubCtr.AcceptedBlockCypherModes = []string{"ofb"}
|
||||||
prvCtr.AcceptedBlockCypherModes = []string{"ofb"}
|
prvCtr.AcceptedBlockCypherModes = []string{"ofb"}
|
||||||
|
|
||||||
|
pubCtr.AcceptedAsynchronousEncryptionScheme = []string{"rsa_oaep"}
|
||||||
|
prvCtr.AcceptedAsynchronousEncryptionScheme = []string{"rsa_oaep"}
|
||||||
|
|
||||||
|
pubCtr.AcceptedSignatureScheme = []string{"rsa_pss"}
|
||||||
|
prvCtr.AcceptedSignatureScheme = []string{"rsa_pss"}
|
||||||
|
|
||||||
ac, err := GetAcceptedContract(prvCtr, pubCtr)
|
ac, err := GetAcceptedContract(prvCtr, pubCtr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
@@ -59,5 +65,11 @@ func TestGetAcceptedContract(t *testing.T) {
|
|||||||
if ac.BlockCipher != "aes" {
|
if ac.BlockCipher != "aes" {
|
||||||
t.Error("bad block cipher")
|
t.Error("bad block cipher")
|
||||||
}
|
}
|
||||||
|
if ac.SignatureScheme != "rsa_pss" {
|
||||||
|
t.Error("bad signature scheme")
|
||||||
|
}
|
||||||
|
if ac.AsynchronousEncryptionScheme != "rsa_oaep" {
|
||||||
|
t.Error("bad asynchronous encryption scheme")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,8 +5,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type PublicContract struct {
|
type PublicContract struct {
|
||||||
AcceptedHashes []string `yaml:"accepted_hashes" json:"accepted_hashes"`
|
AcceptedHashes []string `yaml:"accepted_hashes" json:"accepted_hashes"`
|
||||||
AcceptedBlockCyphers []string `yaml:"accepted_block_cypher" json:"accepted_block_cypher"`
|
AcceptedBlockCyphers []string `yaml:"accepted_block_cypher" json:"accepted_block_cypher"`
|
||||||
AcceptedBlockCypherModes []string `yaml:"accepted_block_cypher_mode" json:"accepted_block_cypher_mode"`
|
AcceptedBlockCypherModes []string `yaml:"accepted_block_cypher_mode" json:"accepted_block_cypher_mode"`
|
||||||
RSAPublicKey []*keys.RSAPublicKey `yaml:"rsa_public_key, omitempty" json:"rsa_public_key,omitempty"`
|
|
||||||
|
AcceptedAsynchronousEncryptionScheme []string `yaml:"accepted_asynchronous_encryption_scheme" json:"accepted_asynchronous_encryption_scheme"`
|
||||||
|
AcceptedSignatureScheme []string `yaml:"accepted_signature_scheme" json:"accepted_signature_scheme"`
|
||||||
|
|
||||||
|
RSAPublicKey []*keys.RSAPublicKey `yaml:"rsa_public_key, omitempty" json:"rsa_public_key,omitempty"`
|
||||||
}
|
}
|
||||||
@@ -38,7 +38,11 @@ func TestSimpleJsonification(t *testing.T) {
|
|||||||
c := &PublicContract{
|
c := &PublicContract{
|
||||||
AcceptedHashes: []string{"sha246"},
|
AcceptedHashes: []string{"sha246"},
|
||||||
AcceptedBlockCyphers: []string{"aes"},
|
AcceptedBlockCyphers: []string{"aes"},
|
||||||
RSAPublicKey: []*keys.RSAPublicKey{&keys.RSAPublicKey{helper.B(12), 3}},
|
|
||||||
|
AcceptedAsynchronousEncryptionScheme: []string{"rsa_oaep"},
|
||||||
|
AcceptedSignatureScheme: []string{"rsa_pss"},
|
||||||
|
|
||||||
|
RSAPublicKey: []*keys.RSAPublicKey{&keys.RSAPublicKey{helper.B(12), 3}},
|
||||||
}
|
}
|
||||||
if err := assertJSONMarshalling(c); err != nil {
|
if err := assertJSONMarshalling(c); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
@@ -49,11 +53,20 @@ func TestEmptyRSAPublicKey(t *testing.T) {
|
|||||||
c := &PublicContract{
|
c := &PublicContract{
|
||||||
AcceptedHashes: []string{"sha246"},
|
AcceptedHashes: []string{"sha246"},
|
||||||
AcceptedBlockCyphers: []string{"aes"},
|
AcceptedBlockCyphers: []string{"aes"},
|
||||||
RSAPublicKey: []*keys.RSAPublicKey{&keys.RSAPublicKey{helper.B(12), 3}},
|
|
||||||
|
AcceptedAsynchronousEncryptionScheme: []string{"rsa_oaep"},
|
||||||
|
AcceptedSignatureScheme: []string{"rsa_pss"},
|
||||||
|
|
||||||
|
RSAPublicKey: []*keys.RSAPublicKey{&keys.RSAPublicKey{helper.B(12), 3}},
|
||||||
}
|
}
|
||||||
jsonData := `{
|
jsonData := `{
|
||||||
"accepted_hashes": ["sha246"],
|
"accepted_hashes": ["sha246"],
|
||||||
"accepted_block_cypher": ["aes"],
|
"accepted_block_cypher": ["aes"],
|
||||||
|
|
||||||
|
|
||||||
|
"accepted_asynchronous_encryption_scheme": ["rsa_oaep"],
|
||||||
|
"accepted_signature_scheme": ["rsa_pss"],
|
||||||
|
|
||||||
"rsa_public_key": [{"n": "c", "e": 3}]
|
"rsa_public_key": [{"n": "c", "e": 3}]
|
||||||
}`
|
}`
|
||||||
if err := expectJson(c, jsonData); err != nil {
|
if err := expectJson(c, jsonData); err != nil {
|
||||||
|
|||||||
@@ -9,7 +9,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func EncryptFile(reader io.Reader, writer io.Writer, acceptedContract *contract.AcceptedContract) error {
|
func EncryptFile(reader io.Reader, writer io.Writer, acceptedContract *contract.AcceptedContract) error {
|
||||||
payload_ := payload.GetDefaultPayload(reader)
|
payload_, err := payload.GetPayload(reader, acceptedContract)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//signer :=
|
||||||
envelop := envelop.NewEnveloper(nil, payload_, nil)
|
envelop := envelop.NewEnveloper(nil, payload_, nil)
|
||||||
return envelop.WriteToWriter(writer)
|
return envelop.WriteToWriter(writer)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user