github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/crypto/scrypt/scrypt_test.go (about)

     1  // Copyright 2012 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 scrypt
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  )
    11  
    12  type testVector struct {
    13  	password string
    14  	salt     string
    15  	N, r, p  int
    16  	output   []byte
    17  }
    18  
    19  var good = []testVector{
    20  	{
    21  		"password",
    22  		"salt",
    23  		2, 10, 10,
    24  		[]byte{
    25  			0x46, 0xdd, 0xe6, 0xca, 0xc9, 0x99, 0x77, 0x78, 0x2c,
    26  			0x25, 0x3a, 0xde, 0x7f, 0x5a, 0x60, 0x9e, 0xc0, 0x61,
    27  			0xf, 0xdc, 0xa3, 0x56, 0xd4, 0x2a, 0x1b, 0x5c, 0x4a,
    28  			0x1a, 0xe, 0x74, 0x40, 0x5f,
    29  		},
    30  	},
    31  	{
    32  		"password",
    33  		"salt",
    34  		16, 100, 100,
    35  		[]byte{
    36  			0xad, 0xde, 0x5f, 0x33, 0xd2, 0xae, 0x96, 0xf7, 0x66,
    37  			0xb2, 0xf3, 0x53, 0xfc, 0x9c, 0xa9, 0xb, 0x3b, 0xfa,
    38  			0x22, 0x8f, 0x2d, 0xe9, 0x3, 0x67, 0x90, 0x6c, 0x0,
    39  			0xc5, 0xbe, 0x96, 0xb0, 0x24,
    40  		},
    41  	},
    42  	{
    43  		"this is a long \000 password",
    44  		"and this is a long \000 salt",
    45  		16384, 8, 1,
    46  		[]byte{
    47  			0xe1, 0x48, 0x46, 0x73, 0xd9, 0x76, 0xf0, 0x1d, 0xd8,
    48  			0xfb, 0x7c, 0x28, 0xe6, 0x9a, 0xf4, 0x8d, 0x18, 0x3f,
    49  			0x6d, 0x9b, 0x79, 0x1, 0x91, 0xcb, 0x3f, 0x7, 0xad,
    50  			0x15, 0x40, 0xbb, 0xf, 0x18, 0x96, 0x7d, 0xf0, 0x62,
    51  			0x31, 0x9b, 0x60, 0x23, 0x84, 0xc2, 0xe0, 0x50, 0xfb,
    52  			0xe, 0x80, 0x5f, 0x8a, 0x79, 0x54, 0x42, 0x6f, 0x1e,
    53  			0x70, 0x73, 0xdc, 0x19, 0xb3, 0xf1, 0xd9, 0x88, 0x38,
    54  			0xb1, 0x3d, 0x20, 0x12, 0x54, 0x9c, 0x64, 0x21, 0xd6,
    55  			0x2, 0x9, 0xe8, 0x21, 0xbd,
    56  		},
    57  	},
    58  	{
    59  		"p",
    60  		"s",
    61  		2, 1, 1,
    62  		[]byte{
    63  			0xae, 0x3d, 0x65, 0x76, 0x23, 0xf9, 0x1f, 0x5b, 0x95,
    64  			0x86, 0x69, 0x9d, 0xe0, 0x3f, 0xec, 0x87,
    65  		},
    66  	},
    67  
    68  	{
    69  		"",
    70  		"",
    71  		16, 1, 1,
    72  		[]byte{
    73  			0x2c, 0xee, 0x14, 0x7, 0xab, 0xf2, 0x52, 0xaa, 0x3,
    74  			0x40, 0xc8, 0x50, 0xbd, 0x4e, 0xcd, 0x59, 0xac, 0x2b,
    75  			0x31, 0xf6, 0x3c, 0x41, 0xb3, 0x87, 0x7c, 0x25, 0xa8,
    76  			0xe7, 0x3b, 0xbd, 0xd4, 0x7e, 0x66, 0x7d, 0x61, 0xd0,
    77  			0xde, 0x42, 0x7c, 0x68, 0x14, 0x1e, 0x8e, 0xe1, 0xd7,
    78  			0x27, 0xae, 0xc2, 0xa9, 0xf3, 0x29, 0xe8, 0x17, 0xb2,
    79  			0x11, 0x36, 0x2, 0xec, 0xdf, 0x8d, 0xa1, 0xe2, 0x45,
    80  			0x10,
    81  		},
    82  	},
    83  	{
    84  		"password",
    85  		"NaCl",
    86  		1024, 8, 16,
    87  		[]byte{
    88  			0xab, 0x41, 0x2d, 0xe0, 0x56, 0x42, 0xe4, 0xb5, 0xf2,
    89  			0x91, 0x86, 0xd8, 0xaf, 0x8, 0x62, 0x92, 0x44, 0xb5,
    90  			0xd3, 0xdf, 0xe2, 0xf8, 0x32, 0x30, 0x2b, 0xc9, 0xec,
    91  			0x8b, 0x9c, 0xb3, 0x82, 0x3b, 0x77, 0xd8, 0x54, 0x3e,
    92  			0xbb, 0x54, 0x53, 0xfd, 0xec, 0x88, 0x96, 0x93, 0xa7,
    93  			0x8c, 0x76, 0xc8, 0xc5, 0x46, 0xa9, 0xfe, 0xfd, 0x6c,
    94  			0xeb, 0x92, 0x2b, 0xdf, 0x1, 0x64, 0xa0, 0x19, 0x6a,
    95  			0x79,
    96  		},
    97  	},
    98  	{
    99  		"pleaseletmein", "SodiumChloride",
   100  		16384, 8, 1,
   101  		[]byte{
   102  			0xf8, 0xa4, 0x6f, 0x17, 0xed, 0x33, 0xb1, 0x82, 0x5f,
   103  			0x2b, 0x56, 0x47, 0x29, 0x2f, 0x20, 0xc0, 0x3a, 0xaa,
   104  			0xdb, 0x15, 0xdf, 0x5d, 0xf8, 0x37, 0x9f, 0xe7, 0x21,
   105  			0xb2, 0x13, 0xfc, 0xf6, 0xb7, 0x88, 0xb7, 0xe8, 0xda,
   106  			0x21, 0x7c, 0xf5, 0x77, 0xae, 0xd8, 0xc9, 0x4e, 0xbf,
   107  			0xec, 0x48, 0x3f, 0x81, 0xb3, 0x3b, 0x9a, 0x2e, 0x7f,
   108  			0x14, 0x2a, 0xb4, 0x4d, 0x81, 0x7f, 0x9e, 0xf0, 0x52,
   109  			0xd2,
   110  		},
   111  	},
   112  
   113  	// Disabled: needs 1 GiB RAM and takes too long for a simple test.
   114  	{
   115  		"pleaseletmein", "SodiumChloride",
   116  		1048576, 8, 1,
   117  		[]byte{
   118  			0xee, 0xef, 0xad, 0x8b, 0xa9, 0xd5, 0x47, 0x13, 0x9d,
   119  			0x58, 0xe9, 0xae, 0x8, 0xa3, 0xf7, 0x79, 0x1b, 0x3c,
   120  			0xba, 0x62, 0x75, 0x7, 0x89, 0x64, 0x24, 0xaa, 0x2d,
   121  			0xb4, 0xa5, 0xa5, 0x8e, 0xdd, 0xb6, 0xa, 0x41, 0x31,
   122  			0x59, 0x1f, 0x40, 0x1, 0xe6, 0x9, 0x63, 0x19, 0xc6,
   123  			0x77, 0xf3, 0x30, 0x74, 0x26, 0xb8, 0x5d, 0x7e, 0x76,
   124  			0x2d, 0x61, 0xe6, 0x3a, 0xbc, 0x8, 0xb5, 0x9e, 0x64,
   125  			0xc,
   126  		},
   127  	},
   128  }
   129  
   130  var bad = []testVector{
   131  	{"p", "s", 0, 1, 1, nil},                    // N == 0
   132  	{"p", "s", 1, 1, 1, nil},                    // N == 1
   133  	{"p", "s", 7, 8, 1, nil},                    // N is not power of 2
   134  	{"p", "s", 16, maxInt / 2, maxInt / 2, nil}, // p * r too large
   135  }
   136  
   137  func TestKey(t *testing.T) {
   138  	for i, v := range good {
   139  		k, err := Key([]byte(v.password), []byte(v.salt), v.N, v.r, v.p, len(v.output), uint(0x30))
   140  		if err != nil {
   141  			t.Errorf("%d: got unexpected error: %s", i, err)
   142  		}
   143  		if !bytes.Equal(k, v.output) {
   144  			t.Errorf("%d: expected %x, got %x", i, v.output, k)
   145  		}
   146  	}
   147  	for i, v := range bad {
   148  		_, err := Key([]byte(v.password), []byte(v.salt), v.N, v.r, v.p, 32, uint(0x30))
   149  		if err == nil {
   150  			t.Errorf("%d: expected error, got nil", i)
   151  		}
   152  	}
   153  }
   154  
   155  var sink []byte
   156  
   157  func BenchmarkKey(b *testing.B) {
   158  	for i := 0; i < b.N; i++ {
   159  		sink, _ = Key([]byte("password"), []byte("salt"), 1<<15, 8, 1, 64, uint(0x30))
   160  	}
   161  }