Skip to content

Commit

Permalink
pkg/object: support chacha20 cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyunha committed Aug 15, 2022
1 parent ab50cd8 commit 37620f9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func createStorage(format meta.Format) (object.ObjectStorage, error) {
if err != nil {
return nil, fmt.Errorf("incorrect passphrase: %s", err)
}
encryptor, err := object.NewDataEncryptor(object.NewRSAEncryptor(privKey), format.EncryptAglo)
encryptor, err := object.NewDataEncryptor(object.NewRSAEncryptor(privKey), format.EncryptAlgo)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -385,7 +385,7 @@ func format(c *cli.Context) error {
format.HashPrefix = c.Bool(flag)
case "storage":
format.Storage = c.String(flag)
case "encrypt-rsa-key":
case "encrypt-rsa-key", "encrypt-algo":
logger.Warnf("Flag %s is ignored since it cannot be updated", flag)
}
}
Expand All @@ -400,7 +400,7 @@ func format(c *cli.Context) error {
SecretKey: c.String("secret-key"),
SessionToken: c.String("session-token"),
EncryptKey: loadEncrypt(c.String("encrypt-rsa-key")),
EncryptAglo: c.String("encrypt-aglo"),
EncryptAlgo: c.String("encrypt-algo"),
Shards: c.Int("shards"),
HashPrefix: c.Bool("hash-prefix"),
Capacity: c.Uint64("capacity") << 30,
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Format struct {
Capacity uint64 `json:",omitempty"`
Inodes uint64 `json:",omitempty"`
EncryptKey string `json:",omitempty"`
EncryptAglo string `json:",omitempty"`
EncryptAlgo string `json:",omitempty"`
KeyEncrypted bool `json:",omitempty"`
TrashDays int `json:",omitempty"`
MetaVersion int `json:",omitempty"`
Expand Down
16 changes: 14 additions & 2 deletions pkg/object/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,21 @@ func BenchmarkRSA4096Decrypt(b *testing.B) {
}
}

func TestChaCha20(t *testing.T) {
kc := NewRSAEncryptor(testkey)
dc, _ := NewDataEncryptor(kc, CHACHA20_RSA)
data := []byte("hello")
ciphertext, _ := dc.Encrypt(data)
plaintext, _ := dc.Decrypt(ciphertext)
if !bytes.Equal(data, plaintext) {
t.Errorf("decrypt fail")
t.Fail()
}
}

func TestAESGCM(t *testing.T) {
kc := NewRSAEncryptor(testkey)
dc := NewDataEncryptor(kc, AES256GCM_RSA)
dc, _ := NewDataEncryptor(kc, AES256GCM_RSA)
data := []byte("hello")
ciphertext, _ := dc.Encrypt(data)
plaintext, _ := dc.Decrypt(ciphertext)
Expand All @@ -139,7 +151,7 @@ func TestAESGCM(t *testing.T) {
func TestEncryptedStore(t *testing.T) {
s, _ := CreateStorage("mem", "", "", "", "")
kc := NewRSAEncryptor(testkey)
dc := NewDataEncryptor(kc, AES256GCM_RSA)
dc, _ := NewDataEncryptor(kc, AES256GCM_RSA)
es := NewEncrypted(s, dc)
_ = es.Put("a", bytes.NewReader([]byte("hello")))
r, err := es.Get("a", 1, 2)
Expand Down
2 changes: 1 addition & 1 deletion pkg/object/object_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func TestEncrypted(t *testing.T) {
s, _ := CreateStorage("mem", "", "", "", "")
privkey, _ := rsa.GenerateKey(rand.Reader, 2048)
kc := NewRSAEncryptor(privkey)
dc := NewDataEncryptor(kc, AES256GCM_RSA)
dc, _ := NewDataEncryptor(kc, AES256GCM_RSA)
es := NewEncrypted(s, dc)
testStorage(t, es)
}
Expand Down

0 comments on commit 37620f9

Please sign in to comment.