// Encrypt encrypts sensitive fields in this instance using key namely // UName, Password, and Special func (e *Entry) Encrypt(key *Key) (err error) { if e.UName, err = aes.Encrypt(e.UName, key.Value); err != nil { return err } if e.Password, err = aes.Encrypt(e.Password, key.Value); err != nil { return err } if e.Special, err = aes.Encrypt(e.Special, key.Value); err != nil { return err } e.Owner = key.Id return }
func TestEncryptsSameTextDifferently(t *testing.T) { encoded, err := aes.Encrypt("aardvark", someKey) if err != nil { t.Fatal(err) } encodedAgain, err := aes.Encrypt("aardvark", someKey) if err != nil { t.Fatal(err) } if encoded == encodedAgain { t.Error("Expected same text to be encrypted differently every time.") } }
func TestEncryptSecurity(t *testing.T) { anotherKey := []byte("12345678901234567890123456789013") encoded, err := aes.Encrypt("aardvark", someKey) if err != nil { t.Fatal(err) } decoded, _ := aes.Decrypt(encoded, anotherKey) if decoded == "aardvark" { t.Error("Expected different decryption with different key") } }
func verifyEncryptDecrypt(t *testing.T, plain string) { encoded, err := aes.Encrypt(plain, someKey) if err != nil { t.Fatal(err) } decoded, err := aes.Decrypt(encoded, someKey) if err != nil { t.Fatal(err) } if decoded != plain { t.Errorf("Expected to get same thing back: '%s', got '%s' %d %d", plain, decoded, len(plain), len(decoded)) } }