github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/azure/pkcs12/rc2/rc2_test.go (about)

     1  package rc2
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"testing"
     7  )
     8  
     9  func TestEncryptDecrypt(t *testing.T) {
    10  
    11  	var tests = []struct {
    12  		key    string
    13  		plain  string
    14  		cipher string
    15  		t1     int
    16  	}{
    17  		{
    18  			"0000000000000000",
    19  			"0000000000000000",
    20  			"ebb773f993278eff",
    21  			63,
    22  		},
    23  		{
    24  			"ffffffffffffffff",
    25  			"ffffffffffffffff",
    26  			"278b27e42e2f0d49",
    27  			64,
    28  		},
    29  		{
    30  			"3000000000000000",
    31  			"1000000000000001",
    32  			"30649edf9be7d2c2",
    33  			64,
    34  		},
    35  		{
    36  			"88",
    37  			"0000000000000000",
    38  			"61a8a244adacccf0",
    39  			64,
    40  		},
    41  		{
    42  			"88bca90e90875a",
    43  			"0000000000000000",
    44  			"6ccf4308974c267f",
    45  			64,
    46  		},
    47  		{
    48  			"88bca90e90875a7f0f79c384627bafb2",
    49  			"0000000000000000",
    50  			"1a807d272bbe5db1",
    51  			64,
    52  		},
    53  		{
    54  			"88bca90e90875a7f0f79c384627bafb2",
    55  			"0000000000000000",
    56  			"2269552ab0f85ca6",
    57  			128,
    58  		},
    59  		{
    60  			"88bca90e90875a7f0f79c384627bafb216f80a6f85920584c42fceb0be255daf1e",
    61  			"0000000000000000",
    62  			"5b78d3a43dfff1f1",
    63  			129,
    64  		},
    65  	}
    66  
    67  	for _, tt := range tests {
    68  		k, _ := hex.DecodeString(tt.key)
    69  		p, _ := hex.DecodeString(tt.plain)
    70  		c, _ := hex.DecodeString(tt.cipher)
    71  
    72  		b, _ := New(k, tt.t1)
    73  
    74  		var dst [8]byte
    75  
    76  		b.Encrypt(dst[:], p)
    77  
    78  		if !bytes.Equal(dst[:], c) {
    79  			t.Errorf("encrypt failed: got % 2x wanted % 2x\n", dst, c)
    80  		}
    81  
    82  		b.Decrypt(dst[:], c)
    83  
    84  		if !bytes.Equal(dst[:], p) {
    85  			t.Errorf("decrypt failed: got % 2x wanted % 2x\n", dst, p)
    86  		}
    87  	}
    88  }
    89  
    90  func BenchmarkEncrypt(b *testing.B) {
    91  	r, _ := New([]byte{0, 0, 0, 0, 0, 0, 0, 0}, 64)
    92  	b.ResetTimer()
    93  	var src [8]byte
    94  	for i := 0; i < b.N; i++ {
    95  		r.Encrypt(src[:], src[:])
    96  	}
    97  }
    98  func BenchmarkDecrypt(b *testing.B) {
    99  	r, _ := New([]byte{0, 0, 0, 0, 0, 0, 0, 0}, 64)
   100  	b.ResetTimer()
   101  	var src [8]byte
   102  	for i := 0; i < b.N; i++ {
   103  		r.Decrypt(src[:], src[:])
   104  	}
   105  }