...

Source file src/github.com/digitorus/pkcs7/encrypt_test.go

Documentation: github.com/digitorus/pkcs7

     1  package pkcs7
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/x509"
     6  	"testing"
     7  )
     8  
     9  func TestEncrypt(t *testing.T) {
    10  	modes := []int{
    11  		EncryptionAlgorithmDESCBC,
    12  		EncryptionAlgorithmAES128CBC,
    13  		EncryptionAlgorithmAES256CBC,
    14  		EncryptionAlgorithmAES128GCM,
    15  		EncryptionAlgorithmAES256GCM,
    16  	}
    17  	sigalgs := []x509.SignatureAlgorithm{
    18  		x509.SHA1WithRSA,
    19  		x509.SHA256WithRSA,
    20  		x509.SHA512WithRSA,
    21  	}
    22  	for _, mode := range modes {
    23  		for _, sigalg := range sigalgs {
    24  			ContentEncryptionAlgorithm = mode
    25  
    26  			plaintext := []byte("Hello Secret World!")
    27  			cert, err := createTestCertificate(sigalg)
    28  			if err != nil {
    29  				t.Fatal(err)
    30  			}
    31  			encrypted, err := Encrypt(plaintext, []*x509.Certificate{cert.Certificate})
    32  			if err != nil {
    33  				t.Fatal(err)
    34  			}
    35  			p7, err := Parse(encrypted)
    36  			if err != nil {
    37  				t.Fatalf("cannot Parse encrypted result: %s", err)
    38  			}
    39  			result, err := p7.Decrypt(cert.Certificate, *cert.PrivateKey)
    40  			if err != nil {
    41  				t.Fatalf("cannot Decrypt encrypted result: %s", err)
    42  			}
    43  			if !bytes.Equal(plaintext, result) {
    44  				t.Errorf("encrypted data does not match plaintext:\n\tExpected: %s\n\tActual: %s", plaintext, result)
    45  			}
    46  		}
    47  	}
    48  }
    49  
    50  func TestEncryptUsingPSK(t *testing.T) {
    51  	modes := []int{
    52  		EncryptionAlgorithmDESCBC,
    53  		EncryptionAlgorithmAES128GCM,
    54  	}
    55  
    56  	for _, mode := range modes {
    57  		ContentEncryptionAlgorithm = mode
    58  		plaintext := []byte("Hello Secret World!")
    59  		var key []byte
    60  
    61  		switch mode {
    62  		case EncryptionAlgorithmDESCBC:
    63  			key = []byte("64BitKey")
    64  		case EncryptionAlgorithmAES128GCM:
    65  			key = []byte("128BitKey4AESGCM")
    66  		}
    67  		ciphertext, err := EncryptUsingPSK(plaintext, key)
    68  		if err != nil {
    69  			t.Fatal(err)
    70  		}
    71  
    72  		p7, _ := Parse(ciphertext)
    73  		result, err := p7.DecryptUsingPSK(key)
    74  		if err != nil {
    75  			t.Fatalf("cannot Decrypt encrypted result: %s", err)
    76  		}
    77  		if !bytes.Equal(plaintext, result) {
    78  			t.Errorf("encrypted data does not match plaintext:\n\tExpected: %s\n\tActual: %s", plaintext, result)
    79  		}
    80  	}
    81  }
    82  
    83  func TestPad(t *testing.T) {
    84  	tests := []struct {
    85  		Original  []byte
    86  		Expected  []byte
    87  		BlockSize int
    88  	}{
    89  		{[]byte{0x1, 0x2, 0x3, 0x10}, []byte{0x1, 0x2, 0x3, 0x10, 0x4, 0x4, 0x4, 0x4}, 8},
    90  		{[]byte{0x1, 0x2, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0}, []byte{0x1, 0x2, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8}, 8},
    91  	}
    92  	for _, test := range tests {
    93  		padded, err := pad(test.Original, test.BlockSize)
    94  		if err != nil {
    95  			t.Errorf("pad encountered error: %s", err)
    96  			continue
    97  		}
    98  		if !bytes.Equal(test.Expected, padded) {
    99  			t.Errorf("pad results mismatch:\n\tExpected: %X\n\tActual: %X", test.Expected, padded)
   100  		}
   101  	}
   102  }
   103  

View as plain text