github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/encrypt/ssh_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package encrypt
     7  
     8  import (
     9  	"bytes"
    10  	"testing"
    11  )
    12  
    13  func Test_SshEncDec(t *testing.T) {
    14  	pub, priv, err := GenerateSshKeyPairs()
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  	// pub = strings.TrimPrefix(pub, "ssh-rsa ")
    19  
    20  	for _, data := range [][]byte{
    21  		[]byte("hello test"),
    22  		[]byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
    23  	} {
    24  		encrypted, err := EncryptWithSshKey(data, []byte(pub))
    25  		if err != nil {
    26  			panic(err)
    27  		}
    28  
    29  		data2, err := DecryptWithSshKey(encrypted, priv)
    30  		if err != nil {
    31  			panic(err)
    32  		}
    33  
    34  		if !bytes.Equal(data, data2) {
    35  			panic("missmatch")
    36  		}
    37  	}
    38  }