mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-06 11:28:10 +02:00
cryptutil: add streaming crypt helpers
This commit is contained in:
parent
58d8f406a9
commit
cebd1df947
2 changed files with 159 additions and 0 deletions
39
pkg/cryptutil/stream_test.go
Normal file
39
pkg/cryptutil/stream_test.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package cryptutil_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/pomerium/pomerium/pkg/cryptutil"
|
||||
)
|
||||
|
||||
func TestEncryptStream(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
plaintext := make([]byte, 4048*2.5)
|
||||
_, err := rand.Read(plaintext)
|
||||
require.NoError(t, err)
|
||||
|
||||
// cipher
|
||||
c, err := cryptutil.NewAEADCipher(cryptutil.NewKey())
|
||||
require.NoError(t, err)
|
||||
|
||||
// encrypt
|
||||
encrypted := &bytes.Buffer{}
|
||||
encrypter, err := cryptutil.EncryptStream(bytes.NewBuffer(plaintext), c)
|
||||
require.NoError(t, err)
|
||||
_, err = io.Copy(encrypted, encrypter)
|
||||
require.NoError(t, err)
|
||||
|
||||
// decrypt
|
||||
decrypted := &bytes.Buffer{}
|
||||
decrypter, err := cryptutil.DecryptStream(encrypted, c)
|
||||
require.NoError(t, err)
|
||||
_, err = decrypted.ReadFrom(decrypter)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, plaintext, decrypted.Bytes())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue