github.com/hairyhenderson/templater@v3.5.0+incompatible/crypto/pbkdf2_test.go (about)

     1  package crypto
     2  
     3  import (
     4  	"crypto"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestPBKDF2(t *testing.T) {
    11  	dk, err := PBKDF2([]byte{}, []byte{}, 0, 0, 0)
    12  	assert.Nil(t, dk)
    13  	assert.Error(t, err)
    14  
    15  	// IEEE 802.11i-2004 test vectors
    16  	dk, err = PBKDF2([]byte("password"), []byte("IEEE"), 4096, 32, crypto.SHA1)
    17  	assert.EqualValues(t, []byte{
    18  		0xf4, 0x2c, 0x6f, 0xc5, 0x2d, 0xf0, 0xeb, 0xef,
    19  		0x9e, 0xbb, 0x4b, 0x90, 0xb3, 0x8a, 0x5f, 0x90,
    20  		0x2e, 0x83, 0xfe, 0x1b, 0x13, 0x5a, 0x70, 0xe2,
    21  		0x3a, 0xed, 0x76, 0x2e, 0x97, 0x10, 0xa1, 0x2e,
    22  	}, dk)
    23  	assert.NoError(t, err)
    24  
    25  	dk, err = PBKDF2([]byte("ThisIsAPassword"), []byte("ThisIsASSID"), 4096, 32, crypto.SHA1)
    26  	assert.EqualValues(t, []byte{
    27  		0x0d, 0xc0, 0xd6, 0xeb, 0x90, 0x55, 0x5e, 0xd6,
    28  		0x41, 0x97, 0x56, 0xb9, 0xa1, 0x5e, 0xc3, 0xe3,
    29  		0x20, 0x9b, 0x63, 0xdf, 0x70, 0x7d, 0xd5, 0x08,
    30  		0xd1, 0x45, 0x81, 0xf8, 0x98, 0x27, 0x21, 0xaf,
    31  	}, dk)
    32  	assert.NoError(t, err)
    33  
    34  	dk, err = PBKDF2([]byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), []byte("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"), 4096, 32, crypto.SHA1)
    35  	assert.EqualValues(t, []byte{
    36  		0xbe, 0xcb, 0x93, 0x86, 0x6b, 0xb8, 0xc3, 0x83,
    37  		0x2c, 0xb7, 0x77, 0xc2, 0xf5, 0x59, 0x80, 0x7c,
    38  		0x8c, 0x59, 0xaf, 0xcb, 0x6e, 0xae, 0x73, 0x48,
    39  		0x85, 0x00, 0x13, 0x00, 0xa9, 0x81, 0xcc, 0x62,
    40  	}, dk)
    41  	assert.NoError(t, err)
    42  
    43  	// some longer hash functions
    44  	dk, err = PBKDF2([]byte("password"), []byte("IEEE"), 4096, 64, crypto.SHA512)
    45  	assert.EqualValues(t, []byte{
    46  		0xc1, 0x6f, 0x4c, 0xb6, 0xd0, 0x3e, 0x23, 0x61,
    47  		0x43, 0x99, 0xde, 0xe5, 0xe7, 0xf6, 0x76, 0xfb,
    48  		0x1d, 0xa0, 0xeb, 0x94, 0x71, 0xb6, 0xa7, 0x4a,
    49  		0x6c, 0x5b, 0xc9, 0x34, 0xc6, 0xec, 0x7d, 0x2a,
    50  		0xb7, 0x02, 0x8f, 0xbb, 0x10, 0x00, 0xb1, 0xbe,
    51  		0xb9, 0x7f, 0x17, 0x64, 0x60, 0x45, 0xd8, 0x14,
    52  		0x47, 0x92, 0x35, 0x2f, 0x66, 0x76, 0xd1, 0x3b,
    53  		0x20, 0xa4, 0xc0, 0x37, 0x54, 0x90, 0x3d, 0x7e,
    54  	}, dk)
    55  	assert.NoError(t, err)
    56  }
    57  
    58  func TestStrToHash(t *testing.T) {
    59  	h, err := StrToHash("foo")
    60  	assert.Zero(t, h)
    61  	assert.Error(t, err)
    62  	h, err = StrToHash("SHA-1")
    63  	assert.Equal(t, crypto.SHA1, h)
    64  	assert.NoError(t, err)
    65  	h, err = StrToHash("SHA224")
    66  	assert.Equal(t, crypto.SHA224, h)
    67  	assert.NoError(t, err)
    68  	h, err = StrToHash("SHA-256")
    69  	assert.Equal(t, crypto.SHA256, h)
    70  	assert.NoError(t, err)
    71  	h, err = StrToHash("SHA384")
    72  	assert.Equal(t, crypto.SHA384, h)
    73  	assert.NoError(t, err)
    74  	h, err = StrToHash("SHA-512")
    75  	assert.Equal(t, crypto.SHA512, h)
    76  	assert.NoError(t, err)
    77  	h, err = StrToHash("SHA-512/224")
    78  	assert.Equal(t, crypto.SHA512_224, h)
    79  	assert.NoError(t, err)
    80  	h, err = StrToHash("SHA512/256")
    81  	assert.Equal(t, crypto.SHA512_256, h)
    82  	assert.NoError(t, err)
    83  }