diff options
| author | Chris Lu <chris.lu@gmail.com> | 2020-03-07 06:06:58 -0800 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2020-03-07 06:08:08 -0800 |
| commit | ea1169dc8021172a5d14e618b041efb56db98de5 (patch) | |
| tree | c94b032402d2c7726b4992d3621f671d5758482c /weed/util/cipher.go | |
| parent | e3b8bf5588835621db05d4572d0960ac7c9d3976 (diff) | |
| download | seaweedfs-ea1169dc8021172a5d14e618b041efb56db98de5.tar.xz seaweedfs-ea1169dc8021172a5d14e618b041efb56db98de5.zip | |
filer cipher: single chunk http POST and PUT and read
Diffstat (limited to 'weed/util/cipher.go')
| -rw-r--r-- | weed/util/cipher.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/weed/util/cipher.go b/weed/util/cipher.go index f044c2ca3..7bcb6559a 100644 --- a/weed/util/cipher.go +++ b/weed/util/cipher.go @@ -1,11 +1,14 @@ package util import ( + "bytes" "crypto/aes" "crypto/cipher" "crypto/rand" "errors" + "fmt" "io" + "io/ioutil" "github.com/chrislusf/seaweedfs/weed/glog" ) @@ -58,3 +61,21 @@ func Decrypt(ciphertext []byte, key CipherKey) ([]byte, error) { nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] return gcm.Open(nil, nonce, ciphertext, nil) } + +func EncryptReader(clearReader io.Reader) (cipherKey CipherKey, encryptedReader io.ReadCloser, clearDataLen, encryptedDataLen int, err error) { + clearData, err := ioutil.ReadAll(clearReader) + if err != nil { + err = fmt.Errorf("read raw input: %v", err) + return + } + clearDataLen = len(clearData) + cipherKey = GenCipherKey() + encryptedData, err := Encrypt(clearData, cipherKey) + if err != nil { + err = fmt.Errorf("encrypt input: %v", err) + return + } + encryptedDataLen = len(encryptedData) + encryptedReader = ioutil.NopCloser(bytes.NewReader(encryptedData)) + return +} |
