github.com/igggame/nebulas-go@v2.1.0+incompatible/crypto/cipher/scrypt_test.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package cipher
    20  
    21  import (
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/nebulasio/go-nebulas/util/byteutils"
    26  )
    27  
    28  func TestScrypt_Encrypt(t *testing.T) {
    29  	passphrase := []byte("passphrase")
    30  	hash1, _ := byteutils.FromHex("0eb3be2db3a534c192be5570c6c42f59")
    31  	hash2, _ := byteutils.FromHex("5e6d587f26121f96a07cf4b8b569aac1")
    32  	hash3, _ := byteutils.FromHex("c7174759e86c59dcb7df87def82f61eb")
    33  
    34  	scrypt := new(Scrypt)
    35  	tests := []struct {
    36  		name string
    37  		data []byte
    38  	}{
    39  		{
    40  			"test1",
    41  			hash1,
    42  		},
    43  		{
    44  			"test2",
    45  			hash2,
    46  		},
    47  		{
    48  			"test3",
    49  			hash3,
    50  		},
    51  	}
    52  	for _, tt := range tests {
    53  		t.Run(tt.name, func(t *testing.T) {
    54  			got, err := scrypt.Encrypt(tt.data, passphrase)
    55  			if err != nil {
    56  				t.Errorf("Encrypt() error = %v", err)
    57  				return
    58  			}
    59  			want, err := scrypt.Decrypt(got, passphrase)
    60  			if err != nil {
    61  				t.Errorf("Decrypt() error = %v", err)
    62  				return
    63  			}
    64  			if !reflect.DeepEqual(tt.data, want) {
    65  				t.Errorf("Decrypt() = %v, data %v", want, tt.data)
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func TestScrypt_DecryptKey(t *testing.T) {
    72  
    73  	passphrase := []byte("qwertyuiop")
    74  
    75  	tests := []struct {
    76  		name string
    77  		key  string
    78  	}{
    79  		{
    80  			"version3",
    81  			`{
    82  					"version":3,
    83  					"id":"3913ded3-2707-4a25-996a-807265dc0cdf",
    84  					"address":"70e30fcae5e7f4b2460faaa9e5b1bd912332ebb5",
    85  					"Crypto":{
    86  						"ciphertext":"30c9606797a6e4fd5bb8e91694184ecdb9ab0230c453fe1922732a1e3212301c",
    87  						"cipherparams":{
    88  							"iv":"65d14cb11d6bb6e57dff0d12346637cc"
    89  						},
    90  						"cipher":"aes-128-ctr",
    91  						"kdf":"scrypt",
    92  						"kdfparams":{
    93  							"dklen":32,
    94  							"salt":"8728c5a28888692acb5e28ee46bdc7935b8306dfece2c6d0cd003b2cbc259af2",
    95  							"n":1024,
    96  							"r":8,
    97  							"p":1
    98  						},
    99  						"mac":"a22874c9c35e365e305b1defe6663bde930d2efbcc6c3d0db192ff44bd9dfa7c"
   100  					}
   101  				  }`,
   102  		},
   103  		{
   104  			"version4",
   105  			`{
   106  					"version":4,
   107  					"id":"3913ded3-2707-4a25-996a-807265dc0cdf",
   108  					"address":"70e30fcae5e7f4b2460faaa9e5b1bd912332ebb5",
   109  					"Crypto":{
   110  						"ciphertext":"30c9606797a6e4fd5bb8e91694184ecdb9ab0230c453fe1922732a1e3212301c",
   111  						"cipherparams":{
   112  							"iv":"65d14cb11d6bb6e57dff0d12346637cc"
   113  						},
   114  						"cipher":"aes-128-ctr",
   115  						"kdf":"scrypt",
   116  						"kdfparams":{
   117  							"dklen":32,
   118  							"salt":"8728c5a28888692acb5e28ee46bdc7935b8306dfece2c6d0cd003b2cbc259af2",
   119  							"n":1024,
   120  							"r":8,
   121  							"p":1
   122  						},
   123  						"mac":"c213e1567ce01702e236fbff6a00481d0478b4844cfe992d1604e0d4af20a79e"
   124  					}
   125                    }`,
   126  		},
   127  	}
   128  
   129  	for _, tt := range tests {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			key := tt.key
   132  			s := new(Scrypt)
   133  			_, err := s.DecryptKey([]byte(key), passphrase)
   134  			if err != nil {
   135  				t.Errorf("DecryptKey() error = %v", err)
   136  				return
   137  			}
   138  		})
   139  	}
   140  }