...

Source file src/edge-infra.dev/pkg/lib/crypto/osutilcrypt/sha512/sha512crypt_test.go

Documentation: edge-infra.dev/pkg/lib/crypto/osutilcrypt/sha512

     1  // Copyright 2012, Jeramey Crawford <jeramey@antihe.ro>
     2  // Copyright 2013, Jonas mg
     3  // All rights reserved.
     4  //
     5  // Use of this source code is governed by a BSD-style license
     6  // that can be found in the LICENSE file.
     7  
     8  package sha512crypt
     9  
    10  import "testing"
    11  
    12  var sha512Crypt = New()
    13  
    14  func TestGenerate(t *testing.T) {
    15  	data := []struct {
    16  		salt []byte
    17  		key  []byte
    18  		out  string
    19  		cost int
    20  	}{
    21  		{
    22  			[]byte("$6$saltstring"),
    23  			[]byte("Hello world!"),
    24  			"$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjn" +
    25  				"QJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1",
    26  			RoundsDefault,
    27  		},
    28  		{
    29  			[]byte("$6$rounds=10000$saltstringsaltstring"),
    30  			[]byte("Hello world!"),
    31  			"$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh" +
    32  				"0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v.",
    33  			10000,
    34  		},
    35  		{
    36  			[]byte("$6$rounds=5000$toolongsaltstring"),
    37  			[]byte("This is just a test"),
    38  			"$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoN" +
    39  				"eKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0",
    40  			5000,
    41  		},
    42  		{
    43  			[]byte("$6$rounds=1400$anotherlongsaltstring"),
    44  			[]byte("a very much longer text to encrypt.  " +
    45  				"This one even stretches over more" +
    46  				"than one line."),
    47  			"$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs" +
    48  				".wPvMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1",
    49  			1400,
    50  		},
    51  		{
    52  			[]byte("$6$rounds=77777$short"),
    53  			[]byte("we have a short salt string but not a short password"),
    54  			"$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkv" +
    55  				"r0gge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0",
    56  			77777,
    57  		},
    58  		{
    59  			[]byte("$6$rounds=123456$asaltof16chars.."),
    60  			[]byte("a short string"),
    61  			"$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4o" +
    62  				"PwcelCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1",
    63  			123456,
    64  		},
    65  		{
    66  			[]byte("$6$rounds=10$roundstoolow"),
    67  			[]byte("the minimum number is still observed"),
    68  			"$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50Yh" +
    69  				"H1xhLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX.",
    70  			1000,
    71  		},
    72  	}
    73  
    74  	for i, d := range data {
    75  		hash, err := sha512Crypt.Generate(d.key, d.salt)
    76  		if err != nil {
    77  			t.Fatal(err)
    78  		}
    79  		if hash != d.out {
    80  			t.Errorf("Test %d failed\nExpected: %s, got: %s", i, d.out, hash)
    81  		}
    82  
    83  		cost, err := sha512Crypt.Cost(hash)
    84  		if err != nil {
    85  			t.Fatal(err)
    86  		}
    87  		if cost != d.cost {
    88  			t.Errorf("Test %d failed\nExpected: %d, got: %d", i, d.cost, cost)
    89  		}
    90  	}
    91  }
    92  
    93  func TestVerify(t *testing.T) {
    94  	data := [][]byte{
    95  		[]byte("password"),
    96  		[]byte("12345"),
    97  		[]byte("That's amazing! I've got the same combination on my luggage!"),
    98  		[]byte("And change the combination on my luggage!"),
    99  		[]byte("         random  spa  c    ing."),
   100  		[]byte("94ajflkvjzpe8u3&*j1k513KLJ&*()"),
   101  	}
   102  	for i, d := range data {
   103  		hash, err := sha512Crypt.Generate(d, nil)
   104  		if err != nil {
   105  			t.Fatal(err)
   106  		}
   107  		if err = sha512Crypt.Verify(hash, d); err != nil {
   108  			t.Errorf("Test %d failed: %s", i, d)
   109  		}
   110  	}
   111  }
   112  

View as plain text