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

     1  package pkcs
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  func TestScryptDeriveKey(t *testing.T) {
     9  	opts := NewScryptOpts(8, 16384, 8, 1)
    10  	key, params, err := opts.DeriveKey([]byte("password"), []byte("saltsalt"), 32)
    11  	if err != nil {
    12  		t.Errorf("unexpected error: %v", err)
    13  	}
    14  	if len(key) != 32 {
    15  		t.Errorf("unexpected key length: got %d, want 32", len(key))
    16  	}
    17  	if params.KeyLength() != 32 {
    18  		t.Errorf("unexpected key length: got %d, want 32", params.KeyLength())
    19  	}
    20  	if len(params.(scryptParams).Salt) != opts.SaltSize {
    21  		t.Errorf("unexpected salt length: got %d, want %d", len(params.(scryptParams).Salt), opts.SaltSize)
    22  	}
    23  	if params.(scryptParams).CostParameter != opts.CostParameter {
    24  		t.Errorf("unexpected cost parameter: got %d, want %d", params.(scryptParams).CostParameter, opts.CostParameter)
    25  	}
    26  	if params.(scryptParams).BlockSize != opts.BlockSize {
    27  		t.Errorf("unexpected block size: got %d, want %d", params.(scryptParams).BlockSize, opts.BlockSize)
    28  	}
    29  	if params.(scryptParams).ParallelizationParameter != opts.ParallelizationParameter {
    30  		t.Errorf("unexpected parallelization parameter: got %d, want %d", params.(scryptParams).ParallelizationParameter, opts.ParallelizationParameter)
    31  	}
    32  	key2, err := params.DeriveKey(nil, []byte("password"), 32)
    33  	if err != nil {
    34  		t.Errorf("unexpected error: %v", err)
    35  	}
    36  	if !bytes.Equal(key, key2) {
    37  		t.Errorf("unexpected key: got %x, want %x", key2, key)
    38  	}
    39  }