|
@ -1,14 +1,11 @@ |
|
|
package util |
|
|
package util |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"bytes" |
|
|
|
|
|
"crypto/aes" |
|
|
"crypto/aes" |
|
|
"crypto/cipher" |
|
|
"crypto/cipher" |
|
|
"crypto/rand" |
|
|
"crypto/rand" |
|
|
"errors" |
|
|
"errors" |
|
|
"fmt" |
|
|
|
|
|
"io" |
|
|
"io" |
|
|
"io/ioutil" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog" |
|
|
"github.com/chrislusf/seaweedfs/weed/glog" |
|
|
) |
|
|
) |
|
@ -61,21 +58,3 @@ func Decrypt(ciphertext []byte, key CipherKey) ([]byte, error) { |
|
|
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] |
|
|
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] |
|
|
return gcm.Open(nil, nonce, ciphertext, nil) |
|
|
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 |
|
|
|
|
|
} |
|
|
|