...

Source file src/golang.org/x/crypto/ssh/internal/bcrypt_pbkdf/bcrypt_pbkdf_test.go

Documentation: golang.org/x/crypto/ssh/internal/bcrypt_pbkdf

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bcrypt_pbkdf
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  )
    11  
    12  // Test vectors generated by the reference implementation from OpenBSD.
    13  var golden = []struct {
    14  	rounds                 int
    15  	password, salt, result []byte
    16  }{
    17  	{
    18  		12,
    19  		[]byte("password"),
    20  		[]byte("salt"),
    21  		[]byte{
    22  			0x1a, 0xe4, 0x2c, 0x05, 0xd4, 0x87, 0xbc, 0x02, 0xf6,
    23  			0x49, 0x21, 0xa4, 0xeb, 0xe4, 0xea, 0x93, 0xbc, 0xac,
    24  			0xfe, 0x13, 0x5f, 0xda, 0x99, 0x97, 0x4c, 0x06, 0xb7,
    25  			0xb0, 0x1f, 0xae, 0x14, 0x9a,
    26  		},
    27  	},
    28  	{
    29  		3,
    30  		[]byte("passwordy\x00PASSWORD\x00"),
    31  		[]byte("salty\x00SALT\x00"),
    32  		[]byte{
    33  			0x7f, 0x31, 0x0b, 0xd3, 0xe7, 0x8c, 0x32, 0x80, 0xc5,
    34  			0x9c, 0xe4, 0x59, 0x52, 0x11, 0xa2, 0x92, 0x8e, 0x8d,
    35  			0x4e, 0xc7, 0x44, 0xc1, 0xed, 0x2e, 0xfc, 0x9f, 0x76,
    36  			0x4e, 0x33, 0x88, 0xe0, 0xad,
    37  		},
    38  	},
    39  	{
    40  		// See http://thread.gmane.org/gmane.os.openbsd.bugs/20542
    41  		8,
    42  		[]byte("секретное слово"),
    43  		[]byte("посолить немножко"),
    44  		[]byte{
    45  			0x8d, 0xf4, 0x3f, 0xc6, 0xfe, 0x13, 0x1f, 0xc4, 0x7f,
    46  			0x0c, 0x9e, 0x39, 0x22, 0x4b, 0xd9, 0x4c, 0x70, 0xb6,
    47  			0xfc, 0xc8, 0xee, 0x81, 0x35, 0xfa, 0xdd, 0xf6, 0x11,
    48  			0x56, 0xe6, 0xcb, 0x27, 0x33, 0xea, 0x76, 0x5f, 0x31,
    49  			0x5a, 0x3e, 0x1e, 0x4a, 0xfc, 0x35, 0xbf, 0x86, 0x87,
    50  			0xd1, 0x89, 0x25, 0x4c, 0x1e, 0x05, 0xa6, 0xfe, 0x80,
    51  			0xc0, 0x61, 0x7f, 0x91, 0x83, 0xd6, 0x72, 0x60, 0xd6,
    52  			0xa1, 0x15, 0xc6, 0xc9, 0x4e, 0x36, 0x03, 0xe2, 0x30,
    53  			0x3f, 0xbb, 0x43, 0xa7, 0x6a, 0x64, 0x52, 0x3f, 0xfd,
    54  			0xa6, 0x86, 0xb1, 0xd4, 0x51, 0x85, 0x43,
    55  		},
    56  	},
    57  }
    58  
    59  func TestKey(t *testing.T) {
    60  	for i, v := range golden {
    61  		k, err := Key(v.password, v.salt, v.rounds, len(v.result))
    62  		if err != nil {
    63  			t.Errorf("%d: %s", i, err)
    64  			continue
    65  		}
    66  		if !bytes.Equal(k, v.result) {
    67  			t.Errorf("%d: expected\n%x\n, got\n%x\n", i, v.result, k)
    68  		}
    69  	}
    70  }
    71  
    72  func TestBcryptHash(t *testing.T) {
    73  	good := []byte{
    74  		0x87, 0x90, 0x48, 0x70, 0xee, 0xf9, 0xde, 0xdd, 0xf8, 0xe7,
    75  		0x61, 0x1a, 0x14, 0x01, 0x06, 0xe6, 0xaa, 0xf1, 0xa3, 0x63,
    76  		0xd9, 0xa2, 0xc5, 0x04, 0xdb, 0x35, 0x64, 0x43, 0x72, 0x1e,
    77  		0xb5, 0x55,
    78  	}
    79  	var pass, salt [64]byte
    80  	var result [32]byte
    81  	for i := 0; i < 64; i++ {
    82  		pass[i] = byte(i)
    83  		salt[i] = byte(i + 64)
    84  	}
    85  	bcryptHash(result[:], pass[:], salt[:])
    86  	if !bytes.Equal(result[:], good) {
    87  		t.Errorf("expected %x, got %x", good, result)
    88  	}
    89  }
    90  
    91  func BenchmarkKey(b *testing.B) {
    92  	pass := []byte("password")
    93  	salt := []byte("salt")
    94  	for i := 0; i < b.N; i++ {
    95  		Key(pass, salt, 10, 32)
    96  	}
    97  }
    98  

View as plain text