88 lines
2.1 KiB
Go
Executable File
88 lines
2.1 KiB
Go
Executable File
package encryptor
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func Test_Encrypter_ToFromFiles(t *testing.T) {
|
|
pubFile, err := ioutil.TempFile(".", "test_pub***.key")
|
|
if err != nil {
|
|
t.Fatalf("could not create temp pub file " + err.Error())
|
|
}
|
|
defer os.Remove(pubFile.Name())
|
|
|
|
priFile, err := ioutil.TempFile(".", "test_pri***.key")
|
|
if err != nil {
|
|
t.Fatalf("could not create temp pri file " + err.Error())
|
|
}
|
|
defer os.Remove(priFile.Name())
|
|
|
|
e := NewEncryptor("", "")
|
|
if err := e.ToFiles(priFile.Name(), pubFile.Name()); err != nil {
|
|
t.Fatalf("Could not save to files: %v", err)
|
|
}
|
|
if err := e.FromFiles(priFile.Name(), pubFile.Name()); err != nil {
|
|
t.Fatalf("Could not load from files: %v", err)
|
|
}
|
|
|
|
tocrypt := "can you hear"
|
|
encrypted := e.Encrypt(tocrypt)
|
|
if len(encrypted) == 0 {
|
|
t.Fatalf("cannot encrypt after fromFiles")
|
|
}
|
|
decrypted := e.Decrypt(encrypted)
|
|
if decrypted != tocrypt {
|
|
t.Fatalf("cannot decrypt after fromFiles")
|
|
}
|
|
}
|
|
|
|
func Test_EncrypterSym(t *testing.T) {
|
|
private, public := NewKeyPair()
|
|
e := NewEncryptor(private, public)
|
|
msg := "can you hear me screaming"
|
|
e.SetPublic(public)
|
|
e.SetSymmetric(e.GetPublic())
|
|
for i := 0; i < 2; i++ {
|
|
encrypted := e.Encrypt(msg)
|
|
decrypted := e.Decrypt(encrypted)
|
|
if decrypted != msg {
|
|
t.Errorf("decrypt(enrypt(%v)) not the same", msg)
|
|
}
|
|
}
|
|
}
|
|
|
|
func Test_EncryptorAsym(t *testing.T) {
|
|
private, public := NewKeyPair()
|
|
e := NewEncryptor(private, public)
|
|
msg := "can you hear me screaming"
|
|
e.SetPublic(public)
|
|
e.SetSymmetric("")
|
|
for i := 0; i < 2; i++ {
|
|
encrypted := e.Encrypt(msg)
|
|
decrypted := e.Decrypt(encrypted)
|
|
if decrypted != msg {
|
|
t.Errorf("decrypt(enrypt(%v)) not the same", msg)
|
|
}
|
|
}
|
|
}
|
|
|
|
func Test_GetSet(t *testing.T) {
|
|
privateA, publicA := NewKeyPair()
|
|
privateB, publicB := NewKeyPair()
|
|
eA := NewEncryptor(privateA, publicA)
|
|
eB := NewEncryptor(privateB, publicB)
|
|
|
|
eA.SetPublic(publicB)
|
|
//eB.SetPublic(publicA)
|
|
|
|
msg := "hello from A to B"
|
|
encrypted := eA.Encrypt(msg)
|
|
decrypted := eB.Decrypt(encrypted)
|
|
|
|
if decrypted != msg {
|
|
t.Errorf("decrypt(enrypt(%v)) not the same", msg)
|
|
}
|
|
}
|