github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/crypto/aes_test.go (about)

     1  package crypto
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func makeBuf(l int) []byte {
    10  	buf := make([]byte, l)
    11  	for i := range buf {
    12  		buf[i] = 'x'
    13  	}
    14  	return buf
    15  }
    16  
    17  func TestEncryptWithAES256CBC(t *testing.T) {
    18  	key := makeBuf(32)
    19  	payload := makeBuf(60)
    20  	iv := makeBuf(16)
    21  	str, err := EncryptWithAES256CBC(key, payload, iv)
    22  	assert.NoError(t, err)
    23  	expected := "0.eHh4eHh4eHh4eHh4eHh4eA==|xYfs1rg9exsctltfkrSi7gROm5RjRhnF71zYvz3zHeIn6UIIoZP3Whbh6CSbbdfdhLJTQ2SxHylBF2L/nziH5Q=="
    24  	assert.Equal(t, expected, str)
    25  	// In ruby:
    26  	// require 'base64'
    27  	// require 'openssl'
    28  	// key = 'x' * 32
    29  	// pt = 'x' * 60
    30  	// iv = 'x' * 16
    31  	// cipher = OpenSSL::Cipher.new "AES-256-CBC"
    32  	// cipher.encrypt
    33  	// cipher.key = key
    34  	// cipher.iv = iv
    35  	// ct = cipher.update(pt)
    36  	// ct << cipher.final
    37  	// expected = "0." + Base64.strict_encode64(iv) + "|" + Base64.strict_encode64(ct)
    38  }