github.com/emmansun/gmsm@v0.29.1/pkcs/pkcs5_pbes1_test.go (about)

     1  package pkcs
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  )
     7  
     8  func TestPBES1(t *testing.T) {
     9  	var testCases []*PBES1
    10  
    11  	pbes1, err := NewPbeWithMD2AndDESCBC(rand.Reader, 8, 1000)
    12  	if err != nil {
    13  		t.Fatalf("unexpected error: %v", err)
    14  	}
    15  	testCases = append(testCases, pbes1)
    16  
    17  	pbes1, err = NewPbeWithMD2AndRC2CBC(rand.Reader, 8, 1000)
    18  	if err != nil {
    19  		t.Fatalf("unexpected error: %v", err)
    20  	}
    21  	testCases = append(testCases, pbes1)
    22  
    23  	pbes1, err = NewPbeWithMD5AndDESCBC(rand.Reader, 8, 1000)
    24  	if err != nil {
    25  		t.Fatalf("unexpected error: %v", err)
    26  	}
    27  	testCases = append(testCases, pbes1)
    28  
    29  	pbes1, err = NewPbeWithMD5AndRC2CBC(rand.Reader, 8, 1000)
    30  	if err != nil {
    31  		t.Fatalf("unexpected error: %v", err)
    32  	}
    33  	testCases = append(testCases, pbes1)
    34  
    35  	pbes1, err = NewPbeWithSHA1AndDESCBC(rand.Reader, 8, 1000)
    36  	if err != nil {
    37  		t.Fatalf("unexpected error: %v", err)
    38  	}
    39  	testCases = append(testCases, pbes1)
    40  
    41  	pbes1, err = NewPbeWithSHA1AndRC2CBC(rand.Reader, 8, 1000)
    42  	if err != nil {
    43  		t.Fatalf("unexpected error: %v", err)
    44  	}
    45  	testCases = append(testCases, pbes1)
    46  
    47  	for _, pbes1 := range testCases {
    48  		t.Run("", func(t *testing.T) {
    49  			_, ciphertext, err := pbes1.Encrypt(rand.Reader, []byte("password"), []byte("pbes1"))
    50  			if err != nil {
    51  				t.Errorf("unexpected error: %v", err)
    52  			}
    53  
    54  			plaintext, _, err := pbes1.Decrypt([]byte("password"), ciphertext)
    55  			if err != nil {
    56  				t.Errorf("unexpected error: %v", err)
    57  			}
    58  			if string(plaintext) != "pbes1" {
    59  				t.Errorf("unexpected plaintext: got %s, want password", plaintext)
    60  			}
    61  		})
    62  	}
    63  }