github.com/spi-ca/misc@v1.0.1/crypto/secret_test.go (about)

     1  package crypto
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"crypto/sha1"
     7  	"crypto/subtle"
     8  	"encoding/hex"
     9  	"io"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestShortEncryption1(t *testing.T) {
    15  	key := make([]byte, 32)
    16  	rand.Read(key)
    17  	box := NewSecretBox(key)
    18  	source := "h"
    19  	hasher := sha1.New()
    20  	_, err := strings.NewReader(source).WriteTo(hasher)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	formerHash := hasher.Sum(nil)
    25  	t.Log("former hash ", hex.EncodeToString(formerHash))
    26  
    27  	encryptor, err := box.NewEncryptReader(strings.NewReader(source))
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	trsnBuf := &bytes.Buffer{}
    32  	_, err = trsnBuf.ReadFrom(encryptor)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	hasher.Reset()
    37  	decrypted, err := box.NewDecryptWriter(hasher)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	_, err = trsnBuf.WriteTo(decrypted)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	_ = decrypted.Close()
    46  	latterHash := hasher.Sum(nil)
    47  	t.Log("latter hash ", hex.EncodeToString(latterHash))
    48  	if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
    49  		t.Fatal("hash not matched!")
    50  	} else {
    51  		t.Log("Hash Match!")
    52  	}
    53  }
    54  func TestShortEncryption2(t *testing.T) {
    55  	key := make([]byte, 32)
    56  	rand.Read(key)
    57  	box := NewSecretBox(key)
    58  	source := "h"
    59  	hasher := sha1.New()
    60  	_, err := strings.NewReader(source).WriteTo(hasher)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	formerHash := hasher.Sum(nil)
    65  	t.Log("former hash ", hex.EncodeToString(formerHash))
    66  
    67  	encryptor, err := box.NewEncryptReader(strings.NewReader(source))
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	trsnBuf := &bytes.Buffer{}
    72  	_, err = trsnBuf.ReadFrom(encryptor)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	hasher.Reset()
    77  	decrypted, err := box.NewDecryptReader(trsnBuf)
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	_, err = io.Copy(hasher, decrypted)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	latterHash := hasher.Sum(nil)
    86  	t.Log("latter hash ", hex.EncodeToString(latterHash))
    87  	if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
    88  		t.Fatal("hash not matched!")
    89  	} else {
    90  		t.Log("Hash Match!")
    91  	}
    92  }
    93  
    94  func TestShortEncryption3(t *testing.T) {
    95  	key := make([]byte, 32)
    96  	rand.Read(key)
    97  	box := NewSecretBox(key)
    98  	source := "h"
    99  	hasher := sha1.New()
   100  	_, err := strings.NewReader(source).WriteTo(hasher)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	formerHash := hasher.Sum(nil)
   105  	t.Log("former hash ", hex.EncodeToString(formerHash))
   106  
   107  	trsnBuf := &bytes.Buffer{}
   108  	encryptor, err := box.NewEncryptWriter(trsnBuf)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	_, err = strings.NewReader(source).WriteTo(encryptor)
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  	_ = encryptor.Close()
   117  	hasher.Reset()
   118  	decrypted, err := box.NewDecryptReader(trsnBuf)
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  	_, err = io.Copy(hasher, decrypted)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	latterHash := hasher.Sum(nil)
   127  	t.Log("latter hash ", hex.EncodeToString(latterHash))
   128  	if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
   129  		t.Fatal("hash not matched!")
   130  	} else {
   131  		t.Log("Hash Match!")
   132  	}
   133  }
   134  
   135  func TestShortEncryption4(t *testing.T) {
   136  	key := make([]byte, 32)
   137  	rand.Read(key)
   138  	box := NewSecretBox(key)
   139  	source := "h"
   140  	hasher := sha1.New()
   141  	_, err := strings.NewReader(source).WriteTo(hasher)
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  	formerHash := hasher.Sum(nil)
   146  	t.Log("former hash ", hex.EncodeToString(formerHash))
   147  
   148  	trsnBuf := &bytes.Buffer{}
   149  	encryptor, err := box.NewEncryptWriter(trsnBuf)
   150  	if err != nil {
   151  		t.Fatal(err)
   152  	}
   153  	_, err = strings.NewReader(source).WriteTo(encryptor)
   154  	if err != nil {
   155  		t.Fatal(err)
   156  	}
   157  	_ = encryptor.Close()
   158  	hasher.Reset()
   159  	decrypted, err := box.NewDecryptWriter(hasher)
   160  	if err != nil {
   161  		t.Fatal(err)
   162  	}
   163  	_, err = trsnBuf.WriteTo(decrypted)
   164  	if err != nil {
   165  		t.Fatal(err)
   166  	}
   167  	_ = decrypted.Close()
   168  	latterHash := hasher.Sum(nil)
   169  	t.Log("latter hash ", hex.EncodeToString(latterHash))
   170  	if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
   171  		t.Fatal("hash not matched!")
   172  	} else {
   173  		t.Log("Hash Match!")
   174  	}
   175  }
   176  
   177  func TestLongEncryption1(t *testing.T) {
   178  	key := make([]byte, 32)
   179  	rand.Read(key)
   180  
   181  	buf := make([]byte, 1024*1024*5)
   182  	rand.Read(buf)
   183  
   184  	box := NewSecretBox(key)
   185  	hasher := sha1.New()
   186  	_, err := hasher.Write(buf)
   187  	if err != nil {
   188  		t.Fatal(err)
   189  	}
   190  	formerHash := hasher.Sum(nil)
   191  	t.Log("former hash ", hex.EncodeToString(formerHash))
   192  
   193  	encryptor, err := box.NewEncryptReader(bytes.NewReader(buf))
   194  	if err != nil {
   195  		t.Fatal(err)
   196  	}
   197  	trsnBuf := &bytes.Buffer{}
   198  	_, err = trsnBuf.ReadFrom(encryptor)
   199  	if err != nil {
   200  		t.Fatal(err)
   201  	}
   202  	hasher.Reset()
   203  	decrypted, err := box.NewDecryptWriter(hasher)
   204  	if err != nil {
   205  		t.Fatal(err)
   206  	}
   207  	_, err = trsnBuf.WriteTo(decrypted)
   208  	if err != nil {
   209  		t.Fatal(err)
   210  	}
   211  	_ = decrypted.Close()
   212  	latterHash := hasher.Sum(nil)
   213  	t.Log("latter hash ", hex.EncodeToString(latterHash))
   214  	if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
   215  		t.Fatal("hash not matched!")
   216  	} else {
   217  		t.Log("Hash Match!")
   218  	}
   219  }
   220  func TestLongEncryption2(t *testing.T) {
   221  	key := make([]byte, 32)
   222  	rand.Read(key)
   223  	box := NewSecretBox(key)
   224  
   225  	buf := make([]byte, 1024*1024*5)
   226  	rand.Read(buf)
   227  
   228  	hasher := sha1.New()
   229  	_, err := hasher.Write(buf)
   230  	if err != nil {
   231  		t.Fatal(err)
   232  	}
   233  	formerHash := hasher.Sum(nil)
   234  	t.Log("former hash ", hex.EncodeToString(formerHash))
   235  
   236  	encryptor, err := box.NewEncryptReader(bytes.NewReader(buf))
   237  	if err != nil {
   238  		t.Fatal(err)
   239  	}
   240  	trsnBuf := &bytes.Buffer{}
   241  	_, err = trsnBuf.ReadFrom(encryptor)
   242  	if err != nil {
   243  		t.Fatal(err)
   244  	}
   245  	hasher.Reset()
   246  	decrypted, err := box.NewDecryptReader(trsnBuf)
   247  	if err != nil {
   248  		t.Fatal(err)
   249  	}
   250  	_, err = io.Copy(hasher, decrypted)
   251  	if err != nil {
   252  		t.Fatal(err)
   253  	}
   254  	latterHash := hasher.Sum(nil)
   255  	t.Log("latter hash ", hex.EncodeToString(latterHash))
   256  	if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
   257  		t.Fatal("hash not matched!")
   258  	} else {
   259  		t.Log("Hash Match!")
   260  	}
   261  }
   262  
   263  func TestLongEncryption3(t *testing.T) {
   264  	key := make([]byte, 32)
   265  	rand.Read(key)
   266  	box := NewSecretBox(key)
   267  
   268  	buf := make([]byte, 1024*1024*5)
   269  	rand.Read(buf)
   270  
   271  	hasher := sha1.New()
   272  	_, err := hasher.Write(buf)
   273  	if err != nil {
   274  		t.Fatal(err)
   275  	}
   276  	formerHash := hasher.Sum(nil)
   277  	t.Log("former hash ", hex.EncodeToString(formerHash))
   278  
   279  	trsnBuf := &bytes.Buffer{}
   280  	encryptor, err := box.NewEncryptWriter(trsnBuf)
   281  	if err != nil {
   282  		t.Fatal(err)
   283  	}
   284  	_, err = encryptor.Write(buf)
   285  	if err != nil {
   286  		t.Fatal(err)
   287  	}
   288  	_ = encryptor.Close()
   289  	hasher.Reset()
   290  	decrypted, err := box.NewDecryptReader(trsnBuf)
   291  	if err != nil {
   292  		t.Fatal(err)
   293  	}
   294  	_, err = io.Copy(hasher, decrypted)
   295  	if err != nil {
   296  		t.Fatal(err)
   297  	}
   298  	latterHash := hasher.Sum(nil)
   299  	t.Log("latter hash ", hex.EncodeToString(latterHash))
   300  	if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
   301  		t.Fatal("hash not matched!")
   302  	} else {
   303  		t.Log("Hash Match!")
   304  	}
   305  }
   306  
   307  func TestLongEncryption4(t *testing.T) {
   308  	key := make([]byte, 32)
   309  	rand.Read(key)
   310  	box := NewSecretBox(key)
   311  
   312  	buf := make([]byte, 1024*1024*5)
   313  	rand.Read(buf)
   314  
   315  	hasher := sha1.New()
   316  	_, err := hasher.Write(buf)
   317  	if err != nil {
   318  		t.Fatal(err)
   319  	}
   320  	formerHash := hasher.Sum(nil)
   321  	t.Log("former hash ", hex.EncodeToString(formerHash))
   322  
   323  	trsnBuf := &bytes.Buffer{}
   324  	encryptor, err := box.NewEncryptWriter(trsnBuf)
   325  	if err != nil {
   326  		t.Fatal(err)
   327  	}
   328  	_, err = encryptor.Write(buf)
   329  	if err != nil {
   330  		t.Fatal(err)
   331  	}
   332  	_ = encryptor.Close()
   333  	hasher.Reset()
   334  	decrypted, err := box.NewDecryptWriter(hasher)
   335  	if err != nil {
   336  		t.Fatal(err)
   337  	}
   338  	_, err = trsnBuf.WriteTo(decrypted)
   339  	if err != nil {
   340  		t.Fatal(err)
   341  	}
   342  	_ = decrypted.Close()
   343  	latterHash := hasher.Sum(nil)
   344  	t.Log("latter hash ", hex.EncodeToString(latterHash))
   345  	if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
   346  		t.Fatal("hash not matched!")
   347  	} else {
   348  		t.Log("Hash Match!")
   349  	}
   350  }
   351  
   352  //
   353  //func TestLongEncryptionDescription(t *testing.T) {
   354  //	var key [32]byte
   355  //	rand.Read(key[:])
   356  //
   357  //	buf := make([]byte, 1024*1024*5)
   358  //	rand.Read(buf)
   359  //	hasher := sha1.New()
   360  //	hasher.Write(buf)
   361  //	formerHash := hasher.Sum(nil)
   362  //	t.Log("former hash ", hex.EncodeToString(formerHash))
   363  //
   364  //	var encryptBuf bytes.Buffer
   365  //	encryptor, err := NewEncryptor(&encryptBuf, &key)
   366  //	if err != nil {
   367  //		t.Fatal("cannot create crypto", err)
   368  //	}
   369  //	_, err = encryptor.Write(buf)
   370  //	if err != nil {
   371  //		t.Fatal("cannot create encryptorGenerator", err)
   372  //	}
   373  //	err = encryptor.Close()
   374  //
   375  //	decryptor, err := NewDecryptor(&encryptBuf, &key)
   376  //	if err != nil {
   377  //		t.Fatal("cannot create decryptorGenerator", err)
   378  //	}
   379  //	decrypted, err := ioutil.ReadAll(decryptor)
   380  //	if err != nil {
   381  //		t.Fatal("error occurred while reading ", err)
   382  //	}
   383  //	hasher.Reset()
   384  //	hasher.Write(decrypted)
   385  //	latterHash := hasher.Sum(nil)
   386  //	t.Log("former hash ", hex.EncodeToString(latterHash))
   387  //	if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
   388  //		t.Fatal("hash not matched!")
   389  //	} else {
   390  //		t.Log("Hash Match!")
   391  //	}
   392  //}