Пример #1
0
// Decrypt decrypts sensitive fields in this instance using key namely
// UName, Password, and Special. Decrypt returns ErrKeyMismatch if the
// provided key does not have the same Id as the key used to encrypt.
func (e *Entry) Decrypt(key *Key) (err error) {
	if e.Owner != key.Id {
		return ErrKeyMismatch
	}
	if e.UName, err = aes.Decrypt(e.UName, key.Value); err != nil {
		return err
	}
	if e.Password, err = aes.Decrypt(e.Password, key.Value); err != nil {
		return err
	}
	if e.Special, err = aes.Decrypt(e.Special, key.Value); err != nil {
		return err
	}
	return
}
Пример #2
0
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")
	}
}
Пример #3
0
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))
	}
}